001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019:
020: package org.openharmonise.him.authentication;
021:
022: import java.util.Random;
023:
024: import javax.crypto.Cipher;
025: import javax.crypto.SecretKey;
026: import javax.crypto.SecretKeyFactory;
027: import javax.crypto.spec.PBEKeySpec;
028: import javax.crypto.spec.PBEParameterSpec;
029:
030: import sun.misc.BASE64Decoder;
031: import sun.misc.BASE64Encoder;
032:
033: /**
034: * Class which encrypts and decrypts plain text using PBEWithMD5AndDES.
035: *
036: * @author Matthew Large
037: * @version $Revision: 1.1 $
038: *
039: */
040:
041: public class PBE {
042: private static int ITERATIONS = 1000;
043:
044: /**
045: * Encrypts supplied text using PBEWithMD5AndDES, with the supplied password
046: * as the key.
047: *
048: * @param password Password to act as the key
049: * @param plaintext Text to encrypt
050: * @return Encrypted text
051: * @throws Exception
052: */
053: public static String encrypt(char[] password, String plaintext)
054: throws Exception {
055: //Begin by creating a random salt of 64 bits (8bytes)
056: byte[] salt = new byte[8];
057: Random random = new Random();
058: random.nextBytes(salt);
059: //Create the PBEKeySpec with the given password
060: PBEKeySpec keySpec = new PBEKeySpec(password);
061: //Get a SecretKeyFactory for PBEWithMD5AndDES
062: SecretKeyFactory keyFactory = SecretKeyFactory
063: .getInstance("PBEWithMD5AndDES");
064: //Create our key
065: SecretKey key = keyFactory.generateSecret(keySpec);
066: //Now create a parameter spec for our salt and iterations
067: PBEParameterSpec paramSpec = new PBEParameterSpec(salt,
068: ITERATIONS);
069: //Create a cipher and initialize it for encrypting
070: Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
071: cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
072: byte[] ciphertext = cipher.doFinal(plaintext.getBytes());
073: BASE64Encoder encoder = new BASE64Encoder();
074: String saltString = encoder.encode(salt);
075: String ciphertextString = encoder.encode(ciphertext);
076: return saltString + ciphertextString;
077: }
078:
079: /**
080: * Dencrypts supplied text using PBEWithMD5AndDES, with the supplied password
081: * as the key.
082: *
083: * @param password Password to act as the key
084: * @param text Text to dencrypt
085: * @return Dencrypted text
086: * @throws Exception
087: */
088: public static String decrypt(char[] password, String text)
089: throws Exception {
090: //Begin by splitting the text into salt and text Strings
091: //salt is first 12 chars, BASE64 encoded from 8 bytes.
092: String salt = text.substring(0, 12);
093: String ciphertext = text.substring(12, text.length());
094: //BASE64Decode the bytes for the salt and the ciphertext
095: BASE64Decoder decoder = new BASE64Decoder();
096: byte[] saltArray = decoder.decodeBuffer(salt);
097: byte[] ciphertextArray = decoder.decodeBuffer(ciphertext);
098: //Create the PBEKeySpec with the given password
099: PBEKeySpec keySpec = new PBEKeySpec(password);
100: //Get a SecretKeyFactory for PBEWithMD5AndDES
101: SecretKeyFactory keyFactory = SecretKeyFactory
102: .getInstance("PBEWithMD5AndDES");
103: //Create our key
104: SecretKey key = keyFactory.generateSecret(keySpec);
105: //Now create a parameterspec for our salt and iterations
106: PBEParameterSpec paramSpec = new PBEParameterSpec(saltArray,
107: ITERATIONS);
108: //Create a cipher and initializeit for encrypting
109: Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
110: cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
111: //Perform the actual decryption
112: byte[] plaintextArray = cipher.doFinal(ciphertextArray);
113: return new String(plaintextArray);
114: }
115: }
|