Forums Register Login

Triple DES decryption

+Pie Number of slices to send: Send
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]
+Pie Number of slices to send: Send
Try to use the same Cipher-Object for encrypting and decrypting. Just initialize it new in the different methods. This worked for me.
+Pie Number of slices to send: Send
If you don't mind, could you please show some code as an example?
thanks for the reply!
+Pie Number of slices to send: Send
In your members you should have only



and in your constructor



and then in encrypt()



and in decrypt()

+Pie Number of slices to send: Send
Thanks a lot Frank, i will try that!
New rule: no elephants at the chess tournament. Tiny ads are still okay.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 5044 times.
Similar Threads
IllegalBlockSizeException while decrypting using Triple DES
BadPaddingException
Characters jumbled pls help
password Encryption in WSAD environment
Encrypting & Decrypting an XML File using Base64Encoder & Base64Decoder
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:33:42.