File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Engineering
»
Security
Author
BadPaddingException using DES
Aravind Ramanujam
Greenhorn
Joined: Dec 14, 2004
Posts: 15
posted
Oct 25, 2005 10:30:00
0
I'm getting
javax.crypto.BadPaddingException
: Given final block not properly padded
while trying to decrypt using DES. Here is the code snippet:
private static byte[] cleartext ; private static byte[] cipherText ; Cipher dcipher; Cipher ecipher; KeyGenerator keygen; SecretKey desKey; public static byte[] encryptCreditCard(String ccno){ try{ keygen = KeyGenerator.getInstance("DES"); desKey = keygen.generateKey(); ecipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); ecipher.init(Cipher.ENCRYPT_MODE, desKey); cipherText= ecipher.doFinal(ccno.getBytes()); } catch (BadPaddingException e) { System.out.println("Caught BadPaddingException in encryptCreditCard method in CryptoTestclass " + e); } catch (IllegalBlockSizeException e) { System.out.println("Caught IllegalBlockSizeException in encryptCreditCard method in CryptoTestclass " + e); } catch (Exception e){ System.out.println("Caught Exception in encryptCreditCard method in CryptoTestclass " + e); } return cipherText; } public static byte[] decryptCreditCard(String encryptedCardNum){ try{ keygen= KeyGenerator.getInstance("DES"); desKey = keygen.generateKey(); dcipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); dcipher.init(Cipher.DECRYPT_MODE, desKey); cleartext = dcipher.doFinal(encryptedCardNum.getBytes()); } catch (BadPaddingException e) { System.out.println("Caught BadPaddingException in decryptCreditCard method in CryptoTestclass " + e); } catch (IllegalBlockSizeException e) { System.out.println("Caught IllegalBlockSizeException in decryptCreditCard method in CryptoTestclass " + e); } catch (Exception e){ System.out.println("Caught Exception in decryptCreditCard method in CryptoTestclass " + e); } return cleartext; }
Please let me know if i'm doing anything wrong here. Any help is appreciated
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35253
7
posted
Oct 25, 2005 12:29:00
0
A quick search through the forum would have uncovered
this thread
. Let us know of if that's the problem.
Android apps
–
ImageJ plugins
–
Java web charts
I agree. Here's the link:
http://ej-technologies/jprofiler
- if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
subject: BadPaddingException using DES
Similar Threads
Encrypt data before storing database
Cryptography - deciphering doesn't yield the same text
error coming while decrypting...
Exception in thread "main" java.security.InvalidKeyException:
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter