• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Triple DES decryption

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you don't mind, could you please show some code as an example?
thanks for the reply!
 
Frank Ertl
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your members you should have only



and in your constructor



and then in encrypt()



and in decrypt()

 
Monoj Barma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Frank, i will try that!
 
I was her plaything! And so was this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic