View Poll Results: CBC mode
- Voters
- 4. You may not vote on this poll
-
in AES
3 75.00% -
in DES
1 25.00%
Results 1 to 8 of 8
Thread: Aes Encryption
- 04-09-2010, 09:29 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
Aes Encryption
Can any one help me out???
Here is a code i got IllegalBlockSizeException in decrypt method
Java Code: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.*; public class EncMain { HexPass h = new HexPass(); GenKey g = new GenKey(); Cipher cipher; static String message = " AES algorithm implementation"; public static void main(String[] args) throws InvalidKeyException { try { EncMain m = new EncMain(); byte[] btt = message.getBytes(); System.out.println("Encrypted string-:" + m.encrypt(btt)); System.out.println("Decrypted string-:" + m.decrpyt(btt)); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(EncMain.class.getName()).log(Level.SEVERE, null, ex); } catch (InvalidAlgorithmParameterException ex) { Logger.getLogger(EncMain.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchPaddingException ex) { Logger.getLogger(EncMain.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedEncodingException ex) { Logger.getLogger(EncMain.class.getName()).log(Level.SEVERE, null, ex); } } public String encrypt(byte[] encrypted) { SecretKeySpec skeySpec1 = null; try { cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); skeySpec1 = g.Gen(); byte[] rawBytes = {66, -56, -63, -97, -23, -9, 27, -3, 63, -115, 85, 81, -31, 127, -48, -92}; IvParameterSpec iv = new IvParameterSpec(rawBytes, 0, rawBytes.length); 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) throws InvalidKeyException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchPaddingException, UnsupportedEncodingException { try { //here i got exception String ostring = null; SecretKeySpec skeySpec = null; skeySpec = g.Gen(); byte[] rawBytes = {66, -56, -63, -97, -23, -9, 27, -3, 63, -115, 85, 81, -31, 127, -48, -92}; IvParameterSpec iv = new IvParameterSpec(rawBytes, 0, rawBytes.length); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] original = cipher.doFinal(encrypted); ostring = new String(original); return ostring; } catch (IllegalBlockSizeException ex) { Logger.getLogger(EncMain.class.getName()).log(Level.SEVERE, null, ex); return "Their is an exception12"; } catch (BadPaddingException ex) { Logger.getLogger(EncMain.class.getName()).log(Level.SEVERE, null, ex); return "Their is an exception13"; } } } class HexPass { public String asHex(byte[] buffer) { StringBuffer strbuf = new StringBuffer(buffer.length * 2); int i; for (i = 0; i < buffer.length; i++) { if (((int) buffer[i] & 0xff) < 0x10) { strbuf.append("0"); } strbuf.append(Long.toString((int) buffer[i] & 0xff, 16)); } return strbuf.toString(); } } class GenKey { public static byte[] passToKey(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException { MessageDigest md = null; try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException ex) { return new byte[0]; } byte[] passwordBytes = null; try { passwordBytes = password.getBytes("ISO-8859-1"); } catch (UnsupportedEncodingException ex) { passwordBytes = new byte[0]; } return md.digest(passwordBytes); } public SecretKeySpec Gen() throws NoSuchAlgorithmException, UnsupportedEncodingException { String password = "437829ADGR83G88D"; byte[] rawKey = passToKey(password); SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES"); return skeySpec; } }Last edited by Eranga; 04-10-2010 at 03:34 AM. Reason: code tags added
- 04-09-2010, 10:49 PM #2
- 04-10-2010, 03:32 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-10-2010, 03:35 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
@OP, Can you post the complete error message here to see?
- 04-10-2010, 09:58 AM #5
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
Encryptd string:d93953141f1edf506fa78480562dbefa154e6b62e8f 00479f5cebed70c6cc423
javax.crypto.spec.IvParameterSpec@1eec612
Apr 10, 2010 1:52:04 PM Encryption.EncMain decrpyt
Decryptd string:error1
SEVERE: null
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.AESCipher.engineDoFinal(Da shoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at Encryption.EncMain.decrpyt(EncMain.java:56)
at Encryption.EncMain.main(EncMain.java:28)
- 04-10-2010, 05:33 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
This is what you misses,
on line,javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at Encryption.EncMain.decrpyt(EncMain.java:56)
- 04-10-2010, 08:39 PM #7
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
yeah .....i got same message...
pls post if u've solution
- 04-12-2010, 04:16 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Check the input length lol. Simply just put a print line and check the value.
Similar Threads
-
Exceptin in PGP Encryption
By InduM in forum New To JavaReplies: 0Last Post: 11-02-2009, 11:26 AM -
Reg : PGP Encryption using Recipients Key
By Deepa in forum New To JavaReplies: 0Last Post: 08-31-2009, 12:28 PM -
Reg : Folder Encryption in PGP
By Deepa in forum New To JavaReplies: 0Last Post: 08-18-2009, 01:45 PM -
encryption
By Joe2003 in forum Advanced JavaReplies: 2Last Post: 02-06-2008, 10:27 AM -
Encryption in java
By Ed in forum Advanced JavaReplies: 1Last Post: 07-09-2007, 01:47 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks