Question
Self
AU
Last activity: 16 Jun 2021 7:01 EDT
Unable to Encrypt Data using AES 256 and Key
I have a requirement to encrypt the data before generating the text file and transfer this text file to downstream application for decrypting and processing the data.
Below are few options tried:
I am able to encrypt the file using OOTB method but destination nation system is not able to decrypt using the jks used to encrypt.
I wrote custom java code by passing Key and String to extract. Custom code is working fine in Eclipse both encryption and decryption. But when using inside function, Receiving illegalblocksizeexception .
Steps to reproduce the issue
1. Create a function with custom code with params (string to encrypt and key)
2. Call the function in DT
3. Trigger call to DT and throws exception
@Arun.Kothapalli Below is the function code used to encrypt the string.
String inputStr=StrMessage;
String keyFile=PubKEYFile;
SecureRandom rand = new SecureRandom();
byte[] salt = new byte[32];
rand.nextBytes(salt);
int iterationCount = rand.nextInt(2048);
@Arun.Kothapalli Below is the function code used to encrypt the string.
String inputStr=StrMessage;
String keyFile=PubKEYFile;
SecureRandom rand = new SecureRandom();
byte[] salt = new byte[32];
rand.nextBytes(salt);
int iterationCount = rand.nextInt(2048);
try {
byte[] iv = new byte[32];
IvParameterSpec ivspec = new IvParameterSpec(iv);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(PubKEYFile.toCharArray(), salt, iterationCount, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
return Base64.getEncoder().encodeToString(cipher.doFinal(inputStr.getBytes(StandardCharsets.UTF_8)));
}
catch (Exception e) {
oLog.infoForced("Unable to encrypt");
}
return "Unable to Encrypt";