function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
karthik karthikkarthik 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.
 
@Karanraj@Karanraj
The key should be same when you are decrypting the encryted vaule. While encrypting you have generated the key Blob cryptoKey = Crypto.generateAesKey(256); but when you are decrypting again you are genrating the new key , that's the reason you are getting the error message. Try simple code belwo

Code with different key
Blob cryptoKey = Crypto.generateAesKey(256);
Blob dataenc = blob.valueOf('Hello');
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey , dataenc );
String b64Data = EncodingUtil.base64Encode(encryptedData);
System.debug('Encrypted' + b64Data);            
 
Blob cryptoKey1 = Crypto.generateAesKey(256);
Blob data = EncodingUtil.base64Decode(b64Data);
Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey1 , data);
System.debug('Decrypted'+ decryptedData.toString());

Code with same key for both encryption and decryption value
Blob cryptoKey = Crypto.generateAesKey(256);
Blob dataenc = blob.valueOf('Hello');
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey , dataenc );
String b64Data = EncodingUtil.base64Encode(encryptedData);
System.debug('Encrypted' + b64Data);            
 
Blob data = EncodingUtil.base64Decode(b64Data);
Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey , data);
System.debug('Decrypted'+ decryptedData.toString());


 
Sujal Gupta 10Sujal Gupta 10
Adding to @Karanraj point you should not be using following line of code
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();
    }
    
}