Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

javax.crypto.BadPaddingException: Given final block not properly padded java callout apigee

Not applicable
package com.tmobile;
import com.apigee.flow.execution.ExecutionContext;
import com.apigee.flow.execution.ExecutionResult;
import com.apigee.flow.execution.spi.Execution;
import com.apigee.flow.message.MessageContext;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
public class DecryptionDocumentId2 implements Execution {
    private static final String _varPrefix = "decrypt_";
    private static final String varName(String s) { return _varPrefix + s; }
    @Override
    public ExecutionResult execute(MessageContext messageContext, ExecutionContext executionContext) {
        try {
            // decode the base64 encoded string
            String decryptionKey1 = messageContext.getVariable("dk1_decryption");
            //String decryptionKey2 = messageContext.getVariable("dk2_decryption");
            byte[] key1 = decryptionKey1.getBytes("UTF-8");
            //byte[] key2 = decryptionKey2.getBytes("UTF-8");
            SecretKey secretKey1 = new SecretKeySpec(key1, 0, key1.length, "AES");
            // SecretKey secretKey2 = new SecretKeySpec(key2, 0, key2.length, "AES");
            String documentId = messageContext.getVariable("documentId");
            if (documentId == null) {
                messageContext.setVariable(varName("reason"), "missing documentId");
                return ExecutionResult.ABORT;
            }
            String decryptedValue = decrypt(documentId.trim(),secretKey1,messageContext);
            messageContext.setVariable("decryptedMsisdn", decryptedValue);
            return ExecutionResult.SUCCESS;
        }
        catch (Exception e) {
            String error = e.toString();
            messageContext.setVariable(varName("error"), error);
            int ch = error.lastIndexOf(':');
            if (ch >= 0) {
                messageContext.setVariable(varName("reason"), error.substring(ch+2));
            }
            else {
                messageContext.setVariable(varName("reason"), error);
            }
            return ExecutionResult.ABORT;
        }
    }
    private String decrypt(String encryptedText, SecretKey secretKey, MessageContext messageContext)
        throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedTextBytes = null;
        try {
            decryptedTextBytes = cipher.doFinal(DatatypeConverter.parseBase64Binary(encryptedText));
        }
        catch (IllegalBlockSizeException e) {
            messageContext.setVariable(varName("reason1"), e.toString());
            throw e;
        }
        catch (BadPaddingException e) {
            messageContext.setVariable(varName("reason2"), e.toString());
            throw e;
        }
        return new String(decryptedTextBytes);
    }
}

This is a java code for an Apigee Edge callout. During execution, while trying to decrypt encrypted data in APIGEE , we are getting error of final block not properly padded.

The code we started with is:

http://aesencryption.net/#Java-aes-encryption-example

We generate encrypted data from there and are trying to decrypt it with a key.

0 2 46.7K
2 REPLIES 2