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
Kanth kKanth k 

FATAL_ERROR|System.SecurityException: pad block corrupted

Hi All,
I am trying to decrypt data in csv file and got error is "FATAL_ERROR|System.SecurityException: pad block corrupted". Can anybody send code to solve issue. I execute below code in annonymous block
list<attachment> atlst =new list<attachment>();

        
        for(attachment at:[select id,body,name,parentId from attachment where parentid='a010o000022leDD' limit 1])
        {
            
        
Blob cryptoKey = Crypto.generateAesKey(256);
       
        Blob data = (at.body);
            
            Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey, data);
             String decryptedClearText = decryptedData.toString();
        
        Attachment atEncrypt=new attachment();
        atEncrypt.name=at.name+'__Decrypted22.csv';
        atEncrypt.body=blob.valueof(decryptedClearText);
        atEncrypt.parentId=at.parentId;
atlst.add(atEncrypt) ;
        }
if(atlst.size()>0)
{
insert atlst;
}

 
Deepali KulshresthaDeepali Kulshrestha
Hi Kanth,
Greetings to you!

- While I cannot reproduce your issue given the code in your post, I have been able to reproduce it. It appears to me that your base64 ciphertext is being truncated at some point during your process. Here's an example:

secretDecoderRing sdr = secretDecoderRing.getInstance();                            
Blob ckey = EncodingUtil.base64Decode(Encryption_Key__c.getOrgDefaults().key__c);
Blob encryptedUserName = Crypto.encryptWithManagedIV('AES256', ckey, blob.valueOf('adamnila@justcheckingstuff.com'));
String ciphertext = EncodingUtil.base64Encode(encryptedUserName);
String decryptedUserName = sdr.decryptValue(ciphertext.substring(0, ciphertext.length() - 1));
System.debug(decryptedUserName);
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Kanth kKanth k
Hi Deepali,

  I executed code in annonymous block what you sent but it throws error is "Invalid type: secretDecoderRing"