Java
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESEncDec {
public static String encryptCallBack(String key, String str_resp)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(key.length() / 2);
for (int i = 0; i < key.length(); i += 2) {
String output = key.substring(i, i + 2);
int decimal = Integer.parseInt(output, 16);
baos.write(decimal);
}
try {
SecretKeySpec skeySpec = new SecretKeySpec(baos.toByteArray(), "AES");
byte [] iv1 = new byte [] {(byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A, (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07,0x72, 0x6F, 0x5A};
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv1);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(1, skeySpec,paramSpec);
byte[] encrypted = cipher.doFinal(str_resp.getBytes("UTF-8"));
ByteArrayOutputStream os = new ByteArrayOutputStream();
os.write(iv1);
os.write(encrypted);
byte[] encryptedWithIV = os.toByteArray();
//return new String(Base64.encode(os.toByteArray()));
String encryptedResult = Base64.getEncoder().encodeToString(encryptedWithIV);
return encryptedResult;
}
catch (Exception ex)
{
ex.printStackTrace();
}
return null;
}
public static String decryptCallBack(String key, String encrypted)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(key.length() / 2);
for (int i = 0; i < key.length(); i += 2)
{
String output = key.substring(i, i + 2);
int decimal = Integer.parseInt(output, 16);
baos.write(decimal);
}
try
{
SecretKeySpec skeySpec = new SecretKeySpec(baos.toByteArray(), "AES");
//byte[] encryptedIVandTextAsBytes = Base64.decode(encrypted);
byte[] encryptedIVandTextAsBytes = Base64.getDecoder().decode(encrypted);
byte[] iv = Arrays.copyOf(encryptedIVandTextAsBytes, 16);
byte[] ciphertextByte = Arrays.copyOfRange(encryptedIVandTextAsBytes, 16, encryptedIVandTextAsBytes.length);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(2, skeySpec, new IvParameterSpec(iv));
byte[] decryptedTextBytes = cipher.doFinal(ciphertextByte);
String original = new String(decryptedTextBytes, "UTF-8");
return original;
}
catch (Exception ex)
{
ex.printStackTrace();
}
return null;
}
public static void main(String args[]) {
AESEncDec m = new AESEncDec();
final String keyAsHexString = "46C1EB633ECAB0CA0F52*****E92EA72";
String plainText = "{ \"mobileNumber\": \"483249c1d0e10d0762ff0ec55365a0f79e*****2f1f1ab08f2b2c2a70b7aaa3b\" }";
String encrptedString = m.encryptCallBack(keyAsHexString,plainText);
System.out.println("Encrypted data = " + encrptedString);
String decrypt = m.decryptCallBack(keyAsHexString,encrptedString);
System.out.println("Decrypted data = " + decrypt);
}
}