Thank you Jeffrey Goldberg!
@jpgoldberg on Twitter
Cyphertext
KEY
Plaintext
A long random sequence of characters has high entropy, a short human memorable password has low entropy.
const key = await window.crypto.subtle.generateKey(
{
name: "AES-GCM",
length: 256
},
true,
["encrypt", "decrypt"]
);AES-GCM
Advanced Encryption Standard
with Galois Counter Mode
const note = "A very secret note";
const noteBuffer = new TextEncoder("utf-8").encode(note);
const nonce = window.crypto.getRandomValues(new Uint8Array(12));
const cyphertext = await window.crypto.subtle.encrypt(
{
name: "AES-GCM",
iv: nonce
},
key,
noteBuffer
); const plaintext = await window.crypto.subtle.decrypt(
{
name: "AES-GCM",
iv: nonce
},
key,
cyphertext
);
const decoder = new TextDecoder("utf-8")
const decryptedNote = decoder.decode(new Uint8Array(plaintext))
console.log(decryptedNote); // A very secret note const password = "The user's secret password"
const enc = new TextEncoder();
const passwordAsKey = await window.crypto.subtle.importKey(
"raw",
enc.encode(password),
{name: "PBKDF2"},
false,
["deriveBits", "deriveKey"]
);PBKDF2
Password-Based Key Derivation Function 2
const salt = window.crypto.getRandomValues(new Uint8Array(12));
const key = await window.crypto.subtle.deriveKey(
{
name: "PBKDF2",
salt: salt,
iterations: 100000,
hash: "SHA-256"
},
passwordAsKey,
{ name: "AES-GCM", length: 256},
true,
[ "encrypt", "decrypt" ]
);
Password
Salt
Cyphertext
Key
Plaintext
Password
Salt
Plaintext
Key
Cyphertext
Password
Salt
Cyphertext
Key
Plaintext
Password
Salt
Plaintext
Key
Cyphertext
Cyphertext
KEY
A
Plaintext
KEY
B
Cyphertext
KEY
B
Plaintext
KEY
A
Cyphertext
Public
KEY
Plaintext
Private
KEY
const keyPair = window.crypto.subtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 4096,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256"
},
true,
["encrypt", "decrypt"]
);Password
Salt
Encrypted
Private KEY
Key
Private
KEY
Twitter: @raae
Instagram: @raae.codes