如何解决无法将类型为“ Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair”的对象转换为类型为“ Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters”的对象
这里是从txt读取文件时工作正常的代码,但是当我从字符串中读取文件时,这里出现错误
public string encrypt(string plainText,string PrivateKey)
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
string filepath = path + "\\rsakeys\\pem_public.pem";
string localPath = new Uri(filepath).LocalPath;
PemReader pr = new PemReader(
(StreamReader)File.OpenText(localPath)
);
var reader = new StringReader(PrivateKey);
var pre = new PemReader(reader);
var o = pr.ReadObject();
var os = pre.ReadObject();
RsaKeyParameters keys = (RsaKeyParameters)os; >>> Here i am getting the error where os is the object readed from the string
解决方法
这解决了我的问题
var o = pr.ReadObject();
AsymmetricCipherKeyPair keyPair=(AsymmetricCipherKeyPair)o;
//As Keypair.public is RsaParamterKey
Pkcs1Encoding eng = new Pkcs1Encoding(new RsaEngine());
eng.Init(true,keyPair.Public);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。