我有私钥(.pfx)和导出的公钥(.pem)证书 . 我可以使用私钥加密字符串,但无法使用public解密它 . 每当我得到“钥匙不存在” . 在这里,我尝试获取公钥 - 它不是空的 .

X509Certificate2 cert2 = new X509Certificate2("sem-cert-public.pem");
string a= cert.GetPublicKeyString();

但通过这种方式解密是行不通的 .

RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PublicKey.Key;
                    int keySize = rsa.KeySize;
                    int base64BlockSize = ((keySize / 8) % 3 != 0) ? (((keySize / 8) / 3) * 4) + 4 : ((keySize / 8) / 3) * 4;
                    int iterations = inputString.Length / base64BlockSize;
                    var arrayList = new ArrayList();
                    for (int i = 0; i < iterations; i++)
                    {
                        byte[] encryptedBytes = Convert.FromBase64String(
                            inputString.Substring(base64BlockSize * i, base64BlockSize));

//crashes here
                        arrayList.AddRange(rsa.Decrypt(encryptedBytes, false));
                    }

不知道麻烦在哪里 . 我确定公钥存在 .