1 package org.bouncycastle.crypto; 2 3 /** 4 * super class for all Password Based Encryption (PBE) parameter generator classes. 5 */ 6 public abstract class PBEParametersGenerator 7 { 8 protected byte[] password; 9 protected byte[] salt; 10 protected int iterationCount; 11 12 /** 13 * base constructor. 14 */ PBEParametersGenerator()15 protected PBEParametersGenerator() 16 { 17 } 18 19 /** 20 * initialise the PBE generator. 21 * 22 * @param password the password converted into bytes (see below). 23 * @param salt the salt to be mixed with the password. 24 * @param iterationCount the number of iterations the "mixing" function 25 * is to be applied for. 26 */ init( byte[] password, byte[] salt, int iterationCount)27 public void init( 28 byte[] password, 29 byte[] salt, 30 int iterationCount) 31 { 32 this.password = password; 33 this.salt = salt; 34 this.iterationCount = iterationCount; 35 } 36 37 /** 38 * return the password byte array. 39 * 40 * @return the password byte array. 41 */ getPassword()42 public byte[] getPassword() 43 { 44 return password; 45 } 46 47 /** 48 * return the salt byte array. 49 * 50 * @return the salt byte array. 51 */ getSalt()52 public byte[] getSalt() 53 { 54 return salt; 55 } 56 57 /** 58 * return the iteration count. 59 * 60 * @return the iteration count. 61 */ getIterationCount()62 public int getIterationCount() 63 { 64 return iterationCount; 65 } 66 67 /** 68 * generate derived parameters for a key of length keySize. 69 * 70 * @param keySize the length, in bits, of the key required. 71 * @return a parameters object representing a key. 72 */ generateDerivedParameters(int keySize)73 public abstract CipherParameters generateDerivedParameters(int keySize); 74 75 /** 76 * generate derived parameters for a key of length keySize, and 77 * an initialisation vector (IV) of length ivSize. 78 * 79 * @param keySize the length, in bits, of the key required. 80 * @param ivSize the length, in bits, of the iv required. 81 * @return a parameters object representing a key and an IV. 82 */ generateDerivedParameters(int keySize, int ivSize)83 public abstract CipherParameters generateDerivedParameters(int keySize, int ivSize); 84 85 /** 86 * generate derived parameters for a key of length keySize, specifically 87 * for use with a MAC. 88 * 89 * @param keySize the length, in bits, of the key required. 90 * @return a parameters object representing a key. 91 */ generateDerivedMacParameters(int keySize)92 public abstract CipherParameters generateDerivedMacParameters(int keySize); 93 94 /** 95 * converts a password to a byte array according to the scheme in 96 * PKCS5 (ascii, no padding) 97 * 98 * @param password a character array reqpresenting the password. 99 * @return a byte array representing the password. 100 */ PKCS5PasswordToBytes( char[] password)101 public static byte[] PKCS5PasswordToBytes( 102 char[] password) 103 { 104 byte[] bytes = new byte[password.length]; 105 106 for (int i = 0; i != bytes.length; i++) 107 { 108 bytes[i] = (byte)password[i]; 109 } 110 111 return bytes; 112 } 113 114 /** 115 * converts a password to a byte array according to the scheme in 116 * PKCS12 (unicode, big endian, 2 zero pad bytes at the end). 117 * 118 * @param password a character array reqpresenting the password. 119 * @return a byte array representing the password. 120 */ PKCS12PasswordToBytes( char[] password)121 public static byte[] PKCS12PasswordToBytes( 122 char[] password) 123 { 124 // BEGIN android-changed 125 if (password != null && password.length > 0) 126 { 127 // +1 for extra 2 pad bytes. 128 byte[] bytes = new byte[(password.length + 1) * 2]; 129 130 for (int i = 0; i != password.length; i ++) 131 { 132 bytes[i * 2] = (byte)(password[i] >>> 8); 133 bytes[i * 2 + 1] = (byte)password[i]; 134 } 135 136 return bytes; 137 } 138 else 139 { 140 return new byte[0]; 141 } 142 // END android-changed 143 } 144 } 145