aes-cbc-192-encrypt-decrypt.html 1.79 KB
Newer Older
ap@apple.com's avatar
ap@apple.com committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
description("Test AES-CBC encrypt and decrypt functions with a 256 bit key.");

jsTestIsAsync = true;

ap@apple.com's avatar
ap@apple.com committed
16 17 18
var keyData = hexStringToUint8Array("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b");
var iv = hexStringToUint8Array("000102030405060708090a0b0c0d0e0f");
var plaintext = hexStringToUint8Array("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710");
ap@apple.com's avatar
ap@apple.com committed
19 20 21 22 23 24 25 26 27 28 29 30
var extractable = true;

debug("Importing a raw AES key from string literal...");
crypto.subtle.importKey("raw", keyData, "aes-cbc", extractable, ["encrypt", "decrypt"]).then(function(result) {
    key = result;
    shouldBe("key.type", "'secret'");
    shouldBe("key.extractable", "true");
    shouldBe("key.algorithm.name", "'aes-cbc'");
    shouldBe("key.algorithm.length", "192");
    shouldBe("key.usages", "['encrypt', 'decrypt']");

    debug("Using the key to encrypt plaintext...");
31
    return crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, key, plaintext);
ap@apple.com's avatar
ap@apple.com committed
32 33
}).then(function(result) {
    encryptionResult = result;
ap@apple.com's avatar
ap@apple.com committed
34
    shouldBe("bytesToHexString(new Uint8Array(encryptionResult))", "'4f021db243bc633d7178183a9fa071e8b4d9ada9ad7dedf4e5e738763f69145a571b242012fb7ae07fa9baac3df102e008b0e27988598881d920a9e64f5615cd612ccd79224b350935d45dd6a98f8176'");
ap@apple.com's avatar
ap@apple.com committed
35 36

    debug("Decrypting it back...");
37
    return crypto.subtle.decrypt({name: "aes-cbc", iv: iv}, key, result);
ap@apple.com's avatar
ap@apple.com committed
38 39 40 41 42 43 44 45 46 47 48
}).then(function(result) {
    decryptionResult = result;
    shouldBe("new Uint8Array(decryptionResult)", "plaintext");

    finishJSTest();
});
</script>

<script src="../../resources/js-test-post.js"></script>
</body>
</html>