Created
February 26, 2023 09:20
-
-
Save ilzrv/6182dc09bae050bfc7448c8c358ea48c to your computer and use it in GitHub Desktop.
Example AES-GCM encode-decode with string key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sources: | |
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey | |
// https://github.com/mdn/dom-examples/blob/main/web-crypto/encrypt-decrypt/aes-gcm.js | |
// https://github.com/diafygi/webcrypto-examples#aes-gcm | |
let enc = new TextEncoder(); | |
let dec = new TextDecoder(); | |
(async function () { | |
additionalData = window.crypto.getRandomValues(new Uint8Array(32)) | |
iv = window.crypto.getRandomValues(new Uint8Array(12)); | |
key = await window.crypto.subtle.importKey( | |
"raw", | |
enc.encode("barracuda radish amenity pregame"), | |
"AES-GCM", | |
true, | |
["encrypt", "decrypt"] | |
) | |
function encrypt(key, text) { | |
return window.crypto.subtle.encrypt( | |
{ | |
name: "AES-GCM", | |
//Don't re-use initialization vectors! | |
//Always generate a new iv every time your encrypt! | |
//Recommended to use 12 bytes length | |
iv: iv, | |
//Additional authentication data (optional) | |
additionalData: additionalData, | |
//Tag length (optional) | |
tagLength: 128, //can be 32, 64, 96, 104, 112, 120 or 128 (default) | |
}, | |
key, //from generateKey or importKey above | |
enc.encode(text) //ArrayBuffer of data you want to encrypt | |
) | |
} | |
function decrypt(key, text) { | |
return window.crypto.subtle.decrypt( | |
{ | |
name: "AES-GCM", | |
iv: iv, //The initialization vector you used to encrypt | |
additionalData: additionalData, //The addtionalData you used to encrypt (if any) | |
tagLength: 128, //The tagLength you used to encrypt (if any) | |
}, | |
key, //from generateKey or importKey above | |
text //ArrayBuffer of the data | |
) | |
} | |
function arrayBufferToBase64(buffer) { | |
var binary = ''; | |
var bytes = new Uint8Array(buffer); | |
var len = bytes.byteLength; | |
for (var i = 0; i < len; i++) { | |
binary += String.fromCharCode(bytes[i]); | |
} | |
return window.btoa(binary); | |
} | |
function base64ToArrayBuffer(base64) { | |
var binary_string = window.atob(base64); | |
var len = binary_string.length; | |
var bytes = new Uint8Array(len); | |
for (var i = 0; i < len; i++) { | |
bytes[i] = binary_string.charCodeAt(i); | |
} | |
return bytes.buffer; | |
} | |
let encryptedBuffer = await encrypt(key, "Message For Encode"); // ArrayBuffer Encrypted Message | |
let encryptedText = arrayBufferToBase64(encryptedBuffer); // Base64 Encrypted Message | |
let arrayBufferEncrypted = base64ToArrayBuffer(encryptedText); // ArrayBuffer Encrypted Message | |
let decryptedBuffer = await decrypt(key, arrayBufferEncrypted); // ArrayBuffer Decrypted Message | |
let decryptedText = dec.decode(decryptedBuffer); | |
console.log('Encrypted: ' + encryptedText) | |
console.log('Decrypted: ' + decryptedText) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment