fork download
  1. public class EncryptionUtil {
  2. private static final String ALGO = 'AES-GCM';
  3. private static final Integer IV_LENGTH = 12; // Recommended length for GCM
  4. private static final Integer KEY_LENGTH = 32; // 256 bits
  5.  
  6. public class EncryptionResult {
  7. public String encryptedTextWithTag;
  8. public String keyiv;
  9.  
  10. public EncryptionResult(String encryptedTextWithTag, String keyiv) {
  11. this.encryptedTextWithTag = encryptedTextWithTag;
  12. this.keyiv = keyiv;
  13. }
  14. }
  15.  
  16. /**
  17. * Encrypts plaintext using AES-256-GCM algorithm
  18. * @param plaintext The text to encrypt
  19. * @return EncryptionResult containing encrypted text and key+iv
  20. */
  21. public static EncryptionResult encryptAES256(String plaintext) {
  22. try {
  23. // Generate random key and IV
  24. Blob key = Crypto.generateAesKey(256);
  25. Blob iv = Crypto.generateRandomIV();
  26.  
  27. // Encrypt the plaintext
  28. Blob plaintextBlob = Blob.valueOf(plaintext);
  29. Blob encryptedBlob = Crypto.encryptWithManagedIV('AES256', key, plaintextBlob);
  30.  
  31. // Convert to base64 strings
  32. String encryptedTextWithTag = EncodingUtil.base64Encode(encryptedBlob);
  33. String keyString = EncodingUtil.base64Encode(key);
  34. String ivString = EncodingUtil.base64Encode(iv);
  35. String keyiv = keyString + '^' + ivString;
  36.  
  37. return new EncryptionResult(encryptedTextWithTag, keyiv);
  38. } catch (Exception e) {
  39. throw new AuraHandledException('Error occurred while encrypting AES256: ' + e.getMessage());
  40. }
  41. }
  42.  
  43. /**
  44. * Decrypts AES-256-GCM encrypted text
  45. * @param encryptedTextWithTag The encrypted text (base64 encoded)
  46. * @param keyiv The key and IV in format "key^iv" (base64 encoded)
  47. * @return Decrypted plaintext
  48. */
  49. public static String decryptAES256(String encryptedTextWithTag, String keyiv) {
  50. try {
  51. // Parse key and IV
  52. List<String> parts = keyiv.split('\\^');
  53. if (parts.size() != 2) {
  54. throw new IllegalArgumentException('Invalid keyiv format. Expected format: key^iv');
  55. }
  56.  
  57. Blob key = EncodingUtil.base64Decode(parts[0]);
  58. Blob iv = EncodingUtil.base64Decode(parts[1]);
  59. Blob cipherText = EncodingUtil.base64Decode(encryptedTextWithTag);
  60.  
  61. // Decrypt
  62. Blob decryptedBlob = Crypto.decryptWithManagedIV('AES256', key, cipherText);
  63. return decryptedBlob.toString();
  64. } catch (Exception e) {
  65. throw new AuraHandledException('Error occurred while decrypting AES256: ' + e.getMessage());
  66. }
  67. }
  68.  
  69. /**
  70. * Encrypts plaintext using RSA with public key
  71. * @param plaintext The text to encrypt
  72. * @param publicKeyName The name of the public key stored in Salesforce
  73. * @return Encrypted text (base64 encoded)
  74. */
  75. public static String rsaEncryptWithPublicKey(String plaintext, String publicKeyName) {
  76. try {
  77. // Get the public key certificate from Salesforce
  78. List<Certificate> certificates = [
  79. SELECT Id, PrivateKeyChecksum, PublicKeyChecksum
  80. FROM Certificate
  81. WHERE DeveloperName = :publicKeyName
  82. LIMIT 1
  83. ];
  84.  
  85. if (certificates.isEmpty()) {
  86. throw new IllegalArgumentException('Certificate not found: ' + publicKeyName);
  87. }
  88.  
  89. Certificate cert = certificates[0];
  90. Blob plaintextBlob = Blob.valueOf(plaintext);
  91.  
  92. // Encrypt with public key
  93. Blob encryptedBlob = Crypto.encryptWithPublicKey('RSA2048', plaintextBlob, cert);
  94.  
  95. return EncodingUtil.base64Encode(encryptedBlob);
  96. } catch (Exception e) {
  97. throw new AuraHandledException('Error occurred while encrypting RSA Key: ' + e.getMessage());
  98. }
  99. }
  100. }
  101.  
  102. EncryptionUtil.EncryptionResult result = EncryptionUtil.encryptAES256('sensitive data');
  103. System.debug('Encrypted: ' + result.encryptedTextWithTag);
  104. System.debug('Key+IV: ' + result.keyiv);
Success #stdin #stdout #stderr 0.01s 8896KB
stdin
Standard input is empty
stdout
Object: UndefinedObject error: did not understand #EncryptionUtil
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject class(Object)>>doesNotUnderstand: #EncryptionUtil (SysExcept.st:1448)
UndefinedObject>>executeStatements (prog:1)
stderr
./prog:3: parse error, expected '}'
./prog:53: expected expression