为 RSACryptoProvider 和 BouncyCastle 生成 key/Encryption/Decryption

Generate key/Encryption/Decryption for RSACryptoProvider and BouncyCastle

通过 RSACryptoProvider 生成的密钥适用于 BouncyCastle 加密(使用公钥)/解密(使用私钥)?

 using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
        {
            var pu = rsa.ToXmlString(false);               
            var pr = rsa.ToXmlString(true);               
        }

另外,如何使用 BouncyCastle 生成密钥?

第一个问题的答案,是的,RSA 是一个标准,它不依赖于所使用的库。

其次,试试这个:

    public static void GetRsaKeyPair(out string privateXml, out string publicXml)
    {
        CryptoApiRandomGenerator randomGenerator = new CryptoApiRandomGenerator();
        SecureRandom secureRandom = new SecureRandom(randomGenerator);
        var keyGenerationParameters = new KeyGenerationParameters(secureRandom, 1024);

        var rsaKeyPairGenerator = new RsaKeyPairGenerator();
        rsaKeyPairGenerator.Init(keyGenerationParameters);
        AsymmetricCipherKeyPair rsaKeyPair = rsaKeyPairGenerator.GenerateKeyPair();

        var privateRsaParameters = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)rsaKeyPair.Private);

        using (RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider())
        {
            rsaProvider.ImportParameters(privateRsaParameters);
            privateXml = rsaProvider.ToXmlString(true);
            publicXml = rsaProvider.ToXmlString(false);
        }
    }