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
Eclipse Collections Categorically: Level up your programming game
this week in the
Open Source Projects
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
paul wheaton
Ron McLeod
Jeanne Boyarsky
Sheriffs:
Paul Clapham
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Security
javax.crypto.BadPaddingException
Arpit Panwar
Greenhorn
Posts: 12
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
package Encryption; import java.io.*; import java.security.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.crypto.Cipher.*; import javax.crypto.*; import javax.crypto.spec.*; import sun.security.pkcs.PKCS10; interface AESAlgorithm { public String encrypt(byte[] encrypted); public String decrpyt(byte[] encrypted); } public class EncMain implements AESAlgorithm { HexPass h=new HexPass(); GenKey g=new GenKey(); Cipher cipher; SecretKeySpec skeySpec1 = null; static String message = "qwsderfgvcbnhjmk"; public static void main(String[] args){ EncMain m = new EncMain(); byte[] btt = message.getBytes(); System.out.println("Encryptd string:" + m.encrypt(btt)); System.out.println("Decryptd string:" + m.decrpyt(btt)); } public String encrypt(byte[] encrypted) { try { cipher = Cipher.getInstance("AES/CBC/PKCS8Padding"); skeySpec1=g.Gen(); byte[] rawBytes = {16, 32, 64, 32, 48, 80, 96, 112, 96, 32, 48, 80, 96, 112, 64,16}; IvParameterSpec iv = new IvParameterSpec(rawBytes); cipher.init(Cipher.ENCRYPT_MODE, skeySpec1, iv); byte[] encrypt = cipher.doFinal(encrypted); return h.asHex(encrypt); } catch (Exception ex) { return "their is an error"; } } public String decrpyt(byte[] encrypted) { String ostring = null; try { //SecretKeySpec skeySpec = null; skeySpec1 =g.Gen(); byte[] rawBytes = {16, 32, 64, 32, 48, 80, 96, 112, 96, 32, 48, 80, 96, 112, 64,16}; IvParameterSpec iv = new IvParameterSpec(rawBytes); System.out.println(iv); cipher.init(Cipher.DECRYPT_MODE, skeySpec1, iv); byte[] original = cipher.doFinal(encrypted); ostring = new String(original); return ostring; } catch (IllegalBlockSizeException ex) { ex.printStackTrace(); return "error1"; } catch (Exception e) { e.printStackTrace(); return "errro2"; } } }
i keep getting badpading exception.
can anyone help me out on how to pad the message bits.
SCJP 75%
Ulf Dittmer
Rancher
Posts: 43081
77
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
This exception usually happens when encrypted data, or an encryption key, is treated like character data (in other words, as a
string
). Here's a full example of AES encryption/decryption:
http://java.sun.com/developer/technicalArticles/Security/AES/AES_v1.html
Could you hold this kitten for a sec? I need to adjust this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
encoding in java and decoding in perl
BadPaddingException
Encrypted values all contain same final characters
Java Cryptography
AES Encryption Service
More...