gpt4 book ai didi

aes - WebCrypto AES-CBC 输出 256 位而不是 128 位

转载 作者:行者123 更新时间:2023-12-03 08:57:47 24 4
gpt4 key购买 nike

我正在使用 WebCrypto,但得到了令人困惑的输出。

以下测试用例使用新生成的 128 位 key 和 128 位随机 IV 加密随机 16 字节(128 位)纯文本,但输出 32 字节(256 位)输出。

如果我记得 AES-CBC 的详细信息,它应该输出 128 位 block 。

function test() { 

var data = new Uint8Array(16);
window.crypto.getRandomValues(data);
console.log(data)


window.crypto.subtle.generateKey(
{
name: "AES-CBC",
length: 128,
},
false,
["encrypt", "decrypt"]
)
.then(function(key){
//returns a key object
console.log(key);

window.crypto.subtle.encrypt(
{
name: "AES-CBC",
iv: window.crypto.getRandomValues(new Uint8Array(16)),
},
key,
data
)
.then(function(encrypted){
console.log(new Uint8Array(encrypted));
})
.catch(function(err){
console.error(err);
});
})
.catch(function(err){
console.error(err);
});

}

示例输出:

Uint8Array(16) [146, 207, 22, 56, 56, 151, 125, 174, 137, 69, 133, 36, 218, 114, 143, 174]
CryptoKey {
algorithm: {name: "AES-CBC", length: 128}
extractable: false
type: "secret"
usages: (2) ["encrypt", "decrypt"]
__proto__: CryptoKey
Uint8Array(32) [81, 218, 52, 158, 115, 105, 57, 230, 45, 253, 153, 54, 183, 19, 137, 240, 183, 229, 241, 75, 182, 19, 237, 8, 238, 5, 108, 107, 123, 84, 230, 209]

知道我做错了什么吗?

(如果更合适,可以迁移到 crypto.stackexchange.com)

我目前正在 MacOS 上的 Chrome 71 上进行测试。

最佳答案

是的。额外的 16 个字节是填充。即使消息文本是 block 大小的倍数,也会添加填充,否则解密逻辑不知道何时寻找填充。

Web Cryptography API Specification说:

When operating in CBC mode, messages that are not exact multiples ofthe AES block size (16 bytes) can be padded under a variety of paddingschemes. In the Web Crypto API, the only padding mode that issupported is that of PKCS#7, as described by Section 10.3, step 2, of[RFC2315].

这意味着与其他语言实现(如 Java)不同,当您知道输入消息文本始终是 block 大小(AES 为 128 位)的倍数时,您可以指定 NoPadding, Web 加密 API 强制您使用 PKCS#7 填充。

如果我们调查RFC2315 :

Some content-encryption algorithms assume the input length is amultiple of k octets, where k > 1, and let the application define amethod for handling inputs whose lengths are not a multiple of koctets. For such algorithms, the method shall be to pad the input atthe trailing end with k - (l mod k) octets all having value k - (l modk), where l is the length of the input. In other words, the input ispadded at the trailing end with one of the following strings:

     01 -- if l mod k = k-1
02 02 -- if l mod k = k-2
.
.
.
k k ... k k -- if l mod k = 0

The padding can be removed unambiguously since all input is padded andno padding string is a suffix of another. This padding method iswell-defined if and only if k < 256; methods for larger k are an openissue for further study.

注意:k k ... k k -- if l mod k = 0

如果引用subtle.encrypt签名,则无法指定填充模式。这意味着,解密逻辑始终需要填充。

但是,在您的情况下,如果您仅使用 Web Cryptography API 进行加密,而您的 Python 应用程序(使用 NoPadding)仅用于解密,我认为您可以简单地去掉最后 16 个字节密文,然后将其提供给 Python 应用程序。以下是仅用于演示目的的代码示例:

function test() { 

let plaintext = 'GoodWorkGoodWork';
let encoder = new TextEncoder('utf8');
let dataBytes = encoder.encode(plaintext);

window.crypto.subtle.generateKey(
{
name: "AES-CBC",
length: 128,
},
true,
["encrypt", "decrypt"]
)
.then(function(key){
crypto.subtle.exportKey('raw', key)
.then(function(expKey) {
console.log('Key = ' + btoa(String.
fromCharCode(...new Uint8Array(expKey))));
});

let iv = new Uint8Array(16);
window.crypto.getRandomValues(iv);
let ivb64 = btoa(String.fromCharCode(...new Uint8Array(iv)));
console.log('IV = ' + ivb64);

window.crypto.subtle.encrypt(
{
name: "AES-CBC",
iv: iv,
},
key,
dataBytes
)
.then(function(encrypted){
console.log('Cipher text = ' +
btoa(String.fromCharCode(...new Uint8Array(encrypted))));
})
.catch(function(err){
console.error(err);
});
})
.catch(function(err){
console.error(err);
});

}

上面的输出是:

IV = qW2lanfRo2H/3aSLzxIecA==
Key = 0LDBq5iz243HBTUE/lrM+A==
Cipher text = Wa4nIF0tt4PEBUChiH1KCkSOg6L2daoYdboEEf+Oh6U=

现在,我将这些作为输入,去掉密文的最后 16 个字节,并且在使用以下 Java 代码解密后仍然得到相同的消息文本:

package com.sapbasu.javastudy;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class EncryptCBC {
public static void main(String[] arg) throws Exception {

SecretKey key = new SecretKeySpec(Base64.getDecoder().decode(
"0LDBq5iz243HBTUE/lrM+A=="),
"AES");

IvParameterSpec ivSpec = new IvParameterSpec(Base64.getDecoder().decode(
"qW2lanfRo2H/3aSLzxIecA=="));

Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);

byte[] cipherTextWoPadding = new byte[16];
System.arraycopy(Base64.getDecoder().decode(
"Wa4nIF0tt4PEBUChiH1KCkSOg6L2daoYdboEEf+Oh6U="),
0, cipherTextWoPadding, 0, 16);

byte[] decryptedMessage = cipher.doFinal(cipherTextWoPadding);
System.out.println(new String(decryptedMessage, StandardCharsets.UTF_8));
}
}

关于aes - WebCrypto AES-CBC 输出 256 位而不是 128 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53765207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com