Created a class for encrypting and decrypting values. It's throwing an error about the 'pad block corrupted' being incomplete.
This works for me:
Blob ckey = EncodingUtil.base64Decode(Encryption_Key__c.getOrgDefaults().key__c);
Blob encryptedUserName = Crypto.encryptWithManagedIV('AES256', ckey,
blob.valueOf('[email protected]'));
String decryptedUserName = Crypto.decryptWIthManagedIV('AES256',ckey, encryptedUserName).toString();
system.debug(decryptedUsername);
Running that in execute anonymous outputs the original string value I encrypted.
However if I run this it fails with the given error:
secretDecoderRing sdr = secretDecoderRing.getInstance();
Blob ckey = EncodingUtil.base64Decode(Encryption_Key__c.getOrgDefaults().key__c);
Blob encryptedUserName = Crypto.encryptWithManagedIV('AES256', ckey, blob.valueOf('[email protected]'));
String decryptedUserName = sdr.decryptValue(EncodingUtil.base64Encode(encryptedUserName));
Here's my secret decoder ring code:
public class secretDecoderRing {
private final Encryption_Key__c myKey = Encryption_Key__c.getOrgDefaults();
public final Blob key;
private static secretDecoderRing instance = null;
private secretDecoderRing(){
key = EncodingUtil.base64Decode(myKey.key__c);
}
public static secretDecoderRing getInstance(){
if(instance == null) instance = new secretDecoderRing();
return instance;
}
public String encryptValue(String cleanData){
encryptionWrapper ew = new encryptionWrapper(Blob.valueOf(cleanData), key);
return ew.encryptedValue.toString();
}
public String decryptValue(String encryptedData){
decryptionWrapper dw = new decryptionWrapper(EncodingUtil.base64Decode(encryptedData), key);
return dw.decryptedValue;
}
private class encryptionWrapper{
Blob encryptedValue{get; private set;}
public encryptionWrapper(Blob cleanData, Blob key){
this.encryptedValue = Crypto.encryptWithManagedIV('AES256',key, cleanData);
}
}
private class decryptionWrapper{
String decryptedValue{get;private set;}
private decryptionWrapper(Blob encryptedData, Blob key){
this.decryptedValue = Crypto.decryptWithManagedIV('AES256',key, encryptedData).toString();
}
}
}
What does "pad block corrupted" even mean, and is there something I need to adjust with my decoder ring class or is what I'm trying to do here just not possible?
key__c
?