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
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
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Security
javax.crypto.BadPaddingException
Arpit Panwar
Greenhorn
Posts: 12
posted 13 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 13 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
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...