C#和ColdFusion AES加密不匹配 [英] C# and ColdFusion AES Encryption not matching

查看:121
本文介绍了C#和ColdFusion AES加密不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须加密url查询sting在C#,并传递到ColdFusion页面。有人可以帮助我使用AES算法在C#.net编写加密代码,相当于下面的ColdFusion函数?提前感谢。

 < cfset strLink = Encrypt(top secret,WTq8zYcZfaWVvMncigHqwQ ==,AES Hex)> 

CF结果




  • strLink = 91E72250B8A7EDBC4E5AF37F04E6AB5B



我尝试下面的代码在C#,但结果不匹配。

  byte [] plainText = Encoding.Unicode.GetBytes最高机密); 

byte [] key = Convert.FromBase64String(WTq8zYcZfaWVvMncigHqwQ ==);
RijndaelManaged algorithm = new RijndaelManaged();
algorithm.Mode = CipherMode.ECB;
algorithm.Padding = PaddingMode.PKCS7;
algorithm.BlockSize = 128;
algorithm.KeySize = 128;
algorithm.Key = key;
string result;
使用(ICryptoTransform的加密= algorithm.CreateEncryptor()),使用(MemoryStream的MemoryStream的=新的MemoryStream())
{
使用
{
(CryptoStream的CryptoStream的=新的CryptoStream (MemoryStream的,加密,CryptoStreamMode.Write))
{
cryptoStream.Write(明文,0,plainText.Length);
cryptoStream.FlushFinalBlock();
result = Convert.ToBase64String(memoryStream.ToArray());
}
}
}
return result;

C#结果




  • HEX = 89F9F3C55CD232362FE1E14240C479BE5B56210FF3913E7B6BA4BCD3C87F9AA7

  • 的Base64 = ifnzxVzSMjYv4eFCQMR5vltWIQ / zkT57a6S808h / mqc =


解决方案

/ p>

这是一个字符编码是如何使一个的的区别一个很好的例子。



相信与否,这只是因为在C#代码中使用了错误的编码。 Encoding.Unicode 使用的 UTF-16 ,而CF的加密函数总是使用的 UTF-8 (非常不同)。因此,C#代码正在加密一个完全不同于CF的值。因此,不同的结果,为什么长度C#字符串(十六进制)长于一个来自CF返回的



使用的 Encoding.UTF8.GetBytes()而不是 Encoding.Unicode.GetBytes(),结果将匹配:

  byte [] plainText = Encoding.UTF8.GetBytes(top secret); 


I have to encrypt url query sting in C# and pass to ColdFusion page. Can someone help me on writing encryption code using AES algorithm in C#.net that is equivalent to below ColdFusion function? Thanks in advance.

<cfset strLink = Encrypt("top secret", "WTq8zYcZfaWVvMncigHqwQ==", "AES","Hex")>

CF Result:

  • strLink = 91E72250B8A7EDBC4E5AF37F04E6AB5B

I tried below code in C#, but the results are not matching.

        byte[] plainText = Encoding.Unicode.GetBytes("top secret");

        byte[] key = Convert.FromBase64String("WTq8zYcZfaWVvMncigHqwQ==");
        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;

C# Result:

  • HEX = 89F9F3C55CD232362FE1E14240C479BE5B56210FF3913E7B6BA4BCD3C87F9AA7
  • Base64 = ifnzxVzSMjYv4eFCQMR5vltWIQ/zkT57a6S808h/mqc=

解决方案

(From comments...)

This is a perfect example of how character encoding makes a big difference.

Believe it or not, it is simply due to using the wrong encoding in the C# code. Encoding.Unicode uses UTF-16, whereas CF's Encrypt function always uses UTF-8 (very different). Consequently, the C# code is encrypting a totally different value than CF. Hence the different results, and why the length of the C# string (hex) is longer than the one returned from CF.

Use Encoding.UTF8.GetBytes() instead of Encoding.Unicode.GetBytes() and the results will match:

byte[] plainText = Encoding.UTF8.GetBytes("top secret");

这篇关于C#和ColdFusion AES加密不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆