AnsweredAssumed Answered

AES-128 ECB decryption issue

Question asked by kuldeep.chauhan.ext584665 on Aug 23, 2018

Hi All,

 

We have a requirement to decrypt a token value in AES-128 ECB.

We are receiving AES-128 ECB encrypted token value. We have to decrypt it in AES-128 ECB and pass it to next process call. We are also using the secret key to decrypt it. While doing this, getting below error:-

 

Error:-

1-Error executing data process; Caused by: Given final block not properly padded (in groovy script); Caused by: Given final block not properly padded.

@

2-Embedded message: Error executing data process; Caused by: Input length must be multiple of 16 when decrypting with padded cipher (in groovy script); Caused by: Input length must be multiple of 16 when decrypting with padded cipher.

 

PFB groovy script:-

 

import java.util.Properties;
import java.io.InputStream;
import java.util.zip.* ;
import com.boomi.execution.ExecutionUtil;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

for( int i = 0; i < dataContext.getDataCount(); i++ ) {
InputStream is = dataContext.getStream(i);
Properties props = dataContext.getProperties(i);
String strToDecrypt = ExecutionUtil.getDynamicProcessProperty("encrytpedDataValue");
String decryptedKey = ExecutionUtil.getDynamicProcessProperty("keyValue");
//String strToEncrypt="7f5j7a5tpLFIvhOBTfOYIouoz7HOWbOERpMohRx8EPVgqm0zYbN3b9rGLqcG8qIJ8OMJjitmPtd2NLLIdgoBcg==";
//String keySign="ce-8f5e-215119fa7dd621DLMRHRLH2S";
MessageDigest sha = null;
byte[] key = decryptedKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16); // use only first 128 bit
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
//cipher.init(Cipher.ENCRYPT_MODE, secretKey);
//String encrytpedData=Base64.encodeBase64String(cipher.doFinal(strToDecrypt.getBytes("UTF-8")));
//ExecutionUtil.setDynamicProcessProperty("value1", encrytpedData, false);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
String decrytpedDataValue=new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt.trim())));
ExecutionUtil.setDynamicProcessProperty("decrytpedDataValue", decrytpedDataValue, false);
dataContext.storeStream(is, props);
}

 

Please let me know if anybody faced this issue earlier.

Thanks,

Kuldeep.

Outcomes