Skip to Content

Requirement:- We had a requirement where we have to encrypt a single field in message mapping using a key and cipher. The key is 128 bit key which willb be hard coded and same key we have used to decrypt the encrypted text.

Solution:- As a solution for this problem, we have written a java code using Base64 algorithm.

Libraries that we have included were:-

Libraries.PNG

Base64Encoder and Base64decoder classes are used to encode and decode the field.

Code for Encryption:-

public String Encryption(String CreditCard_No, Container container) throws StreamTransformationException{

String key = “############”; // 128 bit key

BASE64Encoder encoder = new BASE64Encoder();

String encoded=””;

      try {

         // Create key and cipher

         Key aesKey = new SecretKeySpec(key.getBytes(), “AES”);

         Cipher cipher = Cipher.getInstance(“AES”);

         // encrypt the CreditCard_No

         cipher.init(Cipher.ENCRYPT_MODE, aesKey);

         byte[] encrypted = cipher.doFinal(CreditCard_No.getBytes());

      

       encoded= encoder.encode(encrypted);

          /return encrypted text

     

  return encoded;

     

      }

  catch(Exception e) {

          e.printStackTrace();

  return “error during encryption”+e.toString();

    }

}

Code for Decryption:-

String Encryption(String encryptedValue, Container container) throws StreamTransformationException{

String key = “############”; // any 128 bit key can be given

BASE64Decoder decoder = new BASE64Decoder();

      try {

         // Create key and cipher

         Key aesKey = new SecretKeySpec(key.getBytes(), “AES”);

         Cipher cipher = Cipher.getInstance(“AES”);

         // decrypt the text

         cipher.init(Cipher.DECRYPT_MODE, aesKey);

         byte[] encryptedbyte= decoder.decodeBuffer(encryptedValue);

         byte[] decryptedValue=cipher.doFinal(encryptedbyte);

  //Converting the input Strings to bytes so that it can be decrypted

         String decrypted = new String(decryptedValue,”UTF-8″);

  return decrypted;

      }

catch(Exception e) {

         e.printStackTrace();

  return “error during decryption” + e.toString();

      }

}

Mapping output:-

Encryption-

encrypt.PNG

Decryption:-

Decrypt.PNG

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

    1. shankul saxena Post author

      Thanks Eng for this information. We are working on PI 7.11 and getting package doesn’t exist error. Soon we are upgrading to PO 7.5 and I hope DatatypeConverter will be available there.

      (0) 

Leave a Reply