Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
shankul_saxena
Explorer
0 Kudos

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:-

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-

Decryption:-

2 Comments
Labels in this area