fork download
  1. public without sharing class VIP_EncryptTermsConditionDate {
  2.  
  3.  
  4.  
  5. public static void getKeyAndIV() {
  6.  
  7. // Generate a 32-byte (256-bit) secret key for AES256-GCM
  8.  
  9. Blob key = Crypto.generateAesKey(256);
  10.  
  11. String keyString = EncodingUtil.base64Encode(key); // Securely store this!
  12.  
  13. updateCustomSetting(keyString);
  14.  
  15. }
  16.  
  17.  
  18.  
  19. public static void encryptTodayDate() {
  20.  
  21. // Get today's date
  22.  
  23. Date todayDate = Date.today();
  24.  
  25.  
  26.  
  27. // Manually construct the date in YYYY-MM-DD format
  28.  
  29. String formattedDate =
  30.  
  31. todayDate.year() +
  32.  
  33. '-' +
  34.  
  35. (todayDate.month() < 10 ? '0' + todayDate.month() : String.valueOf(todayDate.month())) +
  36.  
  37. '-' +
  38.  
  39. (todayDate.day() < 10 ? '0' + todayDate.day() : String.valueOf(todayDate.day()));
  40.  
  41.  
  42.  
  43. Blob dataBlob = Blob.valueOf(formattedDate);
  44.  
  45. //Get the KEY and IV from custom setting
  46.  
  47. VIP_Encode_URL__c encodeRec = VIP_Encode_URL__c.getInstance('URLkey');
  48.  
  49.  
  50.  
  51. String keyString = encodeRec.VIP_key__c;
  52.  
  53. Blob key = EncodingUtil.base64Decode(keyString);
  54.  
  55.  
  56.  
  57. // Additional Authenticated Data for extra security (context binding)
  58.  
  59. Blob aad = Blob.valueOf('VIP_TERMS_CONDITIONS_DATE');
  60.  
  61.  
  62.  
  63. // Encrypt using AES256-GCM with managed IV (provides authentication + encryption)
  64.  
  65. Blob encryptedBlob = Crypto.encryptWithManagedIV('AES256-GCM', key, dataBlob,aad);
  66.  
  67.  
  68.  
  69. // Convert encrypted data, IV, and key to Base64 for storage
  70.  
  71. String encryptedString = EncodingUtil.base64Encode(encryptedBlob)
  72. .replace('+', '-')
  73. .replace('/', '_')
  74. .replace('=', '');;
  75.  
  76.  
  77.  
  78. // Replace '/' with '_' only if '/' exists in the string
  79.  
  80. // String aemSafeEncryptedString = encryptedString.contains('/') ? encryptedString.replace('/', '_') : encryptedString;
  81.  
  82.  
  83.  
  84. //Requires SYSTEM_MODE because class is without sharing
  85.  
  86. String encodedURL =
  87.  
  88. [SELECT VIP_URL__c FROM VIP_URL__mdt WHERE DeveloperName = :VIP_Constants.TERMS_AND_CONDITIONS_URL WITH SYSTEM_MODE].VIP_URL__c +
  89.  
  90. aemSafeEncryptedString;
  91.  
  92. System.debug('encodedURL ==>'+encodedURL);
  93.  
  94. }
  95.  
  96.  
  97.  
  98. private static void updateCustomSetting(String keyString) {
  99.  
  100. VIP_Encode_URL__c encodeRec = VIP_Encode_URL__c.getInstance('URLkey');
  101.  
  102. if (encodeRec == null) {
  103.  
  104. // Create a new record if it doesn't exist
  105.  
  106. encodeRec = new VIP_Encode_URL__c();
  107.  
  108. encodeRec.Name = 'URLkey';
  109.  
  110. }
  111.  
  112. encodeRec.VIP_key__c = keyString;
  113.  
  114. VIP_UtilityController.upsertSObjectOperation(new List<VIP_Encode_URL__c>{ encodeRec }, 'VIP_EncryptTermsConditionDate');
  115.  
  116. }
  117.  
  118.  
  119.  
  120. }
Success #stdin #stdout #stderr 0.01s 9060KB
stdin
Standard input is empty
stdout
Object: nil error: did not understand #without
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject(Object)>>doesNotUnderstand: #without (SysExcept.st:1448)
UndefinedObject>>executeStatements (prog:1)
stderr
./prog:5: parse error, expected '}'
./prog:13: expected expression