feat: use random IV for each request

This commit is contained in:
Vlad Stan
2022-08-12 16:52:55 +03:00
parent 19dfef6044
commit a32754610a
3 changed files with 29 additions and 138 deletions
@@ -184,9 +184,6 @@ function findAccountPathIssues(path = '') {
}
}
///////////////
let ivHex = '000102030405060708090a0b0c0d0e0f'
function asciiToUint8Array(str) {
var chars = []
for (var i = 0; i < str.length; ++i) {
@@ -195,124 +192,3 @@ function asciiToUint8Array(str) {
return new Uint8Array(chars)
}
async function encryptMessage(key, message) {
// The iv must never be reused with a given key.
// iv = window.crypto.getRandomValues(new Uint8Array(16));
iv = new Uint8Array([
0x00,
0x01,
0x02,
0x03,
0x04,
0x05,
0x06,
0x07,
0x08,
0x09,
0x0a,
0x0b,
0x0c,
0x0d,
0x0e,
0x0f
])
const ciphertext = await window.crypto.subtle.encrypt(
{
name: 'AES-CBC',
iv
},
key,
message
)
console.log('### #####################################')
return new Uint8Array(ciphertext)
}
async function encryptMessage2(key, message) {
// The iv must never be reused with a given key.
// iv = window.crypto.getRandomValues(new Uint8Array(16));
while (message.length % 16 !== 0) message += ' '
const encodedMessage = asciiToUint8Array(message)
iv = new Uint8Array([
0x00,
0x01,
0x02,
0x03,
0x04,
0x05,
0x06,
0x07,
0x08,
0x09,
0x0a,
0x0b,
0x0c,
0x0d,
0x0e,
0x0f
])
const aesCbc = new aesjs.ModeOfOperation.cbc(key, iv)
const encryptedBytes = aesCbc.encrypt(encodedMessage)
console.log('### #####################################')
return encryptedBytes
}
async function decryptMessage(key, ciphertext) {
iv = new Uint8Array([
0x00,
0x01,
0x02,
0x03,
0x04,
0x05,
0x06,
0x07,
0x08,
0x09,
0x0a,
0x0b,
0x0c,
0x0d,
0x0e,
0x0f
])
let decrypted = await window.crypto.subtle.decrypt(
{
name: 'AES-CBC',
iv
},
key,
ciphertext
)
return decrypted
}
async function decryptMessage2(key, encryptedBytes) {
iv = new Uint8Array([
0x00,
0x01,
0x02,
0x03,
0x04,
0x05,
0x06,
0x07,
0x08,
0x09,
0x0a,
0x0b,
0x0c,
0x0d,
0x0e,
0x0f
])
const aesCbc = new aesjs.ModeOfOperation.cbc(key, iv)
const decryptedBytes = aesCbc.decrypt(encryptedBytes)
console.log('### decryptedBytes', decryptedBytes)
return decryptedBytes
}