cancel
Showing results for 
Search instead for 
Did you mean: 

Help needed with Groovy !

das_sappo
Participant
0 Kudos

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;

Accepted Solutions (1)

Accepted Solutions (1)

asutoshmaharana2326
Active Participant
0 Kudos

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

das_sappo
Participant
0 Kudos

Thanks mate .

Answers (0)