• Stanley Lopez
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

I am working on the Encryption process.

I need to generate encrypted payload by following instructions from the below link: https://support.trustpilot.com/hc/en-us/articles/115004145087--Business-Generated-Links-for-developers-
I tried below code below:
 

String text = '{"email":"sudharani256@gamil.com","name":"Sudha Rani Y","ref":"1234"}';

Blob key = Crypto.generateAesKey(128);

Blob data = Blob.valueOf(text);

String clearText = '{"email":"sudharani256@gmail.com","name":"Sudha Rani Y","ref":"1234"}';

String b64Data = EncodingUtil.base64Encode(data);


String encryptKey = '/...................';

Blob cipherText = Crypto.encryptWithManagedIV('AES128', key, Blob.valueOf(clearText));

//note 1 add above iV in place of key
blob encryptedData = Crypto.encrypt('AES256', EncodingUtil.base64Decode(encryptKey),key,data);

//16 byte string. since characters used are ascii, each char is 1 byte.
//encrypted blob

String strMd5= EncodingUtil.base64Encode (Crypto.generateDigest('MD5',EncodingUtil.base64Decode('ekR0VlHRzQxIlNu0smkKh8sOOcfUAqwsw71g4fhHChA=')));


String algorithmName = 'HmacSHA256';


Blob hmacData = Crypto.generateMac(algorithmName, EncodingUtil.base64Decode(strMd5), data);

string input='...';  // what you want to sign

string privateKey= '...................';  // your private key
Blob keyTwo = Crypto.generateAesKey(256);

//blob rs256sig = Crypto.sign('RSA', data, EncodingUtil.base64Decode(encryptKey));

system.debug(EncodingUtil.urlEncode(EncodingUtil.base64Encode(key)+EncodingUtil.base64Encode(cipherText)+EncodingUtil.base64Encode(hmacData),'UTF-8'));
 


From the above code, I tried rs256sig instead of hmacData but getting error like "System.SecurityException: Invalid Crypto Key".
Please help me it is very needful.