Importing RSA Key and Encrypting March 2017 Using Java Script with Web Crypto API Importing the RSA Key Example 1 Java Script: Importing the RSA Key window.crypto.subtle.importKey( "jwk", { //this is an example jwk key, other key types are Uint8Array objects kty: "RSA", e: "AQAB", n: "vGO3eU16ag9zRkJ4AK8ZUZrjbtp5xWK0LyFMNT8933evJoHeczexMUzSiXaLrEFSyQZortk81zJH3y41MBO_UFDO_X0crAquNrkjZDrf9Scc5‐MdxlWU2Jl7Gc4Z18AC9aNibWVmXhgvHYkEoFdLCFG‐alg: "RSA-OAEP-256", ext: true }, { //these are the algorithm options name: "RSA-OAEP", hash: {name: "SHA-256"} }, false, // key is not extractable ["encrypt"] // key usage ) .then(function(publicKey){ //returns a publicKey (or privateKey if you are importing a private key) console.log(publicKey); }) .catch(function(err){ console.error(err); }); Encrypting Using the Imported Key Example 2 Java Script: Encrypting using the Imported RSA Key window.crypto.subtle.encrypt( { name: "RSA-OAEP", }, publicKey, //from importKey above data //ArrayBuffer of data you want to encrypt ) .then(function(encrypted){ //returns an ArrayBuffer containing the encrypted data console.log(new Uint8Array(encrypted)); }) .catch(function(err){ console.error(err); }); Using Java Importing the Key and Encrypting Example 3 Java: Importing the RSA Key and Encrypting import java.security.KeyFactory; import java.security.PublicKey; import java.security.spec.X509EncodedKeySpec; import java.util.Base64; import javax.crypto.Cipher; // decodedKeyMaterial is byte array containg DER encoded public key PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decodedKeyMaterial)); // Please note the usage of BouncyCastle library. The SUN implementaion is not compatible !!! Cipher oaepFromAlgo = Cipher.getInstance("RSA/None/OAEPWithSHA256AndMGF1Padding", "BC"); oaepFromAlgo.init(Cipher.ENCRYPT_MODE, publicKey); byte[] cipherText = oaepFromAlgo.doFinal(plainText.getBytes()); String encoded = Base64.getEncoder().encodeToString(cipherText); // this value can be passed to Flex