Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C AES CBC错误结果_C#_Aes - Fatal编程技术网

C# C AES CBC错误结果

C# C AES CBC错误结果,c#,aes,C#,Aes,我试图用C加密一些数据 然而,与网站相比,我得到了一个不同的结果: 测试数据: Key = "abcdef1234567890" IV = "1234567890abcdef" Text = "test" 我的C代码如下所示: using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Test { class Program {

我试图用C加密一些数据

然而,与网站相比,我得到了一个不同的结果:

测试数据:

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-项目:nhb34JyWvhiqiz8Gq9Z/kA==

网站:8jbduVNSb0Cz4cF+Cm9pCw==


为什么呢?这和编码有关吗?有什么我忘记添加到代码中的吗?谢谢。

请确保使用相同的填充模式。我尝试了所有4种填充模式ANSIX923、ISO10126、PKCS7和零,但都没有达到预期效果:在将流转换为数组之前尝试刷新流。或者这是一个编码问题;尝试UTF16和其他编码。在不知道PHP库在这里做什么的情况下,很难判断错误在哪里。PHP库也可能是错误的。您可以使用官方测试向量测试代码和PHP库。你可以在附录C中找到它们,为什么你想得到与该网站相同的结果?或者使用二进制/十六进制键。或者使用一个真正的键派生函数。IV应该是一个随机的二进制值。@dtb flush没有区别,我试试官方向量