使用 window.crypto.subtle API 解密来自浏览器的 AES-CTR 256 位消息
Decrypt a AES-CTR 256 bits message from browser with window.crypto.subtle APIs
我需要在浏览器中解密使用 AES-CTR 256 位编码(使用 OpenSSL 编码)的消息。
使用 OpenSSL 我得到类似的东西:
key=189BBBB00C5F1FB7FBA9AD9285F193D1771D7611CB891E5C1F4E24C20E50FB1D
iv =4103C88663AE12CE18EA46E894280C4D
msg=nhVKeu8zNO2PRTwJrDE=
好吧,我的问题是将这些字符串转换为 window.crypto.subtle
API 可以管理的对象。例如
const counter = ???;
const ciphertext = ???;
const rawKey = ???;
const key = window.crypto.subtle.importKey(
"raw",
key,
"AES-CTR",
true,
["encrypt", "decrypt"]
);
const decrypted = await window.crypto.subtle.decrypt(
{
name: "AES-CTR",
counter,
length: 64
},
key,
ciphertext
);
let dec = new TextDecoder();
const msg = dec.decode(decrypted);
console.log(msg);
谁能帮我从key
、iv
、msg
转到counter
、ciphertext
、rawkey
?
非常感谢
密钥、计数器(或 IV)和密文可以作为 TypedArray
传递,即您需要两次转换,一次从十六进制转换,第二次从 Base64 编码字符串转换为 TypedArray
,例如
来自十六进制编码的字符串,:
const fromHex = hexString => new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
来自 Base64 编码的字符串,here:
const fromBase64 = base64String => Uint8Array.from(atob(base64String), c => c.charCodeAt(0));
代码本身缺少一个 await
运算符,在 importKey
函数中必须使用 rawKey
而不是 key
(可能是 copy/paste 错误).总计:
const fromHex = hexString => new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
const fromBase64 = base64String => Uint8Array.from(atob(base64String), c => c.charCodeAt(0));
async function test(){
const rawKey = fromHex("189BBBB00C5F1FB7FBA9AD9285F193D1771D7611CB891E5C1F4E24C20E50FB1D");
const counter = fromHex("4103C88663AE12CE18EA46E894280C4D");
const ciphertext = fromBase64("nhVKeu8zNO2PRTwJrDE=");
const key = await window.crypto.subtle.importKey( // add >await<
"raw",
rawKey, // replace >key< with >rawKey<
"AES-CTR",
true,
["encrypt", "decrypt"]
);
const decrypted = await window.crypto.subtle.decrypt(
{
name: "AES-CTR",
counter,
length: 64
},
key,
ciphertext
);
let dec = new TextDecoder();
const msg = dec.decode(decrypted);
console.log(msg);
}
test();
这会将密文解密为:
hello, world!
我需要在浏览器中解密使用 AES-CTR 256 位编码(使用 OpenSSL 编码)的消息。
使用 OpenSSL 我得到类似的东西:
key=189BBBB00C5F1FB7FBA9AD9285F193D1771D7611CB891E5C1F4E24C20E50FB1D
iv =4103C88663AE12CE18EA46E894280C4D
msg=nhVKeu8zNO2PRTwJrDE=
好吧,我的问题是将这些字符串转换为 window.crypto.subtle
API 可以管理的对象。例如
const counter = ???;
const ciphertext = ???;
const rawKey = ???;
const key = window.crypto.subtle.importKey(
"raw",
key,
"AES-CTR",
true,
["encrypt", "decrypt"]
);
const decrypted = await window.crypto.subtle.decrypt(
{
name: "AES-CTR",
counter,
length: 64
},
key,
ciphertext
);
let dec = new TextDecoder();
const msg = dec.decode(decrypted);
console.log(msg);
谁能帮我从key
、iv
、msg
转到counter
、ciphertext
、rawkey
?
非常感谢
密钥、计数器(或 IV)和密文可以作为 TypedArray
传递,即您需要两次转换,一次从十六进制转换,第二次从 Base64 编码字符串转换为 TypedArray
,例如
来自十六进制编码的字符串,
const fromHex = hexString => new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
来自 Base64 编码的字符串,here:
const fromBase64 = base64String => Uint8Array.from(atob(base64String), c => c.charCodeAt(0));
代码本身缺少一个 await
运算符,在 importKey
函数中必须使用 rawKey
而不是 key
(可能是 copy/paste 错误).总计:
const fromHex = hexString => new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
const fromBase64 = base64String => Uint8Array.from(atob(base64String), c => c.charCodeAt(0));
async function test(){
const rawKey = fromHex("189BBBB00C5F1FB7FBA9AD9285F193D1771D7611CB891E5C1F4E24C20E50FB1D");
const counter = fromHex("4103C88663AE12CE18EA46E894280C4D");
const ciphertext = fromBase64("nhVKeu8zNO2PRTwJrDE=");
const key = await window.crypto.subtle.importKey( // add >await<
"raw",
rawKey, // replace >key< with >rawKey<
"AES-CTR",
true,
["encrypt", "decrypt"]
);
const decrypted = await window.crypto.subtle.decrypt(
{
name: "AES-CTR",
counter,
length: 64
},
key,
ciphertext
);
let dec = new TextDecoder();
const msg = dec.decode(decrypted);
console.log(msg);
}
test();
这会将密文解密为:
hello, world!