在 Flutter 上解密 AES 输入,在 web 上使用 cryptoJS AES [英] decrypt AES input on Flutter, when on web use cryptoJS AES

查看:23
本文介绍了在 Flutter 上解密 AES 输入,在 web 上使用 cryptoJS AES的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网络上,我使用 CryptoJS 来解密 JS:

On web, I'm using CryptoJS for decrypto JS:

CryptoJS.AES.decrypt(inputBase64, key).toString(CryptoJS.enc.Utf8);

CryptoJS.AES.decrypt(inputBase64, key).toString(CryptoJS.enc.Utf8);

示例:

输入:"tzfwnxVwE/qNoaWRRfqLp11ZyhB4UtKO+0/Lvv5B7eE="键:20190225165436_15230006321670000_15510884759030000"

input: "tzfwnxVwE/qNoaWRRfqLp11ZyhB4UtKO+0/Lvv5B7eE=" key: "20190225165436_15230006321670000_15510884759030000"

在 flutter 中,我找不到任何可以使用任意长度密钥解密的库.

On flutter I can't find any library to decrypt with a key of any length.

我知道对于 AES,NIST 选择了 Rijndael 家族的三个成员,每个成员的块大小为 128 位,但三个不同的密钥长度:128、192 和 256 位."

I know "For AES, NIST selected three members of the Rijndael family, each with a block size of 128 bits, but three different key lengths: 128, 192 and 256 bits. "

但我不知道如何将任何长度的密钥转换为 128 位格式?

But I don't know how to convert any length key to 128 bit format?

推荐答案

当您将字符串作为密钥传递给 CryptoJS 时,它会将其视为密码短语,并使用密钥派生函数(在本例中为 PBKDF2)从中生成密钥.它生成一个 256 位的密钥和一个 128 位的初始化向量 (IV).然后它使用那些进行加密/解密.您还需要找出 CryptoJS 使用的链接方法(可能是密码块链接 (CBC))以及它使用的填充方法(以确保纯文本是 128 位块的整数 - 可能是 PKCS#7).

When you pass CryptoJS a string as the key it treats it as a passphrase and generates the key from it using a key derivation function - in this case PBKDF2. It generates a 256 bit key and a 128 bit initialization vector (IV). It then uses those for the encryption/decryption. You also need to find out what chaining method CryptoJS uses (probably cipher block chaining (CBC)) and what padding method it uses (to make sure that the plain text is a round number of 128 bit blocks - probably PKCS#7).

CryptoJS 有这种开箱即用"的模式,但它在幕后做了什么并不是特别清楚——你需要阅读源代码或搜索文档.

CryptoJS has this "works out of the box" mode, but it isn't particularly clear what it's doing under the hood - you'd need to read the source code or scour the documentation.

当尝试在两种系统/语言之间进行互操作时,最好让您保持对事情的负责,而不是让一端做出武断的决定.这样您就可以确保两端的设置相同.

When trying to inter-operate between two systems/languages it's best if you remain in charge of things, rather than letting one end make arbitrary decisions. That way you can make sure that you have the settings the same at each end.

因此,您可以选择:

  • 使用 PBKDF2 从字符串中生成 128 位密钥和 128 位 IV密码 - 例如,9999 轮
  • 使用 PKCS#7 填充
  • 在 CBC 模式下使用 AES

pointycastle 包在 Dart 中支持上述所有内容.看起来 CryptoJS 也支持所有这些.

The pointycastle package supports all the above in Dart. It looks like CryptoJS supports all of those too.

从密码开始,确保您可以在 JS 和 Dart 中生成相同的密钥和 IV.然后继续创建密码.

Start with a passphrase and make sure you can generate the same key and IV in JS and Dart. Then move onto creating the ciphers.

也请记住,永远不要用相同的密钥/IV 对加密两条消息.例如,使用消息序列号稍微更改每条消息的 IV.

Remember, too, never to encrypt two messages with the same key/IV pair. Use a message sequence number, for example, to slightly change the IV for each message.

这篇关于在 Flutter 上解密 AES 输入,在 web 上使用 cryptoJS AES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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