使用非标准密钥长度调用CryptoJS.AES.encrypt / decrypt时,如何处理AES密钥? [英] How is an AES key processed when calling CryptoJS.AES.encrypt/decrypt with a non-standard key length?

查看:245
本文介绍了使用非标准密钥长度调用CryptoJS.AES.encrypt / decrypt时,如何处理AES密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在解密C#中使用长度不为128、192或256位的密钥的CryptoJS中加密的项目时遇到问题。 CryptoJS允许在加密/解密过程中使用奇数长度的密钥,但是C#中的对称算法类(例如 RijndaelManaged )不允许这样做。

I am currently having issues with decrypting items in C# that have been encrypted in CryptoJS using keys that are not of length 128, 192, or 256 bits. CryptoJS allows keys of 'odd' lengths to be used during encryption/decryption, but the symmetric algorithm classes in C# (such as RijndaelManaged) do not allow this.

JavaScript

var key =  CryptoJS.enc.Utf8.parse("This key is 160 bits"); // Not 128, 192, or 256 bits, but is allowed
var iv = CryptoJS.enc.Hex.parse("101112131415161718191a1b1c1d1e1f");
var result = CryptoJS.AES.encrypt("Encrypt Me!", key, { iv: iv });

// result.ciphertext = 2839aff89d889dd29480b038679fbd6e
// or result.ciphertext.toString(CryptoJS.enc.Base64) = KDmv+J2IndKUgLA4Z5+9bg==

C#

byte[] key = Encoding.UTF8.GetBytes("This key is 160 bits");
byte[] iv = { 0x10, 0x11, 0x12, 0x13,
              0x14, 0x15, 0x16, 0x17,
              0x18, 0x19, 0x1a, 0x1b,
              0x1c, 0x1d, 0x1e, 0x1f };
byte[] encryptMe = Encoding.UTF8.GetBytes("Encrypt Me!");

using (RijndaelManaged rm = new RijndaelManaged())
{
    rm.IV = iv;
    rm.Key = key; //This is not allowed due to the key size being 160 bits. Exception thrown here.

    using (ICryptoTransform ict = rm.CreateEncryptor())
    {
        byte[] encrypted = ict.TransformFinalBlock(encryptMe, 0, encryptMe.Length);
    }
}

我的问题是, JavaScript代码,以使其可用于加密/解密?填充?截断? CryptoJS中的AES实现是否已调整为可以使用奇数密钥长度?

My question is what exactly happens to the key in the javascript code to allow it to be used for encryption/decryption? Padding? Truncation? Is the AES implementation in CryptoJS adjusted to work with 'odd' key lengths?

我尝试过通过截断或填充(开始和结束)来调整C#代码的密钥字节数组无济于事。我对JavaScript语法并不十分熟悉,并且在不了解所发生的事情的情况下浏览了CryptoJS源代码。

I have tried adjusting the C# code's key by truncating or padding (both the beginning and end) the byte array to no avail. I'm not terribly familiar with javascript syntax and have looked over the CryptoJS source without understanding much of what's happening.

推荐答案

快速浏览CryptoJS源代码,似乎它默默地执行了一些非标准的操作。如果是这样,则无法使用标准AES复制它的功能。

I took a quick look at the CryptoJS sources, and it seems to be the case that it silently does something nonstandard. If this is true, there is no way to replicate what it does using standard AES.

但是,要确保没有任何密钥派生,请尝试执行 alert(result.key); 看看是否与您输入的内容相同。

However, to make sure there is no key derivation or such, try doing an alert(result.key); and see if it's the same as your input.

这篇关于使用非标准密钥长度调用CryptoJS.AES.encrypt / decrypt时,如何处理AES密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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