Tuesday, 19 March 2019

encryption and decryption

 //text to encrypt
    String clearText = 'FDVendor';

    //16 byte string. since characters used are ascii, each char is 1 byte.
    Blob key = Blob.valueOf('12345678901234567890123456789012');
    //encrypted blob
    Blob cipherText = Crypto.encryptWithManagedIV('AES256', key, Blob.valueOf(clearText ));
    //encrypted string
    String encodedCipherText = EncodingUtil.base64Encode(cipherText);

    System.debug(encodedCipherText);

    //encrypted blob
    Blob encodedEncryptedBlob = EncodingUtil.base64Decode(encodedCipherText);
    //decrypted blob
    Blob decryptedBlob = Crypto.decryptWithManagedIV('AES256', key, encodedEncryptedBlob);
    //decrypted string
    String decryptedClearText = decryptedBlob.toString();

    System.debug(decryptedClearText);


************Finailzed below

Blob key = Blob.valueOf(System.Label.SecPSD);
Blob data = Blob.valueOf('FDVendor_Live');
                String encodeddata =  EncodingUtil.base64encode(data);
                System.debug('encodeddata ' +encodeddata);
                Blob ecdata = Blob.valueOf(encodeddata);
                // Encrypt the data and have Salesforce.com generate the initialization vector
                Blob encryptedData = Crypto.encryptWithManagedIV('AES256', key, ecdata);
                System.debug('encryptedData ' +encryptedData);
                String encodeddata1 =  EncodingUtil.base64encode(encryptedData);
                System.debug('encodeddata1 ' +encodeddata1);

**************

SB3g45P2UScCfmW+KVZJnIw/9ER1aEXwNav+HxDUEOO/27jcF7N911pqvC8IJx+G
UOe5R2RkY7npT28yoCpCEbfMtl7e62B9lDwrLBR8sWTyTPjCk8H3xSEXCsDAL29l

String uname='UOe5R2RkY7npT28yoCpCEbfMtl7e62B9lDwrLBR8sWTyTPjCk8H3xSEXCsDAL29l';
Blob encodedEncryptedBlob1 = EncodingUtil.base64Decode(uname);
 Blob key = Blob.valueOf(System.Label.SecPSD);
Blob decryptedData = Crypto.decryptWithManagedIV('AES256', key, encodedEncryptedBlob1);
            System.debug('decryptedData ' +decryptedData);
            String decryptedDataString = decryptedData.toString();
            System.debug('decryptedDataString ' +decryptedDataString);
            Blob bdata  = EncodingUtil.base64Decode(decryptedDataString);
            String decryptedClearText1 = bdata.toString();
            System.debug('decrypted username  ' +decryptedClearText1);

No comments:

Post a Comment