Hi,
I have written the code which uses a password to encrypt and decrypt the text that I have passed. This is just the sample code that has been written. I was trying figure out how to make this work because I have to use the password for encrypting the text at one end and the same password at the other end to decrypt the encrypted text. The algorithm that I have used is PBEWithMD5AndDES. There is no compilation error but at runtime error it gives
Exception java.security.NoAlgorithmException: Algorithm not found. [SecretKeyFactory.PBEWithMD5AndDES]
This exception occurs when I try to instantaite the SecretKeyFactory object ( keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); )
I am unable to figure out why. Can someone please help me out.
publi void encrytDecrypt
{
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);
// Prompt user for encryption password.
// Collect user password as char array (using the
// "readPasswd" method from above), and convert
// it into a SecretKey object, using a PBE key
// factory.
String pass = "shweta";
System.out.print("Enter encryption password: ");
System.out.flush();
pbeKeySpec = new PBEKeySpec(pass.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 = "This is another example".getBytes();
// Encrypt the cleartext
byte[] ciphertext = pbeCipher.doFinal(cleartext);
System.out.println("before encrypted text "+cleartext);
System.out.println("the encrypted text "+ciphertext);
pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
// Decrypt the ciphertext
byte[] cleartext1 = pbeCipher.doFinal(ciphertext);
System.out.println("the decrypted text "+cleartext1);
}
Thanks and Regards,
Shweta