Can some one help me with what's wrong with this code .....
method used to generate key
================================================
public static SecretKey generateKey() throws NoSuchAlgorithmException
{
// Get a key generator for Triple DES (a.k.a DESede)
KeyGenerator keygen = KeyGenerator.getInstance("DESede");
// Use it to generate a key
return keygen.generateKey();
}
Method used for encryption
==========================
public static byte[] encrypt(final SecretKey key, final
String password)
throws NoSuchAlgorithmException, InvalidKeyException,
NoSuchPaddingException, IOException
{
try
{
Cipher cipher = Cipher.getInstance("DESEDE/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] passArr = password.getBytes("UTF-8");
return cipher.doFinal(passArr);
}
catch (Throwable t)
{
t.printStackTrace();
}
return null;
}
method used for decryption
=========================================
public static byte[] decrypt(final SecretKey key, final String encryptedPassword)
throws NoSuchAlgorithmException, InvalidKeyException, IOException,
IllegalBlockSizeException, NoSuchPaddingException,
BadPaddingException
{
try
{
Cipher cipher = Cipher.getInstance("DESEDE/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] bOut = cipher.doFinal(encryptedPassword.getBytes("UTF-8"));
return bOut;
}
catch(Exception ex)
{
ex.printStackTrace();
return null;
}
It encrypts the password and when i am trying to decrypt the same....i get following error
javax.crypto.BadPaddingException: Given final block not properly padded
at com.ibm.crypto.provider.DESedeCipher.engineDoFinal(Unknown Source)
at com.ibm.crypto.provider.DESedeCipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(Unknown Source)
at com.fmr.xtrac.commons.application.util.GenerateKey.decrypt(GenerateKey.java:146)
Can someone please point out what's wrong either in encryption method or decryption method ?