Hi,
I am trying to encrypt a password and store the same in DB then decrypt again. This is my program. i am getting an error
"Exception = Given final block not properly padded"
Can anyone help me on this please?
thanks a lot!
[CODE]
public class TripleDESPasswordEncrypt
{
private String characterEncoding = "ASCII";
private Cipher encryptCipher;
private Cipher decryptCipher;
private BASE64Encoder base64Encoder = new BASE64Encoder();
private BASE64Decoder base64Decoder = new BASE64Decoder();
private static TripleDESPasswordEncrypt tripleDESPWDEncryptInstance;
private static String key="123453760666666555551205";
public TripleDESPasswordEncrypt() throws Exception
{
// required for initialization vector IV for CBC mode
final byte[] ivBytes ={0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07};
System.out.println("Inside instance.........");
//SecureRandom random=SecureRandom.getInstance("SHA1PRNG");
//SecureRandom random=SecureRandom.getInstance("IBMSecureRandom");
SecureRandom random=new SecureRandom(key.getBytes());
KeyGenerator kg=KeyGenerator.getInstance("DESede");
kg.init(168,random); //key must be initialize to 168 bit size required as per Fidelity standards
SecretKey key = kg.generateKey();
IvParameterSpec iv = new IvParameterSpec(ivBytes);
this.characterEncoding = characterEncoding;
this.encryptCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
this.encryptCipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key, iv);
this.decryptCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
this.decryptCipher.init(javax.crypto.Cipher.DECRYPT_MODE, key, iv);
}
synchronized public String encrypt(String password) throws Exception
{
// before encrypting, make sure that the password is in fact ASCII.
if (!password.matches("[ -~]{6,}"))
throw new IllegalArgumentException("Password must be printable ASCII");
System.out.println("Password ....................[" + password +"]");
byte[] passwordBytes = password.getBytes(characterEncoding);
byte[] encryptedPasswordBytes = this.encryptCipher.doFinal(passwordBytes);
String encodedEncryptedPassword =this.base64Encoder.encode(encryptedPasswordBytes);
System.out.println("Password encrypted successfully!!!");
return encodedEncryptedPassword;
}
synchronized public String decrypt(String encodedEncryptedPassword)throws Exception
{
System.out.println("Could not decrypt password !!!");
byte[] encryptedPasswordBytes = this.base64Decoder.decodeBuffer(encodedEncryptedPassword);
byte[] passwordBytes = this.decryptCipher.doFinal(encryptedPasswordBytes);
String recoveredPassword = new String(passwordBytes,characterEncoding);
System.out.println("Password decrypted successfully!!!");
return recoveredPassword;
}
//Create only one instance of TripleDESPasswordEncrypt
public static synchronized TripleDESPasswordEncrypt getInstance() throws Exception
{
if(tripleDESPWDEncryptInstance == null)
{
return new TripleDESPasswordEncrypt();
}
else
{
return tripleDESPWDEncryptInstance;
}
}
}
[CODE]
I am trying to encrypt a password and store the same in DB then decrypt again. This is my program. i am getting an error
"Exception = Given final block not properly padded"
Can anyone help me on this please?
thanks a lot!
[CODE]
public class TripleDESPasswordEncrypt
{
private String characterEncoding = "ASCII";
private Cipher encryptCipher;
private Cipher decryptCipher;
private BASE64Encoder base64Encoder = new BASE64Encoder();
private BASE64Decoder base64Decoder = new BASE64Decoder();
private static TripleDESPasswordEncrypt tripleDESPWDEncryptInstance;
private static String key="123453760666666555551205";
public TripleDESPasswordEncrypt() throws Exception
{
// required for initialization vector IV for CBC mode
final byte[] ivBytes ={0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07};
System.out.println("Inside instance.........");
//SecureRandom random=SecureRandom.getInstance("SHA1PRNG");
//SecureRandom random=SecureRandom.getInstance("IBMSecureRandom");
SecureRandom random=new SecureRandom(key.getBytes());
KeyGenerator kg=KeyGenerator.getInstance("DESede");
kg.init(168,random); //key must be initialize to 168 bit size required as per Fidelity standards
SecretKey key = kg.generateKey();
IvParameterSpec iv = new IvParameterSpec(ivBytes);
this.characterEncoding = characterEncoding;
this.encryptCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
this.encryptCipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key, iv);
this.decryptCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
this.decryptCipher.init(javax.crypto.Cipher.DECRYPT_MODE, key, iv);
}
synchronized public String encrypt(String password) throws Exception
{
// before encrypting, make sure that the password is in fact ASCII.
if (!password.matches("[ -~]{6,}"))
throw new IllegalArgumentException("Password must be printable ASCII");
System.out.println("Password ....................[" + password +"]");
byte[] passwordBytes = password.getBytes(characterEncoding);
byte[] encryptedPasswordBytes = this.encryptCipher.doFinal(passwordBytes);
String encodedEncryptedPassword =this.base64Encoder.encode(encryptedPasswordBytes);
System.out.println("Password encrypted successfully!!!");
return encodedEncryptedPassword;
}
synchronized public String decrypt(String encodedEncryptedPassword)throws Exception
{
System.out.println("Could not decrypt password !!!");
byte[] encryptedPasswordBytes = this.base64Decoder.decodeBuffer(encodedEncryptedPassword);
byte[] passwordBytes = this.decryptCipher.doFinal(encryptedPasswordBytes);
String recoveredPassword = new String(passwordBytes,characterEncoding);
System.out.println("Password decrypted successfully!!!");
return recoveredPassword;
}
//Create only one instance of TripleDESPasswordEncrypt
public static synchronized TripleDESPasswordEncrypt getInstance() throws Exception
{
if(tripleDESPWDEncryptInstance == null)
{
return new TripleDESPasswordEncrypt();
}
else
{
return tripleDESPWDEncryptInstance;
}
}
}
[CODE]