Apex generate a hash value for encrypting and decrypting emails

String cryptoSalt='249D5EC76175B12A';
Blob cryptoKey=Blob.valueof('J@NcRfUjXn2r5u8x/A?D(G-KaPdSgVkY');
Blob cryptoIv =Blob.valueof('4t6w9z$C&F)J@NcR');

Blob data = Blob.valueOf('youremail@gmail.com'+cryptoSalt);
Blob encryptedData = Crypto.encrypt('AES256', cryptoKey, cryptoIv, data);
String encodeDataHex = EncodingUtil.convertToHex(encryptedData);
System.debug('>>> encodeDataHex >>> ' + encodeDataHex);
//92939c472350e797aca42206a1b1d90f3b33bd1dbbac23af5fba40866515ef43e11e35edd354d341253bf89eb4094f2c

String encodeData = EncodingUtil.base64Encode(encryptedData);
System.debug('>>> encodeData >>> ' + encodeData);
//kpOcRyNQ55espCIGobHZDzszvR27rCOvX7pAhmUV70PhHjXt01TTQSU7+J60CU8s

// Decrypt the data - the first 16 bytes contain the initialization vector
Blob decodeData = EncodingUtil.base64Decode(encodeData);
Blob decryptedData = Crypto.decrypt('AES256', cryptoKey, cryptoIv, decodeData);

// Decode the decrypted data for subsequent use
String decryptedDataString = decryptedData.toString();
System.debug('>>> ' + decryptedDataString);
//youremail@gmail.com249D5EC76175B12A

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: