on 02-27-2023 6:49 AM
Hi Experts,
I am using following Groovy script in Integration Suite to encrypt the payload but getting numeric output in bytes.
Could you please advise or review the code ?
def Message processData(Message message) {
def body = message.getBody(String);
String ALGORITHM = "AES/CBC/PKCS5Padding";
String key ="86dh577a8h0a402eed195f7f1798850d";
Cipher cipher = Cipher.getInstance(ALGORITHM);
byte[] messageArr = body.getBytes();
byte[] keyparam=key.getBytes();
SecretKeySpec keySpec = new SecretKeySpec(keyparam, "AES");
byte[] ivParams = new byte[16];
byte[] encoded = new byte[messageArr.length + 16];
System.arraycopy(ivParams,0,encoded,0,16);
System.arraycopy(messageArr, 0, encoded, 16, messageArr.length);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(ivParams));
byte[] encryptedBytes = cipher.doFinal(encoded);
encryptedB = Base64.getEncoder().encode(encryptedBytes);
def encryptString = encryptedB.toString();
message.setBody(encryptString);
return message;
Dear Arvik,
If you want the output as a Base64 encoded string then I would say use "encodeToString" method rather than "encode" method.
import com.sap.gateway.ip.core.customdev.util.Message
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec;
def Message processData(Message message) {
def body = message.getBody(String);
String ALGORITHM = "AES/CBC/PKCS5Padding";
String key ="86dh577a8h0a402eed195f7f1798850d";
Cipher cipher = Cipher.getInstance(ALGORITHM);
byte[] messageArr = body.getBytes();
byte[] keyparam=key.getBytes();
SecretKeySpec keySpec = new SecretKeySpec(keyparam, "AES");
byte[] ivParams = new byte[16];
byte[] encoded = new byte[messageArr.length + 16];
System.arraycopy(ivParams,0,encoded,0,16);
System.arraycopy(messageArr, 0, encoded, 16, messageArr.length);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(ivParams));
byte[] encryptedBytes = cipher.doFinal(encoded);
encryptedB = Base64.getEncoder().encodeToString(encryptedBytes);
//def encryptString = encryptedB.toString();
message.setBody(encryptedB);
return message;
}
Thanks,
Asutosh
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
92 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.