C#AES CBC错误的结果

时间:2012-03-10 12:58:55

标签: c# aes

我正在尝试使用C#加密(AES)一些数据。

然而,与网站相比,我得到了不同的结果: http://www.phpaes.com/index.php#encrypt

TESTDATA:

Key = "abcdef1234567890"
IV = "1234567890abcdef"
Text = "test"

我的C#代码如下所示:

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Test
{
    class Program
    {
        public static void Main()
        {
            try
            {
                using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
                {
                    aes.Mode = CipherMode.CBC;
                    aes.Key = Encoding.UTF8.GetBytes("abcdef1234567890");
                    aes.IV = Encoding.UTF8.GetBytes("1234567890abcdef");

                    ICryptoTransform encryptor = aes.CreateEncryptor();

                    using (MemoryStream msEncrypt = new MemoryStream())
                    {
                        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                            {
                                swEncrypt.Write(Encoding.UTF8.GetBytes("test"));
                            }
                            byte[] encrypted = msEncrypt.ToArray();
                            Console.WriteLine(Convert.ToBase64String(encrypted));
                        }
                    }

                    Console.ReadKey();
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }

        }

    }
}

结果:(Base64)

C#-Program:nhb34JyWvhiqiz8Gq9Z / kA ==

网站:8jbduVNSb0Cz4cF + Cm9pCw ==

为什么?它与编码有关吗?有没有我忘记添加到代码中的东西?感谢。

0 个答案:

没有答案