Monday, July 24, 2006

Password Based Encryption With MD5 and DES algorithms


package com.pavan.text.encryption;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
*
* @author pgupta
*
*/
public class PasswordTest {
//Predefined key for encryption and decryption.
public static final String key = "PaVaNkUmArGuPtA";

public static void main(String[] args) {
PasswordTest pt = new PasswordTest();
String encryptedString = pt.Encrypt("pgupta");
System.out.println("encryptedString: "+encryptedString);

String decryptedString = pt.Decrypt(encryptedString);
System.out.println("decryptedString: "+decryptedString);
}

/**
* Encrypts the given string using Password Based Encryption With MD5 and DES algorithm.
* @param password
* @return
*/
public String Encrypt(String password){
PBEKeySpec pbeKeySpec;
PBEParameterSpec pbeParamSpec;
SecretKeyFactory keyFac;
// Salt
byte[] salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};
// Iteration count
int count = 20;
try {
// Create PBE parameter set
pbeParamSpec = new PBEParameterSpec(salt, count);
pbeKeySpec = new PBEKeySpec(PasswordTest.key.toCharArray());
keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
// Create PBE Cipher
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
// Initialize PBE Cipher with key and parameters
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
// Our cleartext
byte[] cleartext = password.getBytes();
// Encrypt the cleartext
byte[] encryptedPassword = pbeCipher.doFinal(cleartext);
String hash = (new BASE64Encoder()).encode(encryptedPassword);
return hash;

} catch (Exception e) {

}
return null;
}

/**
* Decrypts the given string using Password Based Decryption With MD5 and DES algorithm.
* @param encryptedPassword
* @return
*/
public String Decrypt(String encryptedPassword){
try {
PBEKeySpec pbeKeySpec;
PBEParameterSpec pbeParamSpec;
SecretKeyFactory keyFac;
// Salt
byte[] salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};
// Iteration count
int count = 20;
// Create PBE parameter set
pbeParamSpec = new PBEParameterSpec(salt, count);
pbeKeySpec = new PBEKeySpec(PasswordTest.key.toCharArray());
keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
// Create PBE Cipher
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
// Initialize PBE Cipher with key and parameters
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

//decrypt
pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
byte hash[] = (new BASE64Decoder()).decodeBuffer(encryptedPassword);

byte[] decryptPassword = pbeCipher.doFinal(hash);
return new String(decryptPassword);
} catch (Exception e) {

}
return null;
}


}

0 Comments:

Post a Comment

<< Home