我有一个c#Web API,它将公钥传递给Reaction应用程序.我需要使用该公钥来加密有效负载/消息,然后将其发送回我的API.然而,我在重建前端的公钥时遇到了麻烦.听我解释.
该应用程序不使用https,而只使用http.鉴于这种情况,我继续在c#中生成公钥和私钥,如下所示:
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
RSAParameters publicKey = rsa.ExportParameters(false);
RSAParameters privateKey = rsa.ExportParameters(true);
}
公钥为模(N)和指数(E)参数生成字节数组,可用于重建公钥(我已经在后端进行了测试).因此,我将n和e转换为Base64字符串,并将它们发送给客户端.
现在,在我的React应用程序中,我使用JSEncrypt库使用来自后端的公钥加密有效负载(example here).但它正在失败.以下是我的js:
const encryptMessage = (messageToEncrypt) => {
if (publicKeyParts?.length === 2) {
const encrypt = new JSEncrypt();
// publicKeyParts is a string[] with the exponent stored in index 0
// and the modulus stored in index 1
encrypt.setPublicKey(`${publicKeyParts[1]}${publicKeyParts[0]}`);
let data = {
payload: messageToEncrypt
}
let encryptedPayload = encrypt.encrypt(JSON.stringify(data));
return encryptedPayload;
}
}
加密的消息是假的,不起作用.我认为这与我的公钥格式有关.下面是从c#(并转换为Base64)生成的公钥:
8NHGP6WqPGgHa5Q+15V5z+SI7XAvIgMVjbNuGdmlvHoLY2kutP5u+m1lcJpcj49aVMFnoo0eJ5nBIMXVô1Qz0EpR3BreHp0X0Yfavf2E1lyIl5oyQFTpuzvMu0U3DebLvRezICn66CmFJxnsuERTM5NN
当我将其与其他公钥进行比较时,我注意到我的公钥具有在其他正常工作的公钥中看不到的字符(就像我上面链接的例子中的那个).
有没有我错过的转换?我已经搜索了网站,但看不到可能是什么问题.