JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/promiz/1.0.4/promiz.js"></script>
<script src="https://rawgithub.com/vibornoff/webcrypto-shim/master/webcrypto-shim.js"></script>
<script src="https://rawgithub.com/bitwiseshiftleft/sjcl/master/sjcl.js"></script>
JavaScript
var iv = base64DecodeToArray("iKZaX3K+X2g6RLxH2Iz2Rg==");
var wrappedKey = base64DecodeToArray("FWPt4AZDKJaN1KssHCBo+ZFSX/rKvh3eLMpOfHILsVYbV7WWSwX8NQ==");
var iterations = parseInt("100000");
var hashAlgorithm = "SHA-256";
var saltString = "d1YQ3JvtIjIdOJkkD7HHrEUnAixTSEz77xp5aGGWoXk=";
var cryptoAlgorithm = "AES-CBC";
getKEK().then(function (derivedKey) {
return crypto.subtle.unwrapKey(
'raw',
wrappedKey,
derivedKey,
{"name": "AES-KW"},
{name: cryptoAlgorithm, iv: iv},
true,
["encrypt", "decrypt"]
);
})
.then(function (key) {
var encryptedBinary = base64DecodeToArray("f3KxjbNrz0entEYWHnbBHg==");
return crypto.subtle.decrypt({ name: cryptoAlgorithm, iv: iv }, key, encryptedBinary);
})
.then(function (plainBinary) {
console.log("ok");
})
.catch(
function (err) {
console.error(err);
}
);
function getKEK() {
return new Promise(
function (resolve, reject) {
var passwordSalt = sjcl.codec.base64.toBits(saltString);
var derivedKey = sjcl.misc.pbkdf2("1q2w3e4r", passwordSalt, iterations, 256);
var arrayKey = sjcl.codec.base64.fromBits(derivedKey);
var subtle;
if (window.msCrypto) {
subtle = window.msCrypto.subtle;
} else {
subtle = window.crypto.subtle;
}
resolve(subtle.importKey(
"raw",
base64DecodeToArray(arrayKey),
{"name": "AES-KW"},
true,
['unwrapKey', "wrapKey"]));
}
);
}
function base64DecodeToArray(sBase64, nBlocksSize) {
var
sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length,
nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2, taBytes = new Uint8Array(nOutLen);
for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
nMod4 = nInIdx & 3;
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;
if (nMod4 === 3 || nInLen -...