I need to decrypt in the browser a message encoded with AES-CTR 256 bits (encoded using OpenSSL).
Using OpenSSL I get something like:
key=189BBBB00C5F1FB7FBA9AD9285F193D1771D7611CB891E5C1F4E24C20E50FB1D
iv =4103C88663AE12CE18EA46E894280C4D
msg=nhVKeu8zNO2PRTwJrDE=
Well, my problem is converting those strings into objects the window.crypto.subtle APIs can manage. Eg.
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);
Could anyone help me passing from key, iv, msg to counter, ciphertext, rawkey?
Thank you very much
Key, counter (or IV) and ciphertext can be passed as
TypedArray, i.e. you need two conversions, one from a hexadecimal, and a second from a Base64 encoded string into aTypedArray, e.g.from a hexadecimal encoded string, here:
from a Base64 encoded string, here:
In the code itself an
awaitoperator is missing and in theimportKeyfunctionrawKeymust be used instead ofkey(probably copy/paste errors). Alltogether:This decrypts the ciphertext to: