Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.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# NET与nodejs、CryptoJS之间的AES算法值差异_C#_Node.js_Encryption_Aes_Cryptojs - Fatal编程技术网

C# NET与nodejs、CryptoJS之间的AES算法值差异

C# NET与nodejs、CryptoJS之间的AES算法值差异,c#,node.js,encryption,aes,cryptojs,C#,Node.js,Encryption,Aes,Cryptojs,下面C#中的AES算法返回的加密值与node js和CryptoJS返回的值不同。NodeJS加密库和CryptoJS返回相同的值,但.NET的AesCryptoServiceProvider返回不同的值。有什么想法吗 C#示例 private const string AesIV = @"!QAZ2WSX#EDC4RFV"; private const string AesKey = @"5TGB&YHN7UJM(IK<"; public static

下面C#中的AES算法返回的加密值与node js和CryptoJS返回的值不同。NodeJS加密库和CryptoJS返回相同的值,但.NET的AesCryptoServiceProvider返回不同的值。有什么想法吗

C#示例

    private const string AesIV = @"!QAZ2WSX#EDC4RFV";
    private const string AesKey = @"5TGB&YHN7UJM(IK<";


    public static void Main()
    {
        try
        {
            string original = "HelloWorld";
            Console.WriteLine(Encrypt(original));
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: {0}", e.Message);
        }
    }
    /// <summary>
    /// AES Encryption
    /// </summary>
    private static string Encrypt(string text)
    {
        // AesCryptoServiceProvider
        AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
        aes.BlockSize = 128;
        aes.KeySize = 128;
        aes.IV = Encoding.UTF8.GetBytes(AesIV);
        aes.Key = Encoding.UTF8.GetBytes(AesKey);
        aes.Mode = CipherMode.CBC;
        aes.Padding = PaddingMode.PKCS7;

        // Convert string to byte array
        byte[] src = Encoding.Unicode.GetBytes(text);

        // encryption
        using (ICryptoTransform encrypt = aes.CreateEncryptor())
        {
            byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);

            // Convert byte array to Base64 strings
            return Convert.ToBase64String(dest);
        }
    }
private const string AesIV=@“!QAZ2WSX#EDC4RFV”;

private const string AesKey=@“5TGB&YHN7UJM(IK您的C#版本使用UTF-16LE将HelloWorld转换为明文字节。NodeJS one(可能也就是CryptoJS one)使用UTF-8字节。在C#one中使用Encoding.UTF8.GetBytes()。

您的C#版本使用UTF-16LE将HelloWorld转换为明文字节。NodeJS one是一个(大概是CryptoJS one)使用UTF-8字节。在C#one中使用Encoding.UTF8.GetBytes()。

NodeJS中的默认填充是PKCS5(假设CryptoJS是相同的,因为您说它们产生相同的结果)。您的C#代码将填充设置为PKCS7。

NodeJS中的默认填充是PKCS5(假设CryptoJS是相同的,因为您说过它们会产生相同的结果)。您的C#code正在将填充设置为PKCS7。

它们会产生哪些值?字符集差异?添加行结尾?是否有多余的垃圾标记“UTF字节顺序”?它们会产生哪些值?字符集差异?添加行结尾?是否有多余的垃圾标记”UTF字节顺序“?
crypto = require "crypto"
algo = 'aes-128-cbc'
keyBuffer = new Buffer("!QAZ2WSX#EDC4RFV")
ivBuffer = new Buffer("5TGB&YHN7UJM(IK<")

cipher = crypto.createCipheriv(algo, keyBuffer, ivBuffer)
textBuffer = new Buffer('HelloWorld')
encrypted = cipher.update(textBuffer)
encryptedFinal = cipher.final()
encryptedText = encrypted.toString('base64') + encryptedFinal.toString('base64')

console.log encryptedText
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script>
    var key = CryptoJS.enc.Utf8.parse('!QAZ2WSX#EDC4RFV');
    var iv  = CryptoJS.enc.Utf8.parse('5TGB&YHN7UJM(IK<');

    var encrypted = CryptoJS.AES.encrypt("HelloWorld", key, { iv: iv });

    alert(encrypted);
</script>