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

Save This Page
Home » crypto-143 » org.bouncycastle.crypto » [javadoc | source]