File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Triple DES decryption Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Triple DES decryption" Watch "Triple DES decryption" New topic
Author

Triple DES decryption

Monoj Barma
Greenhorn

Joined: Oct 20, 2005
Posts: 9
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]
Frank Ertl
Ranch Hand

Joined: Apr 25, 2005
Posts: 59
Try to use the same Cipher-Object for encrypting and decrypting. Just initialize it new in the different methods. This worked for me.
Monoj Barma
Greenhorn

Joined: Oct 20, 2005
Posts: 9
If you don't mind, could you please show some code as an example?
thanks for the reply!
Frank Ertl
Ranch Hand

Joined: Apr 25, 2005
Posts: 59
In your members you should have only



and in your constructor



and then in encrypt()



and in decrypt()

Monoj Barma
Greenhorn

Joined: Oct 20, 2005
Posts: 9
Thanks a lot Frank, i will try that!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Triple DES decryption
 
Similar Threads
password Encryption in WSAD environment
BadPaddingException
Encrypting & Decrypting an XML File using Base64Encoder & Base64Decoder
Characters jumbled pls help
IllegalBlockSizeException while decrypting using Triple DES