gpt4 book ai didi

javascript - 使用 RSA OAEP 加密和解密

转载 作者:行者123 更新时间:2023-11-29 21:11:40 29 4
gpt4 key购买 nike

我正在使用 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;
}

最佳答案

你的问题中包含的代码是正确的,因此该问题将出现在隐藏部分。我刚刚将 window.crypto.subtle.generateKey 添加到您的代码中以生成 RSA-OAEP key 并运行

请看完整的例子

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

关于javascript - 使用 RSA OAEP 加密和解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41559807/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com