首页 > C# 如何使用OpenSSL生成的公钥秘钥对进行加密解密?

C# 如何使用OpenSSL生成的公钥秘钥对进行加密解密?

class RSA
    {
        static String pubKey = "MCwwDQYJKoZIhvcNAQEBBQADGwAwGAIRAO8EnwmDeL+OOSvkqKy5dIkCAwEAAQ==";
        static String priKey = "MGMCAQACEQDvBJ8Jg3i/jjkr5KisuXSJAgMBAAECEQDJhcPxaX/tQIOj7Io/sUQB\nAgkA92qTuJhxI7kCCQD3T3SHDsffUQIIbIW46b0j0zECCD0Kr4hmbY8BAgkAwBBk\n2nJozn4=";


        public static String Encrypt(String content)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            byte[] pubKeyBytes = Convert.FromBase64String(pubKey);
            rsa.ImportCspBlob(pubKeyBytes);
            byte[] resultBytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
            String result = Convert.ToBase64String(resultBytes);

            return result;
        }
        public static String Decrypt(String content)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            byte[] priKeyBytes = Convert.FromBase64String(priKey);
            rsa.ImportCspBlob(priKeyBytes);
            byte[] resultBytes = rsa.Decrypt(Convert.FromBase64String(content), false);
            String reuslt = Encoding.UTF8.GetString(resultBytes);
            return reuslt;
        }
    }

测试:String result = RSA.Encrypt("123"); 会在这一行:rsa.ImportCspBlob(pubKeyBytes); 报错:System.Security.Cryptography.CryptographicException: 不正确的提供程序版本。

请问正确的做法是怎么样的?


测试了C#自己生成的公钥密钥对是可以用的,但是ruby 用openssl生成的就不行. 反之 C#生成的 ruby的openssl库也不能用.

是参数设置的不对么

【热门文章】
【热门文章】