Web Crypto API RSA-OAEP Generate Keys and Perform Encryption/Decryption

by Anish

Posted on Friday september 21, 2018


This sample chapter extracted from the book, Cryptography for JavaScript Developers.


The Web crypto api RSA-OAEP algorithm identifier is used to perform encryption and decryption ordering to the RSAES-OAEP algorithm , using the SHA hash functions defined in this specification and using the mask generation function MGF1.

The Demo

This is the web cryptography api example of performing rsa oaep encryption decryption

Input Text
Output
Generated IV
RSA-OAEP Public Key (JWK) :
RSA-OAEP Private Key (JWK):

webcrypto api RSA-OAEP Javascript example

Generate the RSA-OAEP 2048 bit Keys using SHA-256 Algorithms

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"
                    }, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
                },
                true, //whether the key is extractable (i.e. can be used in exportKey)
                ["encrypt", "decrypt"] //must be ["encrypt", "decrypt"] or ["wrapKey", "unwrapKey"]
            )

Sample Export keys to display the RSA public private key in JWK format (You can skip this step) This is for demo purpose, which i showed in UI.

window.crypto.subtle.exportKey("jwk", key.publicKey).then(
                    function(keydata) {
                        publicKeyhold = keydata;
                        publicKeyJson = JSON.stringify(publicKeyhold);
                        document.getElementById("rsapublic").value = publicKeyJson;
                    }
                );

window.crypto.subtle.exportKey("jwk", key.privateKey).then(
                    function(keydata) {
                        privateKeyhold = keydata;
                        privateKeyJson = JSON.stringify(privateKeyhold);
                        document.getElementById("rsaprivate").value = privateKeyJson;
                    }
                );

RSA-OAEP Encryption Example

window.crypto.subtle.encrypt({
                            name: "RSA-OAEP",
                            iv: iv,
                        },
                        publicKey, //from generateKey or importKey above
                        asciiToUint8Array(plainText) //ArrayBuffer of data you want to encrypt
                    )
                    .then(function(encrypted) {
                        //returns an ArrayBuffer containing the encrypted data
                        document.getElementById("cipherText").value = bytesToHexString(encrypted);
                        document.getElementById("salt").value = bytesToHexString(iv);
                    })
                    .catch(function(err) {
                        console.error(err);
                    });

RSA-OAEP decryption Example

var cipherText = document.getElementById("cipherText").value;
    window.crypto.subtle.decrypt({
                name: "RSA-OAEP",
                iv: iv
            },
            privateKey, //from generateKey or importKey above
            hexStringToUint8Array(cipherText) //ArrayBuffer of the data
        )
        .then(function(decrypted) {
            alert(bytesToASCIIString(decrypted));
        })
        .catch(function(err) {
            console.error(err);
        });

Download the sample code here
Next Reading Perform AES Encryption Using importKey Method

Thanku for reading !!! Give a Share for Support

Asking for donation sound bad to me, so i'm raising fund from The Modern Cryptography CookBook for Just $9. Leanpub books Discount coupon first 100 reader. No hurry read the sample chapters here then decide.

Alternatively you can buy My all four Cryptography book Just $10.99


Referefce
  • The Modern Cryptography Book.
  • Python Cryptography
  • Cryptography for JavaScript Developer
  • Go lang Cryptography for developers


python Cryptography Topics
Topics
For Coffee/ Beer/ Amazon Bill and further development of the project Support by Purchasing, The Modern Cryptography CookBook for Just $9 Coupon Price

Cryptography for Python Developers

Cryptography for JavaScript Developers

Go lang ryptography for Developers

Here