async
function
pwdget(password) {
const encoder =
new
TextEncoder();
return
window.crypto.subtle.importKey(
"raw"
,
encoder.encode(password),
{ name:
"PBKDF2"
},
false
,
[
"deriveBits"
,
"deriveKey"
]
);
}
function
arrBfToB64(buffer) {
let
binary =
''
;
const bytes =
new
Uint8Array(buffer);
for
(
let
i = 0; i < bytes.length; i++) {
binary += String.fromCharCode(bytes[i]);
}
return
window.btoa(binary);
}
function
Encrypt() {
const plainText =
""
;
const password =
""
;
const iv =
""
;
const k = pwdget(password);
const key = window.crypto.subtle.deriveKey(
{ name:
"PBKDF2"
, salt:
new
TextEncoder().encode(
"hello-dasctf"
), iterations: 100000, hash:
'SHA-256'
},
k,
{ name:
"AES-GCM"
, length: 256 },
true
,
[
"encrypt"
,
"decrypt"
]
);
const encoder =
new
TextEncoder();
const encodedText = encoder.encode(plainText);
const ciphertext = window.crypto.subtle.encrypt(
{ name:
"AES-GCM"
, iv },
key,
encodedText
);
const base64Ciphertext = arrBfToB64(ciphertext);
}