Anda di halaman 1dari 4

2/4/2019 SAP PI AES encryption in Message Mapping – Jaehoo Weblog

Jaehoo Weblog

Blog personal de jaehoo

SAP PI AES encryption in Message Mapping


enero 17, 2019enero 17, 2019

I developed a simple UDF (User Defined Function) in Java to crypt  and decrypt some text data in my
message mapping, first into section ‘Attributes and Methods‘ I setted static constants for AES and I used
a method to “normalize” the string text for crypt and decrypt with the method fixedLengthString.

I don’t know why but if I didn’t this the decryption does not work, the method only get the first 16
characters and apply a String format.

This is the code:

1 public static String SEC_K_SPEC="AES";
2 public static String CIPER_S="AES/ECB/PKCS5Padding";
3  
4 public static String fixedLengthString(String string) {
5 return String.format("%1$16s", string).substring(0,16);
6 }

And this are the functions , each one require a key to crypt or decrypt the text value, the rest of the code
is explained by self:

https://jaehoo.wordpress.com/2019/01/17/sap-pi-aes-encryption-in-message-mapping/ 1/4
2/4/2019 SAP PI AES encryption in Message Mapping – Jaehoo Weblog

Crypt function:

1 public String cryptAES(String keytext, String valueToCryp, Container co
2  
3 byte[] key = new byte[16];
4 String outKey=fixedLengthString(keytext);
5  
6 key =outKey.getBytes();
7  
8 BASE64Encoder encoder = new BASE64Encoder();
9  
10 String encoded="";
11  
12 try {
13 // Create key and cipher
14 Key aesKey = new SecretKeySpec(key, SEC_K_SPEC);
15 Cipher cipher = Cipher.getInstance(CIPER_S);
16  
17 // encrypt the CreditCard_No
18 cipher.init(Cipher.ENCRYPT_MODE, aesKey);
19 byte[] encrypted = cipher.doFinal(valueToCryp.getBytes());
20  
21 encoded= encoder.encode(encrypted);
22 return encoded;
23 }
24  
25 catch(Exception e) {
26  
27 e.printStackTrace();
28  
29 return "error during encryption"+e.toString();
30  
31 }
32  
33 }

Decrypt Function:

https://jaehoo.wordpress.com/2019/01/17/sap-pi-aes-encryption-in-message-mapping/ 2/4
2/4/2019 SAP PI AES encryption in Message Mapping – Jaehoo Weblog
1 public String decryptAES(String keytext, String encryptedValue, Contain
2  
3 byte[] key = new byte[16];
4 String outKey=fixedLengthString(keytext);
5  
6 key =outKey.getBytes();
7  
8 BASE64Decoder decoder = new BASE64Decoder();
9  
10 try {
11 // Create key and cipher
12 Key aesKey = new SecretKeySpec(key, SEC_K_SPEC);
13 Cipher cipher = Cipher.getInstance(CIPER_S);
14  
15 // decrypt the text
16 cipher.init(Cipher.DECRYPT_MODE, aesKey);
17  
18 byte[] encryptedbyte= decoder.decodeBuffer(encryptedValue);
19 byte[] decryptedValue=cipher.doFinal(encryptedbyte);
20  
21 //Converting the input Strings to bytes so that it can be decrypted
22  
23 String decrypted = new String(decryptedValue,"UTF­8");
24  
25 return decrypted;
26  
27 }
28  
29 catch(Exception e) {
30  
31 e.printStackTrace();
32  
33 return "error during decryption" + e.toString();
34  
35 }
36  
37 }

Now to use it, drag the functions into message mapping editor, for example to crypt with this  data
“1234567890123456” with key “test” the result is
“va4IRNUgVL3DbF+j5yCGCHEvnmjmfpMe58F8YhHGFhE=”

https://jaehoo.wordpress.com/2019/01/17/sap-pi-aes-encryption-in-message-mapping/ 3/4
2/4/2019 SAP PI AES encryption in Message Mapping – Jaehoo Weblog

And to recover the original value, the crypted data and the key are going used with Decrypt function:

And that’s all, the original value is recovered.

Cheers

Publicado en: Development, Java, SAP, SAP PI | Etiquetado: Java, SAP PI, Security
This site uses Akismet to reduce spam. Learn how your comment data is processed.

CREA UN BLOG O UN SITIO WEB GRATUITOS CON WORDPRESS.COM.

https://jaehoo.wordpress.com/2019/01/17/sap-pi-aes-encryption-in-message-mapping/ 4/4

Anda mungkin juga menyukai