3

我正在使用 Web Crypto,更具体地说,这些示例:https ://github.com/diafygi/webcrypto-examples/#rsa-oaep

我的主要目标是用我的公钥加密一个字符串并用我的私钥解密它。

公钥加密效果很好,但是当我尝试用私钥解密加密字符串时,它返回以下错误:OperationError以及一个空字符串。

我正在使用以下功能:

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;
}

更新

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;
}
4

1 回答 1

3

您问题中包含的代码是正确的,因此问题将在隐藏部分中。我刚刚添加window.crypto.subtle.generateKey到您的代码中以生成 RSA-OAEP 密钥并且可以正常工作

请看完整的例子

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);
});
于 2017-01-10T08:28:28.643 回答