简体   繁体   English

在 C# 中加密时,为什么在 node.js 中 RSA 解密失败?

[英]Why does RSA decryption fail in node.js when encrypted in C#?

I am creating a C# app that authenticates via a node.js server.我正在创建一个通过 node.js 服务器进行身份验证的 C# 应用程序。 I am using RSA for this purpose.我为此目的使用 RSA。 I generated a public and private key for the server using crypto.我使用加密为服务器生成了一个公钥和私钥。 Every time the client connects to the server, it generates a key pair for itself.每次客户端连接到服务器时,它都会为自己生成一个密钥对。 The client gets the server public key from an endpoint.客户端从端点获取服务器公钥。 I have used XML strings as well as PEM strings, but neither of them worked.我使用过 XML 字符串以及 PEM 字符串,但它们都不起作用。 (using RSACryptoServiceProvider) When the server attempted to decrypt it, it threw an OAEP decoding error. (使用 RSACryptoServiceProvider)当服务器试图解密它时,它抛出了一个 OAEP 解码错误。 I am trying to decrypt the message with the paired private key.我正在尝试使用配对的私钥解密消息。

I have viewed other threads but they were not very helpful.我查看了其他线程,但它们不是很有帮助。

Here's the code for the server.这是服务器的代码。 It encrypts/decrypts with the built-in crypto module.它使用内置的加密模块加密/解密。 (I have tested this with a node.js client and a node.js server, and it works.) (我已经用 node.js 客户端和 node.js 服务器对此进行了测试,它可以工作。)


var encrypt = function(input, publicKey) {
    var buffer = Buffer.from(input);
    var encrypted = crypto.publicEncrypt(publicKey, buffer);
    return encrypted.toString("base64");
};

var decrypt = function(input, privateKey) {
    var buffer = Buffer.from(input, "base64");
    var decrypted = crypto.privateDecrypt(privateKey, buffer);
    return decrypted.toString("utf8");
};

module.exports = {
    encrypt,
    decrypt
}

Edit: I made a test C# console app that takes an input string and encrypts it with my node.js server's public key.编辑:我做了一个测试 C# 控制台应用程序,它接受输入字符串并使用我的 node.js 服务器的公钥对其进行加密。

        public const string pubKey = "<RSAKeyValue>public key etc etc</RSAKeyValue>";
        private static void Main(string[] args)
        {
            string enc = encrypt(pubKey, args[0]);
            Console.WriteLine(enc);
        }

        public static string encrypt(string publicKey, string decrypted)
        {
            RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
            csp.FromXmlString(publicKey);
            byte[] bytesPlainTextData = Encoding.UTF8.GetBytes(decrypted);
            byte[] bytesCipherText = csp.Encrypt(bytesPlainTextData, false);
            string cipherText = Convert.ToBase64String(bytesCipherText);
            return cipherText;
        }

It gave me the result VnzRc4yhIa9XcSDvHyDkwCNHFG6Ps2dddyCD4RHE4jIqvMl56DhmIJWprLRZle9EpZ/3Zq4fDkkplHUGBidoH+9VkPV/2+sV6P+C+4u6yisV5zTarZfjcvsShwBp/9z4YfOE7kQZVRENhvflrRw6GutxtDz0lO4KhvdvQztm0u7JmUB9ynM7XFYXOKT391InBs2eqRh+JRfJzTfhFqn3Bt8K/kKNE1xkvQV0GK7U1qSpWOWfB+0hdwNkUEQpT26jU93bAcex1SVwfbj4PJQMH6Wxzx2s6u4fcOzf9ELEgel/Fuj5b0UKHHE48B/zBmnoDsS3twt/8TJb9jbCU8S3ES/hKwndkS809bSoJl6TkBXErlOLCDpay3AO23+NjPGwSl1JvnFUVgTqAABd/yAcsokjIgxkbRqAvhC/js5Oh3y9wJwc9Z7V1ImPGcifIWsEBuH/8lerJdYw7ABB/eUZosC+tQkzvjr4H9urupM0mk6Zd+92sJaG/COrwOAPkiiM6lJK9ealRrlPMEKv39aWVr+brlQzN8zyoT+a0oGsYSPt9B/P3CJhbkbHqw9e1u9TZ7q9Ba7x/oqeRBmpRfFrcjegGFQkYViYkd1bswNF3KumqhBCsw4VeTkYmRNCKrLZdZyJ5BLSfvc+PTPOzDPVgOZb1InacmIWOqkapRbeELc= It gave me the result VnzRc4yhIa9XcSDvHyDkwCNHFG6Ps2dddyCD4RHE4jIqvMl56DhmIJWprLRZle9EpZ/3Zq4fDkkplHUGBidoH+9VkPV/2+sV6P+C+4u6yisV5zTarZfjcvsShwBp/9z4YfOE7kQZVRENhvflrRw6GutxtDz0lO4KhvdvQztm0u7JmUB9ynM7XFYXOKT391InBs2eqRh+JRfJzTfhFqn3Bt8K/kKNE1xkvQV0GK7U1qSpWOWfB+0hdwNkUEQpT26jU93bAcex1SVwfbj4PJQMH6Wxzx2s6u4fcOzf9ELEgel/Fuj5b0UKHHE48B/zBmnoDsS3twt/8TJb9jbCU8S3ES/hKwndkS809bSoJl6TkBXErlOLCDpay3AO23+NjPGwSl1JvnFUVgTqAABd/yAcsokjIgxkbRqAvhC/js5Oh3y9wJwc9Z7V1ImPGcifIWsEBuH/8lerJdYw7ABB/eUZosC+tQkzvjr4H9urupM0mk6Zd+92sJaG/COrwOAPkiiM6lJK9ealRrlPMEKv39aWVr+brlQzN8zyoT+a0oGsYSPt9B/P3CJhbkbHqw9e1u9TZ7q9Ba7x/oqeRBmpRfFrcjegGFQkYViYkd1bswNF3KumqhBCsw4VeTkYmRNCKrLZdZyJ5BLSfvc+PTPOzDPVgOZb1InacmIWOqkapRbeELc=

Then, I did a simple console.log(decrypt(stringAbove, privateKey));然后,我做了一个简单的console.log(decrypt(stringAbove, privateKey));

It still gives me the following error: Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error它仍然给我以下错误: Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error

There are multiple types of padding, and apparently the encryption is trying to use PKCS1 (I guess), and the decryption defaults to OAEP.有多种类型的填充,显然加密正在尝试使用 PKCS1(我猜),而解密默认为 OAEP。

In crypto.privateDecrypt you can set the padding to eg.crypto.privateDecrypt ,您可以将填充设置为例如。 padding: crypto.constants.RSA_PKCS1_PADDING and it should work. padding: crypto.constants.RSA_PKCS1_PADDING ,它应该可以工作。

You should go for OAEP on both ends if possible (and it should be), in which case your Node code is already ok as the default is OAEP, and C# should be set to OAEP too.如果可能(并且应该是),您应该在两端为 OAEP go,在这种情况下,您的节点代码已经可以,因为默认值为 OAEP,并且 C# 也应该设置为 OAEP。

Edit: I mixed it up first, but the point is, you can set the padding type on either end, and they must match.编辑:我先把它弄混了,但重点是,您可以在任一端设置填充类型,并且它们必须匹配。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM