I have a input form in LWC(It's a salesforce Public Site). Once the Form submitted by the user , we will generate the Random Password and send Email to User with username and password using Email template.(Username is Email address)
Password generator Logic
Integer len = 10;
Blob blobKey = crypto.generateAesKey(128);
String key = EncodingUtil.convertToHex(blobKey);
string pwd = key.substring(0,len);
System.debug('************ '+pwd);
Right now I'm storing the password as a plain text in my field.
My requirement is to store the encrypt password value in that field and in email send the decrypt value.
To achieve that, I'm using Crypto.encryptWithManagedIV to encrypt and storing in the field on sending email i want to sent the decrypt value of that field. Now it's sending a encrypt value in the email.
Can you Please tell me how to achieve this. (I'm using Email template with setWhatId)
Below code to encrpty and Decrypt the value
//Data to encrypt
Blob data = Blob.valueOf(pwd);
//Generate an AES key for the encryption.
Blob cryptoValue = Crypto.generateAesKey(256);
//Encrypt the data
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoValue, data);
system.debug('encryptedData' + encryptedData );
//Decrypt the data - the first 16 bytes contain the initialization vector
Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoValue, encryptedData);
//Get decrypted data
String decryptedDataString = decryptedData.toString();
System.debug(decryptedDataString);