使用C#和CryptoJS的不同加密结果 [英] Different encryption results using C# and CryptoJS

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

问题描述

我在服务器应用程序中使用AES加密某些数据,这是用C#编写的。我使用预定义的密钥(32字节)和IV(16字节),例如......

I encrypt some data using AES in a server application, which is written in C#. I use a predefined key (32 bytes) and IV (16 bytes), for instance...

Key: 81fe1681..6a451c1c
IV:  e83c..ae76

这是我用来加密的C#代码数据:

This is my C# code I use to encrypt the data:

async Task<byte[]> Encrypt(string privateKey, string pin, byte[] data)
{
    using (var sha = SHA256.Create())
    {
        byte[] keyHash = sha.ComputeHash(Encoding.UTF8.GetBytes($"{privateKey}"));
        byte[] pinHash = sha.ComputeHash(Encoding.UTF8.GetBytes($"{pin}"));
        using (Aes aes = Aes.Create())
        {
            byte[] key = keyHash.Slice(0, aes.Key.Length);
            byte[] iv = pinHash.Slice(0, aes.IV.Length);
            using (ICryptoTransform transform = aes.CreateEncryptor(key, iv))
            using (var stream = new MemoryStream())
            using (var cryptStream = new CryptoStream(stream, transform, CryptoStreamMode.Write))
            {
                await cryptStream.WriteAsync(data, 0, data.Length);
                await cryptStream.FlushAsync();

                return stream.ToArray();
            }
        }
    }
}

加密结果数据看起来像......

The encrypted result data looks like...

534c..28f5

现在,我想使用CryptoJS解密客户端应用程序中的数据。我使用完全相同的密钥和IV信息,但解密似乎失败...至少解密结果总是为空。

Now, I want to decrypt the data in a client application using CryptoJS. I use the exact same key and IV information, but decryption seems to fail... at least the decrypted result is always empty.

所以,我加密了数据客户端(当然是相同的密钥和IV),结果加密的文本是不同的;更准确地说,它是相同的,但最后有更多的数据...

So, I encrypted the data on the client (of course same key and IV) and in result the ciphered text is different; more precisely it is identical but has more data at the end...

534c..28f5bbd5..ac0e

如果我加密服务器上的数据,最后这些额外的数据是什么?

What is this additional data at the end that I don´t get if I encrypt the data on the server?

如果我解密在客户端加密的加密文本,则解密有效。只需提一下,模式和填充在服务器和客户端都是默认的,即 CBC Pkcs7 ; keysize应为 256 。这是我用来解密服务器加密的数据的代码:

If I decrypt the ciphered text that has been encrypted on the client, the decryption works. Just to mention it, mode and padding are default on both server and client, which is CBC and Pkcs7; keysize should be 256. This is the code I use to decrypt the data that has been ciphered by the server:

let keyHash: WordArray = CryptoJS.SHA256(CryptoJS.enc.Utf8.parse(privateKey));
let key: WordArray = CryptoJS.lib.WordArray.create(keyHash.words.slice(0, 8), 32);

let pinHash: WordArray = CryptoJS.SHA256(CryptoJS.enc.Utf8.parse(pin));
let iv: WordArray = CryptoJS.lib.WordArray.create(pinHash.words.slice(0, 4), 16);

let cfg: CryptoJS.lib.IBlockCipherCfg = { iv: iv };
let paramsData: CryptoJS.lib.CipherParamsData = { 
    ciphertext: cipherBuffer
};

let decrypted: WordArray = CryptoJS.AES.decrypt(paramsData, key, cfg);


推荐答案

对于写入,有一个问题是刷新块。 FlushFinalBlock() Flush()(或 FlushAsync()不同)。您必须同时执行它们,或者只是处理 CryptoStream 。这将解决代码没有写入最后一个数据块的事实。

For the write there was a problem with the flushing of the blocks. The FlushFinalBlock() is distinct from the Flush() (or from the FlushAsync()). You have to do them both, or simply dispose the CryptoStream. This will solve the fact that the code wasn't writing the last block of data.

async static Task<byte[]> Encrypt(string privateKey, string pin, byte[] data)
{
    using (var sha = SHA256.Create())
    {
        byte[] keyHash = sha.ComputeHash(Encoding.UTF8.GetBytes($"{privateKey}"));
        byte[] pinHash = sha.ComputeHash(Encoding.UTF8.GetBytes($"{pin}"));
        using (Aes aes = Aes.Create())
        {
            byte[] key = keyHash.Slice(0, aes.Key.Length);
            byte[] iv = pinHash.Slice(0, aes.IV.Length);

            Trace.WriteLine($"Key length: { key.Length }, iv length: { iv.Length }, block mode: { aes.Mode }, padding: { aes.Padding }");

            using (var stream = new MemoryStream())
            using (ICryptoTransform transform = aes.CreateEncryptor(key, iv))
            {
                using (var cryptStream = new CryptoStream(stream, transform, CryptoStreamMode.Write))
                {
                    await cryptStream.WriteAsync(data, 0, data.Length);
                }

                return stream.ToArray();
            }
        }
    }
}

打字稿代码似乎能够解密它。

The typescript code seems to be able to decrypt it.

工作小提琴: https://jsfiddle.net/uj58twrr/3/

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

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