001: package org.bouncycastle.crypto;
002:
003: /**
004: * super class for all Password Based Encryption (PBE) parameter generator classes.
005: */
006: public abstract class PBEParametersGenerator {
007: protected byte[] password;
008: protected byte[] salt;
009: protected int iterationCount;
010:
011: /**
012: * base constructor.
013: */
014: protected PBEParametersGenerator() {
015: }
016:
017: /**
018: * initialise the PBE generator.
019: *
020: * @param password the password converted into bytes (see below).
021: * @param salt the salt to be mixed with the password.
022: * @param iterationCount the number of iterations the "mixing" function
023: * is to be applied for.
024: */
025: public void init(byte[] password, byte[] salt, int iterationCount) {
026: this .password = password;
027: this .salt = salt;
028: this .iterationCount = iterationCount;
029: }
030:
031: /**
032: * return the password byte array.
033: *
034: * @return the password byte array.
035: */
036: public byte[] getPassword() {
037: return password;
038: }
039:
040: /**
041: * return the salt byte array.
042: *
043: * @return the salt byte array.
044: */
045: public byte[] getSalt() {
046: return salt;
047: }
048:
049: /**
050: * return the iteration count.
051: *
052: * @return the iteration count.
053: */
054: public int getIterationCount() {
055: return iterationCount;
056: }
057:
058: /**
059: * generate derived parameters for a key of length keySize.
060: *
061: * @param keySize the length, in bits, of the key required.
062: * @return a parameters object representing a key.
063: */
064: public abstract CipherParameters generateDerivedParameters(
065: int keySize);
066:
067: /**
068: * generate derived parameters for a key of length keySize, and
069: * an initialisation vector (IV) of length ivSize.
070: *
071: * @param keySize the length, in bits, of the key required.
072: * @param ivSize the length, in bits, of the iv required.
073: * @return a parameters object representing a key and an IV.
074: */
075: public abstract CipherParameters generateDerivedParameters(
076: int keySize, int ivSize);
077:
078: /**
079: * generate derived parameters for a key of length keySize, specifically
080: * for use with a MAC.
081: *
082: * @param keySize the length, in bits, of the key required.
083: * @return a parameters object representing a key.
084: */
085: public abstract CipherParameters generateDerivedMacParameters(
086: int keySize);
087:
088: /**
089: * converts a password to a byte array according to the scheme in
090: * PKCS5 (ascii, no padding)
091: *
092: * @param password a character array reqpresenting the password.
093: * @return a byte array representing the password.
094: */
095: public static byte[] PKCS5PasswordToBytes(char[] password) {
096: byte[] bytes = new byte[password.length];
097:
098: for (int i = 0; i != bytes.length; i++) {
099: bytes[i] = (byte) password[i];
100: }
101:
102: return bytes;
103: }
104:
105: /**
106: * converts a password to a byte array according to the scheme in
107: * PKCS12 (unicode, big endian, 2 zero pad bytes at the end).
108: *
109: * @param password a character array representing the password.
110: * @return a byte array representing the password.
111: */
112: public static byte[] PKCS12PasswordToBytes(char[] password) {
113: if (password.length > 0) {
114: // +1 for extra 2 pad bytes.
115: byte[] bytes = new byte[(password.length + 1) * 2];
116:
117: for (int i = 0; i != password.length; i++) {
118: bytes[i * 2] = (byte) (password[i] >>> 8);
119: bytes[i * 2 + 1] = (byte) password[i];
120: }
121:
122: return bytes;
123: } else {
124: return new byte[0];
125: }
126: }
127: }
|