jose-jwe-jws
Version:
Library to encrypt and decrypt data in JSON Web Encryption (JWE) format and to sign data in JSON Web Signature (JWS) format. Leverages Browser's native web crypto API.
35 lines (30 loc) • 1.18 kB
HTML
<html>
<body>
<p>plain text: <span id="plaintext">hello world, ⛄</span></p>
<p>cipher text: <pre style="word-wrap: break-word" id="ciphertext"></pre></p>
<p>error: <span id="error" style="color: red"></span></p>
<script src="../dist/jose.js"></script>
<script>
var cryptographer = new Jose.WebCryptographer();
cryptographer.setKeyEncryptionAlgorithm("A128KW");
cryptographer.setContentEncryptionAlgorithm("A128CBC-HS256");
var shared_key = {"kty":"oct", "k":"GawgguFyGrWKav7AX4VKUg"};
shared_key = crypto.subtle.importKey("jwk", shared_key, {name: "AES-KW"}, true, ["wrapKey", "unwrapKey"]);
var encrypter = new Jose.JoseJWE.Encrypter(cryptographer, shared_key);
encrypter.encrypt(plaintext.textContent).then(function(result) {
ciphertext.textContent = result;
var decrypter = new Jose.JoseJWE.Decrypter(cryptographer, shared_key);
decrypter.decrypt(result)
.then(function(decrypted_plain_text) {
if (decrypted_plain_text != plaintext.textContent) {
error.textContent = "decryption failed!";
}
}).catch(function(err) {
error.textContent = err;
});
}).catch(function(err) {
error.textContent = err;
});
</script>
</body>
</html>