Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Security
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Spring Boot in Practice
this week in the
Spring
forum!
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
Tim Cooke
Ron McLeod
Jeanne Boyarsky
Paul Clapham
Sheriffs:
Liutauras Vilda
Henry Wong
Devaka Cooray
Saloon Keepers:
Tim Moores
Stephan van Hulst
Tim Holloway
Al Hobbs
Carey Brown
Bartenders:
Piet Souris
Mikalai Zaikin
Himai Minh
Forum:
Security
BadPaddingException using DES
Aravind Ramanujam
Greenhorn
Posts: 16
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
Rancher
Posts: 43028
76
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
A quick search through the forum would have uncovered
this thread
. Let us know of if that's the problem.
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Cryptography - deciphering doesn't yield the same text
Encrypt data before storing database
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8
error coming while decrypting...
Exception in thread "main" java.security.InvalidKeyException:
More...