3

Related to the topic in this post: Converting Coldfusion encryption code to C#

How would you do the conversion for this:

<!--- valueToEncrypt = "34245678", key = "TJhKuhjyx/87giutBNHh9t==" --->
<cfset output = Encrypt( valueToEncrypt, key, "AES", "Hex")>

Here's what I tried in C#:

byte[] plainText = Encoding.UTF8.GetBytes(TextToEncrypt);
byte[] key = Convert.FromBase64String(encryptionKey);
RijndaelManaged algorithm = new RijndaelManaged();
algorithm.Mode = CipherMode.ECB;
algorithm.Padding = PaddingMode.PKCS7;
algorithm.BlockSize = 128;
algorithm.KeySize = 128;
algorithm.Key = key;
string result;
using (ICryptoTransform encryptor = algorithm.CreateEncryptor())
{
    using (MemoryStream memoryStream = new MemoryStream())
    {
        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
        {
            cryptoStream.Write(plainText, 0, plainText.Length);
            cryptoStream.FlushFinalBlock();
            result = Convert.ToBase64String(memoryStream.ToArray());
        }
    }
}

return result;

ColdFusion Result:

04197FAA3C9C030660A6377E44F77C4E

C# Result:

BBl/qjycAwZgpjd+RPd8Tg==
Community
  • 1
  • 1
NewBee
  • 53
  • 7
  • 1
    Welcome to SO. Please take a minute to read [Ask], then [edit](http://stackoverflow.com/posts/39230309/edit) your question to include A) the code you have tried so far and B) the result. If you have not tried anything yet, please start by doing a search on [coldfusion AES C#](http://stackoverflow.com/search?q=%5Bcoldfusion%5D+AES+C%23). There are plenty of examples for basic AES, such as http://stackoverflow.com/questions/31945755/c-sharp-and-coldfusion-aes-encryption-not-matching – Leigh Aug 30 '16 at 14:33
  • Thanks for the quick response Leigh. I have been following all your replies in the other posts and have tried your solutions. My problem is similar to the initial problem in the thread you suggested. The results do not match in CF and C#. I have updated my post to include what I've tried. Thanks again. – NewBee Aug 30 '16 at 14:48
  • What are the literal values you used for the `#valueToEncrypt#` and `#key#`? We need to see the encoding (base64, etc..), key size, etcetera. Obviously do *not* post your real key (generate a similar dummy key if needed). – Leigh Aug 30 '16 at 14:53
  • valueToEncrypt: is a unique ID like 34245678 and key: is a string like TJhKuhjyx/87giutBNHh9t==. Also the resulting encrypted value in ColdFusion has always been 32 characters long – NewBee Aug 30 '16 at 14:55
  • One big difference is the C# example returns base64, whereas the CF code returns hex. Does the result have to be "hex"? If so, you will need to change the C# code a bit. – Leigh Aug 30 '16 at 15:03
  • The resulting encrypted value is always similar to something like this: 78GSHS88BSGST6FGN48JANQABAK1HGAK...32 characters long, just alphanumeric with no special characters. – NewBee Aug 30 '16 at 15:05

1 Answers1

4

Actually the results are the same. They are just encoded differently. Both encrypt the input and generate binary, then encode the result for easier storage and transport. The ColdFusion code just chooses to encode those bytes as "hex", while the C# code uses "base64". While the results may look different, they still represent the same value. For example, notice if you decode the C# result (base64) into binary and re-encode it as hex, it matches the CF result?

C# (Convert result from base64 to hex)

byte[] decoded = Convert.FromBase64String("BBl/qjycAwZgpjd+RPd8Tg==");
string resultAsHex = (BitConverter.ToString(decoded).Replace("-", string.Empty));

Result:

04197FAA3C9C030660A6377E44F77C4E 

Having said that, if you need to produce the same encoded string on both sides, either:

A. Change the C# code to encode the result as hex, instead of base64

    result =  BitConverter.ToString(memoryStream.ToArray()).Replace("-", string.Empty);

OR

B. Change the CF code to use base64 encoding:

    <cfset output = Encrypt( valueToEncrypt, key, "AES", "Base64")>
Community
  • 1
  • 1
Leigh
  • 28,424
  • 10
  • 49
  • 96