You need to sign in to do that
Don't have an account?
karthik karthik
FATAL_ERROR|System.SecurityException: Given final block not properly padded
Hi,
I am trying to encrypt and decrypt the data, I got error while decrypt the data. Please find the code below.
Encrypt Code:
Blob cryptoKey = Crypto.generateAesKey(256);
Blob data = Blob.valueOf(opps.id);
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey , data );
String b64Data = EncodingUtil.base64Encode(encryptedData);
opps.EncryptedKey__c = b64Data ;
Decrypt Code :
Blob cryptoKey = Crypto.generateAesKey(256);
Blob data = EncodingUtil.base64Decode(opps.EncryptedKey__c);
Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey , data);
String dryptData = decryptedData.toString();
opps.Unecrypt__c = dryptData;
The above encrypt code is working fine, the decrypt code is not working i got the below error. Please i need urgent help.
Error :
FATAL_ERROR|System.SecurityException: Given final block not properly padded
Thanks in advance.
I am trying to encrypt and decrypt the data, I got error while decrypt the data. Please find the code below.
Encrypt Code:
Blob cryptoKey = Crypto.generateAesKey(256);
Blob data = Blob.valueOf(opps.id);
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey , data );
String b64Data = EncodingUtil.base64Encode(encryptedData);
opps.EncryptedKey__c = b64Data ;
Decrypt Code :
Blob cryptoKey = Crypto.generateAesKey(256);
Blob data = EncodingUtil.base64Decode(opps.EncryptedKey__c);
Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey , data);
String dryptData = decryptedData.toString();
opps.Unecrypt__c = dryptData;
The above encrypt code is working fine, the decrypt code is not working i got the below error. Please i need urgent help.
Error :
FATAL_ERROR|System.SecurityException: Given final block not properly padded
Thanks in advance.
Code with different key
Code with same key for both encryption and decryption value
Blob cryptoKey = Crypto.generateAesKey(256);
As it will generate a diffrent key each time you call this piece of code.
So, what I did to solve the issue is generated a one time key using Crypto.generateAesKey(256).
Here is my code
public class IDM_CrytoUtils {
private static final String ckey = 'vAJez/JqcmUEd0CAe2QQ+0YNSh+UV89a/4Usew0i7Gs=';
private static final Blob cryptoKey = EncodingUtil.base64Decode(ckey);
public static String getEncryptedData(String normalString){
if(String.isBlank(normalString)){
return '';
}
Blob data = Blob.valueOf(normalString);
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
return EncodingUtil.base64Encode(encryptedData);
}
public static string getDecryptedData(String encryptedString){
if(String.isBlank(encryptedString)){
return '';
}
Blob data = EncodingUtil.base64Decode(encryptedString);
Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey , data);
return decryptedData.toString();
}
}