C# (.NET) RSACryptoServiceProvider import/export x509 public 密钥 blob 和 PKCS8 私钥 blob

C# (.NET) RSACryptoServiceProvider import/export x509 public key blob and PKCS8 private key blob

首先让我声明我不是加密专家,但我了解基础知识。

我希望能够:

  1. 从 X509 Public 密钥 blob 获取 RSACryptoServiceProvider 实例
  2. 从 PKCS8 私钥 blob 获取 RSACryptoServiceProvider 实例
  3. 从 RSACryptoServiceProvider 实例导出 public 密钥作为 x509 Public 密钥 blob
  4. 将私钥从 RSACryptoServiceProvider 实例导出为 PKCS8 blob

首先,Java 中的 byte 与 .Net 中的 byte 不同。 Java 只有有符号整数,所以 Java byte 的范围从 -128 到 127,而 .Net byte 的范围从 0 到 255。但我不确定,如果这是问题所在,因为 Base64 字符串依赖于位模式。尝试在其正范围内使用更大的类型,并且只使用低 8 位。

其次,有关错误提供程序版本的消息可能表明上述原因,或者可能只是误导性文本,这不会真正有帮助。我记得我曾经遇到过同样的错误信息并且很难找到真正的原因。几分钟前的快速搜索也没有成功。就我而言,我的情况完全不同,所以我无法直接指出你的问题。相反,我会建议检查填充、加密模式、base64 转换和 RSA 算法周围的相关内容,并尝试找到一种替代方法来实现与您之前想要的相同。在前往那里的路上,您可能会偶然发现实际问题。

环顾了一整天后,我找到了这个 repository (thanks a lot jrnker) 并且我选择了能够实现目标 1、2 和 3 所需的代码。

由于 Jrnker 仅提供从 PKCS1 blob 获取 RSACryptoServiceProvider 的方法(而我需要的是从 PKCS8 blob 获取 RSACryptoServiceProvider)我一直在寻找实现目标 4。然后我发现 Michel Gallant's "opensslkey.cs"我选择了实现目标 4 所需的代码。

然后我开始用需要的方法和 classes 编译 class。

这是我的演示 class:

using System;

namespace RSAKeyTests
{
    class Demo
    {
        static void Main(string[] args)
        {
            //EXPORTED KEYS
            string importedPublicKeyBase64 = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhbVC4aUR+XRCepBcPlod69wruXqwW9yL/YJYvuaQ33QxUoAehQ0z4SuphHwEP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FgGTgRAKTs6sG9E1Cf3LNBLDP9OhfRkXFc192PmQRAktaZAN89zXeGxK1tLbJ3003qKXw05K3KOksVjJ7AH4Yhurv3VWmFZB8pryUsxIp+rm/5GLf4LfptUmBO6R4+jTfJVRBtK4A9KmkbY7BjgwKBgQCwPWayTgd0fmDqJxptWfPThcUw3/cG6EWTpnx1dSOdaBHzewRwq/8/i4vs314/onLggXgZTIkPU7y8ylTmz5KcaPIQkmRSSSL0Y2yzMGcHnylj7ysgBLw23k/PVzGSsMZ6ly7lE03SNQ3tyg6u0lc3pbT8ZLHf/x913stxSSiT4QKBgQChdgnKmZRqhS1WSGGSP3pZCJNFY9HTeLijaQqFOFB3hg/Tp37VDv2MMKCQsbi0z13UnP4glrQAehbbCBixQiMzMIx+ldx3UIEWNN4E3TGAwPROiCIJnY0q4rBxg/SgwgftBvF5oU4X2YluZuQ/1ddZ4ya0jq4oQ9jJgL9+kKKsJwKBgQCndbBfPEVZK7xqwT0bKp3EHxd/mU/gAFQcN9WKxgNRTdHAyOMvLD8c4jvSl2u2i2UcbejwIQkaxzZPLPH/XrywYgegN3mbtmLAVLi0iwla9KEfk+ImSlmMyTCMkw1HlTECyySEBhOr6T2S9Kt+8d5twcZ3DDb34DLEjS5CNoGYAQKBgDCEyhrg2lwyYwrL26ohNNuzgiabC5IKCgHlMpsUQjoCid9awCSb2iROf7iZIBoDyzXqgEQWTAf2clpJxgHz0necVw2sXP8wGcJXJ+e/lXNfPaC4z2QRnQ6i2iV88jRlWLK+S403hGnK0L/SDu9LtBhHwy6r/qRGT14ourqS6x7O";
            byte[] importedPublicKeyBytes = Convert.FromBase64String(importedPublicKeyBase64);
            byte[] importedPrivateKeyBytes = Convert.FromBase64String(importedPrivateKeyBase64);

            //PRINT INFO
            Console.WriteLine("------   IMPORTED KEY PAIR:   ------\n");
            Console.WriteLine("PUBLIC KEY:\n"+importedPublicKeyBase64+"\n\n");
            Console.WriteLine("PRIVATE KEY:\n" + importedPrivateKeyBase64 + "\n\n");

            //GENERATING RSACRYPTOSERVICEPROVIDER FROM X509 PUBLIC KEY BLOB
            using (var providerFromX509pubKey = RSAKeyUtils.DecodePublicKey(importedPublicKeyBytes))
            {
                providerFromX509pubKey.PersistKeyInCsp = false; //DO NOT STORE IN KEYSTORE

                //EXPORT TO X509 PUBLIC KEY BLOB
                byte[] x509pubKeyBytes = RSAKeyUtils.PublicKeyToX509(providerFromX509pubKey.ExportParameters(false));

                //CONVERT TO BASE64
                string x509pubKeyBase64 = Convert.ToBase64String(x509pubKeyBytes);

                //PRINT INFO
                Console.WriteLine("------   PUBLIC KEY TO EXPORT   ------");
                Console.WriteLine("Public key to export matches imported? "+importedPublicKeyBase64.Equals(x509pubKeyBase64));
                Console.WriteLine(x509pubKeyBase64+"\n\n");
            }

            //GENERATING RSACRYPTOSERVICEPROVIDER FROM PKCS8 PRIVATE KEY BLOB
            using (var providerFromPKCS8privKey = RSAKeyUtils.DecodePrivateKeyInfo(importedPrivateKeyBytes))
            {
                providerFromPKCS8privKey.PersistKeyInCsp = false; //DO NOT STORE IN KEYSTORE

                //EXPORT TO PKCS8 PRIVATE KEY BLOB
                byte[] pkcs8privKeyBytes = RSAKeyUtils.PrivateKeyToPKCS8(providerFromPKCS1privKey.ExportParameters(true));

                //CONVERT TO BASE64
                string pkcs8privKeyBase64 = Convert.ToBase64String(pkcs8privKeyBytes);

                //PRINT INFO
                Console.WriteLine("------   PRIVATE KEY TO EXPORT   ------");
                Console.WriteLine("Private key to export matches imported? " + importedPrivateKeyBase64.Equals(pkcs8privKeyBase64));
                Console.WriteLine(pkcs8privKeyBase64);
            }

                //PREVENTS THE PROGRAM FROM EXITING
                Console.ReadKey();
        }
    }
}

Here's the "RSAKeyUtils" class i've compiled.

我希望这对其他人有用。