KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > authentication > PBE


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19
20
21 package org.openharmonise.him.authentication;
22 import java.util.Random JavaDoc;
23
24 import javax.crypto.Cipher;
25 import javax.crypto.SecretKey;
26 import javax.crypto.SecretKeyFactory;
27 import javax.crypto.spec.PBEKeySpec;
28 import javax.crypto.spec.PBEParameterSpec;
29
30 import sun.misc.BASE64Decoder;
31 import sun.misc.BASE64Encoder;
32
33 /**
34  * Class which encrypts and decrypts plain text using PBEWithMD5AndDES.
35  *
36  * @author Matthew Large
37  * @version $Revision: 1.1 $
38  *
39  */

40
41 public class PBE {
42     private static int ITERATIONS = 1000;
43
44     /**
45      * Encrypts supplied text using PBEWithMD5AndDES, with the supplied password
46      * as the key.
47      *
48      * @param password Password to act as the key
49      * @param plaintext Text to encrypt
50      * @return Encrypted text
51      * @throws Exception
52      */

53     public static String JavaDoc encrypt(char[] password, String JavaDoc plaintext)
54         throws Exception JavaDoc {
55         //Begin by creating a random salt of 64 bits (8bytes)
56
byte[] salt = new byte[8];
57         Random JavaDoc random = new Random JavaDoc();
58         random.nextBytes(salt);
59         //Create the PBEKeySpec with the given password
60
PBEKeySpec keySpec = new PBEKeySpec(password);
61         //Get a SecretKeyFactory for PBEWithMD5AndDES
62
SecretKeyFactory keyFactory =
63             SecretKeyFactory.getInstance("PBEWithMD5AndDES");
64         //Create our key
65
SecretKey key = keyFactory.generateSecret(keySpec);
66         //Now create a parameter spec for our salt and iterations
67
PBEParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATIONS);
68         //Create a cipher and initialize it for encrypting
69
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
70         cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
71         byte[] ciphertext = cipher.doFinal(plaintext.getBytes());
72         BASE64Encoder encoder = new BASE64Encoder();
73         String JavaDoc saltString = encoder.encode(salt);
74         String JavaDoc ciphertextString = encoder.encode(ciphertext);
75         return saltString + ciphertextString;
76     }
77
78     /**
79      * Dencrypts supplied text using PBEWithMD5AndDES, with the supplied password
80      * as the key.
81      *
82      * @param password Password to act as the key
83      * @param text Text to dencrypt
84      * @return Dencrypted text
85      * @throws Exception
86      */

87     public static String JavaDoc decrypt(char[] password, String JavaDoc text)
88         throws Exception JavaDoc {
89         //Begin by splitting the text into salt and text Strings
90
//salt is first 12 chars, BASE64 encoded from 8 bytes.
91
String JavaDoc salt = text.substring(0, 12);
92         String JavaDoc ciphertext = text.substring(12, text.length());
93         //BASE64Decode the bytes for the salt and the ciphertext
94
BASE64Decoder decoder = new BASE64Decoder();
95         byte[] saltArray = decoder.decodeBuffer(salt);
96         byte[] ciphertextArray = decoder.decodeBuffer(ciphertext);
97         //Create the PBEKeySpec with the given password
98
PBEKeySpec keySpec = new PBEKeySpec(password);
99         //Get a SecretKeyFactory for PBEWithMD5AndDES
100
SecretKeyFactory keyFactory =
101             SecretKeyFactory.getInstance("PBEWithMD5AndDES");
102         //Create our key
103
SecretKey key = keyFactory.generateSecret(keySpec);
104         //Now create a parameterspec for our salt and iterations
105
PBEParameterSpec paramSpec =
106             new PBEParameterSpec(saltArray, ITERATIONS);
107         //Create a cipher and initializeit for encrypting
108
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
109         cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
110         //Perform the actual decryption
111
byte[] plaintextArray = cipher.doFinal(ciphertextArray);
112         return new String JavaDoc(plaintextArray);
113     }
114 }
115
Popular Tags