Quantcast
Channel: SCN : Document List - Process Integration (PI) & SOA Middleware
Viewing all articles
Browse latest Browse all 571

To Encrypt and Decrypt a Single Field in Message Mapping

$
0
0

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


Viewing all articles
Browse latest Browse all 571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>