01: package Shared.Logging.Internal;
02:
03: import javax.crypto.*;
04: import javax.crypto.spec.*;
05: import javax.crypto.interfaces.*;
06:
07: public class CypherFactory {
08:
09: /**
10: * Creates a cipher in the mode Cipher.ENCRYPT_MODE for encripting
11: * or Cipher.DECRYPT_MODE for decripting.
12: */
13: public static Cipher CreatePasswordBasedCipher(int cipherMode)
14: throws Exception {
15: // Encryption:
16: // Salt
17: byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21,
18: (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee,
19: (byte) 0x99 };
20: // Iteration count
21: int count = 20;
22: char[] pwd = new char[] { 'd', 'z', '2', 'x', 'p', 'a', 'q',
23: 'i', 'm', 'r' };
24: // Create PBE parameter set
25: PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt,
26: count);
27: PBEKeySpec pbeKeySpec = new PBEKeySpec(pwd);
28: SecretKeyFactory keyFac = SecretKeyFactory
29: .getInstance("PBEWithMD5AndDES");
30: SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
31: // Create PBE Cipher
32: Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
33: // Initialize PBE Cipher with key and parameters
34: pbeCipher.init(cipherMode, pbeKey, pbeParamSpec);
35: return pbeCipher;
36: }
37:
38: } // CypherFactory
|