首页 > 解决方案 > 为什么更改语言时会出现此 RSA 填充错误?

问题描述

所以我试图用 RSA 加密 C# 中的数据,并用 JavaScript 也用 RSA 解密它。

这是我用来在 C# 中加密数据的函数:

private static byte[] Encrypt(byte[] input, bool server)
{
    byte[] encrypted;
    using (var rsa = new RSACryptoServiceProvider(4096))
    {
        rsa.PersistKeyInCsp = false;
        if (server)
        {
            rsa.ImportParameters(ImportPublicKey(Main.PUBLIC_KEY_SERVER).ExportParameters(false));
        }
        else
        {
            rsa.ImportParameters(publicKey);
        }
        encrypted = rsa.Encrypt(input, true);
    }
    return encrypted;
}

这是我用来解密 JavaScript 数据的代码:

function decryptMessage(encryptedMessage, privateKey) {
    console.log(encryptedMessage);
    const rsaPrivateKey = {
        key: privateKey,
        passphrase: '',
        padding: crypto.constants.RSA_PKCS1_OAEP_PADDING
    };

    const decryptedMessage = crypto.privateDecrypt(
        rsaPrivateKey,
        Buffer.from(encryptedMessage, 'base64'),
    );

    return decryptedMessage.toString('utf8');
}

现在的问题是我总是得到错误: Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error

我不知道为什么。出于某种原因,加密填充似乎不再相同。有没有办法解决这个问题?

〜路易

标签: javascriptc#encryptionrsa

解决方案


推荐阅读