C# 使用RSA/ECB/PKCS1加密使用.cert公钥添加加密

C# 使用RSA/ECB/PKCS1加密使用.cert公钥添加加密,c#,encryption,bouncycastle,C#,Encryption,Bouncycastle,我们正在尝试使用RSA/ECB/PKCs1代码和.cert publickey对代码进行加密,但是我们失败了,并得到一个错误“不是有效的RSAKey” public string RsaEncryptWithPublic(string clearText, string publicKey) { publicKey = GetCertificate().PublicKey.Key.ToXmlString(false

我们正在尝试使用RSA/ECB/PKCs1代码和.cert publickey对代码进行加密,但是我们失败了,并得到一个错误“不是有效的RSAKey”

        public string RsaEncryptWithPublic(string clearText, string publicKey)
        {

           
            publicKey = GetCertificate().PublicKey.Key.ToXmlString(false);
               
                        var bytesToEncrypt = Encoding.UTF8.GetBytes(clearText);

            var encryptEngine = new Pkcs1Encoding(new RsaEngine());

            using (var txtreader = new StringReader(publicKey))
            {
                var keyParameter = (AsymmetricKeyParameter)new PemReader(txtreader).ReadObject();

                encryptEngine.Init(true, keyParameter);
            }

            var encrypted = Convert.ToBase64String(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));
            return encrypted;

        }