0

Apex Class:

public with sharing class Security {
  /*
    key length depends of ALGORITHM_NAME! 
    In case, it uses AES256 so we need to use 32 bytes key must.
  */    
  public static final String MY_KEY = '12345678901234567890123456789012'; // it always must  be 32bytes 
  public static final String MY_IV = 'HR$2pIjHR$2pIj12'; //it always will be 16 bytes
  public static final String ALGORITHM_NAME = 'AES256';
  public static Blob encrypt(String valueToEncrypt){
    Blob key = Blob.valueOf(MY_KEY);
    Blob iv = Blob.valueOf(MY_IV);
    Blob data = Blob.valueOf(valueToEncrypt);
    return Crypto.encrypt(ALGORITHM_NAME, key, iv, data);
  }
  public static Blob decrypt(Blob encryptedData){
    Blob key = Blob.valueOf(MY_KEY);
    Blob iv = Blob.valueOf(MY_IV);
    return Crypto.decrypt(ALGORITHM_NAME, key, iv, encryptedData);
  }
   
}
 
//--------------open the anonymous window in developer console-----------------------//
Blob encryptedData = Security.encrypt('India');
String base64Data = EncodingUtil.base64Encode(encryptedData);
System.debug('encryptedData: ' + base64Data);
System.debug('iv: ' + EncodingUtil.base64Encode(Blob.valueOf(Security.MY_IV)));
System.debug('key: ' + EncodingUtil.base64Encode(Blob.valueOf(Security.MY_KEY)));
Blob decryptedData = Security.decrypt(encryptedData);
System.debug('decryptedData: ' + decryptedData.toString());

//------------------------requirement is here------------------------//

We have used a salesforce method key sizeof AES256 and keyvalue as '12345678901234567890123456789012' 32bytes for encrypting the login credentials But in .net application for encrypting, they have used keysize of AES256 and keyvalue as “MARKETING” which is 9bytes. we had used the same keyvalue“MARKETING”,for encryption but it is showing an error that keyvalue must be of 32bytes..

AFTER ENCRYPTION INDIA THE ENCRPTED VALUE SHOULD BE :

k+aPFgavOKqygOIsLhf8EQ==

For 'India'

////////////////////----dotnet code as shown below----//////////////////
private static string Salt = "MarketingInc";
        private static string HashAlgorithm = "SHA1";
        private static int PasswordIterations = 2;
        private static string InitialVector = "HR$2pIjHR$2pIj12";
        private static int KeySize = 256;
 
 
public static string EncryptText(string toBeEncrypted, string password)
        {
            string result;
            try
            {
                if (string.IsNullOrEmpty(toBeEncrypted))
                {
                    result = "";
                }
                else
                {
                    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(toBeEncrypted);
                    byte[] bytes2 = System.Text.Encoding.UTF8.GetBytes(password);
                    byte[] inArray = EncryptDecrypt.AES_Encrypt(bytes, bytes2);
                    string text = System.Convert.ToBase64String(inArray);
                    result = text;
                }
            }
            catch (System.Exception)
            {
                result = "";
            }
            return result;
        }
        public static string DecryptText(string toBeDecrypted, string password)
        {
            string result;
            try
            {
                if (string.IsNullOrEmpty(toBeDecrypted))
                {
                    result = "";
                }
                else
                {
                    byte[] cipherTextBytes = System.Convert.FromBase64String(toBeDecrypted);
                    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(password);
                    byte[] bytes2 = EncryptDecrypt.AES_Decrypt(cipherTextBytes, bytes);
                    string @string = System.Text.Encoding.UTF8.GetString(bytes2);
                    result = @string;
                }
            }
            catch (System.Exception)
            {
                result = "";
            }
            return result;
        }
3

AES256 cannot function with a 9 byte key. That's not how encryption works. Your AES_Encrypt function is padding the "password" with, well, something. It's not null values, nor spaces, nor wrapping around, as far as I can tell, because none of those end up with the same output. The key must be 32 bytes long for 256-bit encryption, so I don't know where it's pulling the remainder of the key material from.

If you use "12345678901234567890123456789012" in .Net, you should get the same output. Verify this first before continuing. Once you have that, you need to figure out what those mystery bytes are that .Net uses when there's too few characters to use. You might consider computing an HMAC using SHA256 to begin with:

  // Apex Code
  public static final String MY_KEY = 'MARKETING'; // it always must  be 32bytes 
  public static final String MY_IV = 'HR$2pIjHR$2pIj12'; //it always will be 16 bytes
  public static final String ALGORITHM_NAME = 'AES256';
  public static Blob encrypt(String valueToEncrypt){
    Blob key = Crypto.generateDigest('SHA256', Blob.valueOf(MY_KEY));
    Blob iv = Blob.valueOf(MY_IV);
    Blob data = Blob.valueOf(valueToEncrypt);
    return Crypto.encrypt(ALGORITHM_NAME, key, iv, data);
  }

This will ensure you always have an appropriate-sized key material that's reasonably secure. From there, modify your .Net code to match this behavior. This should give you the same output for each. I don't have a .Net compiler on my system, so I can't verify this, but it should get you closer to your goal.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.