Nwn
Nwn

Reputation: 571

Decrypting returns, javax.crypto.BadPaddingException: Given final block not properly padded

Im trying to decrypt the encrypted xml file. Im getting it as a inputstream as follows.I have the correct encrypt key. but each time my program returns empty string. Every time i enter the correct key. but each time it returns Badpadding Exception.

   try{
                InputStream is = new ByteArrayInputStream(decryption.getFileData().getBytes());
                String xmlEncryptedStr = getStringFromInputStream(is);
               String xmlStr = CipherUtils.decrypt(xmlEncryptedStr, new  Long(key));
               .......

here is my CipherUtils.java class

.........

     public static String decrypt(String strToDecrypt,Long key)
        {
            String keyString=String.format("%016d", key);
            //System.out.println("decrypt keyString :"+keyString);
            return decrypt(strToDecrypt, keyString.getBytes());
        }


        public static String decrypt(String strToDecrypt,byte[] key)
        {
            if(strToDecrypt==null)
                return strToDecrypt;
            try
            {
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
                cipher.init(Cipher.DECRYPT_MODE, secretKey);

                final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
                System.out.println("CipherUtils.decryptedString :"+decryptedString);
                return decryptedString;
            }
            catch (Exception e)
            {
                log.error("Ops!", e);
            }
            return null;
        }

.......

For more information here is my encrypting code

   public static String encrypt(String strToEncrypt,Long key)
        {
            String keyString=String.format("%016d", key);
            //System.out.println("encrypt keyString :"+keyString);
            return encrypt(strToEncrypt,keyString.getBytes());
        }



        public static String encrypt(String strToEncrypt,byte[] key)
        {
            if(strToEncrypt==null)
                return strToEncrypt;
            try
            {
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
                cipher.init(Cipher.ENCRYPT_MODE, secretKey);
                final String encryptedString = Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes()));
              //  System.out.println("CipherUtils.encrypt :"+encryptedString);
                return encryptedString;
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;

        }

Upvotes: 0

Views: 1129

Answers (1)

Sagar Kharab
Sagar Kharab

Reputation: 369

I am sorry I couldn't comment so I am writing in answers section. I faced this issue when I was using different keys though I was passing the same but i used CBC methodology.

Just to note that have you checked that encryption is also done by the AES/ECB/PKCS5Padding and not other format like AES/CBC/PKCS5Padding

Also check if key format for encryption is also having the same format like %016d of your keyValue. Also the key is 16 char long.

I created a simple AES and DESede encryption utility and it worked fine.

private static final byte[] keyValue = new String(
        "CjxI&S@V&#DSA_S0dA-SDSA$").getBytes();

public static void main(String[] args) throws Exception {
    Client cli = new Client();
    System.out.println(cli.encrypt("your password for encryption"));

    Client cli1 = new Client();
    System.out.println(cli1.decrypt("fTsgVQtXvv49GynHazT4OGZ4Va1H57d+6AM+44Ex040="));
}
public String encrypt(String Data) throws Exception {
    Key key = new SecretKeySpec(keyValue, "AES");
    Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes());
    String encryptedValue = DatatypeConverter.printBase64Binary(encVal);
    // String encryptedValue = new BASE64Encoder().encode(encVal);
    return encryptedValue;
}

public String decrypt(String encryptedData) throws Exception {
    Key key = new SecretKeySpec(keyValue, "AES");
    Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = DatatypeConverter
            .parseBase64Binary(encryptedData);
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

Upvotes: 0

Related Questions