Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using Web Crypto, more specifically these examples: https://github.com/diafygi/webcrypto-examples/#rsa-oaep

My main goal is to encrypt a string with my public key and decrypt it with my private key.

The public key encryption works well but when I try to decrypt the encrypted string with the private key, it returns the following error: OperationError and a empty string as well.

I'm using the following functions:

function encryptDataWithPublicKey(data, key) {
    data = stringToArrayBuffer(data);
    return window.crypto.subtle.encrypt(
        name: "RSA-OAEP",
        //label: Uint8Array([...]) //optional
    key, //from generateKey or importKey above
    data //ArrayBuffer of data you want to encrypt
function decryptDataWithPrivateKey(data, key) {
    data = stringToArrayBuffer(data);
    return window.crypto.subtle.decrypt(
            name: "RSA-OAEP",
            //label: Uint8Array([...]) //optional
        key, //from generateKey or importKey above
        data //ArrayBuffer of data you want to encrypt
function stringToArrayBuffer(str){
    var buf = new ArrayBuffer(str.length);
    var bufView = new Uint8Array(buf);
    for (var i=0, strLen=str.length; i<strLen; i++) {
        bufView[i] = str.charCodeAt(i);
    return buf;

UPDATE

var data = "example";
encryptDataWithPublicKey(data, publicKey).then((result) => {
    var rdata = arrayBufferToString(result);
    return decryptDataWithPrivateKey(rdata, privateKey).then((result) => {
        var result = arrayBufferToString(result);
function arrayBufferToString(str){
    var byteArray = new Uint8Array(str);
    var byteString = '';
    for(var i=0; i < byteArray.byteLength; i++) {
        byteString += String.fromCodePoint(byteArray[i]);
    return byteString;
                you say you are encrypting the data, then decrypting it ... you've shown the functions that do that, but you haven't shown how you use them - perhaps you are using them wrong (i.e. what are you doing with the return value from your encrypt/decrypt functions)
– Jaromanda X
                Jan 10, 2017 at 1:36
                The encryptDataWithPublicKey works well, but I can't decrypt it with the other function. I can't understand why.
– urb
                Jan 10, 2017 at 1:38
                yes, so you already said in the question ... but I asked you to show how you are using your functions, because I suspect you are doing it wrong, I didn't ask this to waste your time or anything
– Jaromanda X
                Jan 10, 2017 at 1:40
                Yeah, I get the feeling the issue is in arrayBufferToString and stringToArrayBuffer conversion ... but I can't put a finger on it
– Jaromanda X
                Jan 10, 2017 at 1:51
                Most systems encode the cipher text from raw bytes to hexadecimal or Base64 encoding for transportability and stability (you'll get a "safe" string like 54686973206973206120706c61696e74657874206d6573736167652e0a or VGhpcyBpcyBhIHBsYWludGV4dCBtZXNzYWdlLgo=). Then just decode that string before decrypting the raw bytes.
– Andy
                Jan 10, 2017 at 3:52

The code included in your question is correct, so the issue will be in the hidden part. I just added window.crypto.subtle.generateKey to your code to generate the RSA-OAEP keys and works

Please, take a look to the full example

function stringToArrayBuffer(str){
    var buf = new ArrayBuffer(str.length);
    var bufView = new Uint8Array(buf);
    for (var i=0, strLen=str.length; i<strLen; i++) {
        bufView[i] = str.charCodeAt(i);
    return buf;
function arrayBufferToString(str){
    var byteArray = new Uint8Array(str);
    var byteString = '';
    for(var i=0; i < byteArray.byteLength; i++) {
        byteString += String.fromCodePoint(byteArray[i]);
    return byteString;
function encryptDataWithPublicKey(data, key) {
    data = stringToArrayBuffer(data);
    return window.crypto.subtle.encrypt(
        name: "RSA-OAEP",
        //label: Uint8Array([...]) //optional
    key, //from generateKey or importKey above
    data //ArrayBuffer of data you want to encrypt
function decryptDataWithPrivateKey(data, key) {
    data = stringToArrayBuffer(data);
    return window.crypto.subtle.decrypt(
            name: "RSA-OAEP",
            //label: Uint8Array([...]) //optional
        key, //from generateKey or importKey above
        data //ArrayBuffer of data you want to encrypt
window.crypto.subtle.generateKey(
        name: "RSA-OAEP",
        modulusLength: 2048,
        publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
        hash: {name: "SHA-256"}
    true,
    ["encrypt", "decrypt"]
).then(function(keyPair) {
    var data = "example";
    encryptDataWithPublicKey(data, keyPair.publicKey).then((result) => {
        var rdata = arrayBufferToString(result);
        return decryptDataWithPrivateKey(rdata, keyPair.privateKey).then((result) => {
            var result = arrayBufferToString(result);
            console.log(result);
}).catch (function (err){
    console.log(err);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.