+ Start a Discussion
U JayU Jay 

Isn't same string encrypt with same key generate same encrypted value?

I need an alogorithm to get same value after encryption if i use same string.
<apex:page controller="encryptNewController">
    <apex:form >  
         <apex:outPutLabel value="Encrypted by Code"/>
         <apex:inputsecret value="{!encryptedByCode}"/>
       
       
         <apex:commandButton value="Save" action="{!saveValues}"/>
         <apex:outputText value="{!decryptedDataString}"/>
    </apex:form>
</apex:page>


public with sharing class encryptNewController {

     Public Encrypt_Object__c encryptObject {get;set;}
     Public String encryptedByCode {get;set;}
     Public String decryptedDataString {get;set;}
     Blob cryptoKey;
     public encryptNewController(){
          encryptObject=new Encrypt_Object__c();
     }
     public void saveValues(){
            List<CryptoKey__c> keyValue = [SELECT  Key__c FROM CryptoKey__c where id != null];
                String cryptoKeyString;
                System.debug('000000000000000000000000000000000000000000keyValue'+keyValue);
                if(keyValue.size() > 0){
                    cryptoKeyString = keyValue[0].Key__c;
                    cryptoKey = EncodingUtil.base64Decode(cryptoKeyString);
                System.debug('000000000000000000000000000000000000000000cryptoKey'+cryptoKey);
            }          
            encryptObject.Encrypted_by_Code__c = encryptToken(encryptedByCode);
          
            insert encryptObject;
            Encrypt_Object__c insertedencryptObject = [Select id,Encrypted_by_Code__c from Encrypt_Object__c where id=: encryptObject.id][0];
            decryptedDataString =decryptToken(insertedencryptObject.Encrypted_by_Code__c);           
     }
 
     public String encryptToken(String strOriginal){
        Blob encryptedData;
        if(cryptoKey != null){
            String strUrlUTF8 = EncodingUtil.urlEncode(strOriginal, 'UTF-8');
            Blob b = Blob.valueOf(strUrlUTF8);
            System.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@cryptoKey'+cryptoKey);
            encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, b);
            return  EncodingUtil.base64Encode(encryptedData);
        }else{
            return null;
        }
      
      }
   
      public String decryptToken(String encryptedString){
        if(cryptoKey != null){
         Blob b = EncodingUtil.base64Decode(encryptedString);
         Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey, b);
         String strUrlUTF8 = decryptedData.toString();
         return EncodingUtil.urlDecode(strUrlUTF8, 'UTF-8');
        }else{
            return null;
        }
      }  
}
================================================
This is my page and class to encrypt the string entered and save on databease.
Each time the 'Key__c' is same as i take from custom settings.
But when i enter 'abc' and save two times the entered value is different.
What is the reason?
Isn't  same string encrypt with same key generate same encrypted value?
Thanks in advance
SeAlVaSeAlVa

Hello, 

Encrypt generates the same output based on IV and cryptoKey. As you are using "WithManagedIV", this means that salesforce generates the IV for you and concatenates it at the begining of the string.

For that reason, each time you call encrypt, it generates a different IV, and hence, a different encrypted String.

You can check-it out at https://developer.salesforce.com/page/Apex_Crypto_Class#Discussion_and_Sample_Code
 

The algorithm requires an initialization vector of 16 bytes (128 bits). Use the encryptWithManagedIV() function to have Salesforce generate the IV for you in the first 16 bytes of the cipher text
Kind regards