const key = await window.crypto.subtle.generateKey({
name: 'RSA-OAEP',
modulusLength: 2048, // can be 1024, 2048, or 4096
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {name: 'SHA-256'},
},
false, // key is extractable?
['encrypt', 'decrypt']
);
// returns a keypair object
console.log(key);
console.log(key.publicKey);
console.log(key.privateKey);
const data = 'Je suis Iron Man';
const encrypted = await window.crypto.subtle.encrypt({
name: 'RSA-OAEP',
},
publicKey, // public key of the recipient
str2ab(data) //ArrayBuffer of data you want to encrypt
);
console.log(encrypted);
const decrypted = await window.crypto.subtle.decrypt({
name: 'RSA-OAEP',
},
privateKey, //Private key of the recipient
encrypted // ArrayBuffer of the data
);
// Affiche "Je suis Iron Man"
console.log(ab2str(decrypted));
Twitter: @pierregillesl, @gladysassistant