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

  1. #1
    rohitpatidar3 is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default 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 04:34 AM. Reason: code tags added

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Considering AES is the replacement for DES... i would vote AES :)
    My Hobby Project: LegacyClone

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by mrmatt1111 View Post
    Considering AES is the replacement for DES... i would vote AES :)
    Modern Cryptography, I vote for AES too.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    rohitpatidar3 is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    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)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    This is what you misses,

    javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
    on line,

    at Encryption.EncMain.decrpyt(EncMain.java:56)

  7. #7
    rohitpatidar3 is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    yeah .....i got same message...
    pls post if u've solution

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Exceptin in PGP Encryption
    By InduM in forum New To Java
    Replies: 0
    Last Post: 11-02-2009, 12:26 PM
  2. Reg : PGP Encryption using Recipients Key
    By Deepa in forum New To Java
    Replies: 0
    Last Post: 08-31-2009, 01:28 PM
  3. Reg : Folder Encryption in PGP
    By Deepa in forum New To Java
    Replies: 0
    Last Post: 08-18-2009, 02:45 PM
  4. encryption
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 02-06-2008, 11:27 AM
  5. Encryption in java
    By Ed in forum Advanced Java
    Replies: 1
    Last Post: 07-09-2007, 02:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •