如何使用 .pem 文件中提供的公钥验证签名
本文关键字:公钥 验证 何使用 pem 文件 | 更新日期: 2024-11-05 04:25:49
如何使用 .pem 文件中提供的公钥验证签名?
我使用流动代码:
RSACryptoServiceProvider CrRsa;
var reader21 = File.OpenText(@"C:GTLpublicKey.pem");
var x = new PemReader(reader21);
var y = (RsaKeyParameters)x.ReadObject();
CrRsa = (RSACryptoServiceProvider)RSACryptoServiceProvider.Create();
RSAParameters pa = new RSAParameters();
pa.Modulus = y.Modulus.ToByteArray();
pa.Exponent = y.Exponent.ToByteArray();
CrRsa.ImportParameters(pa);
y
返回 null
,导致
pa.Modulus = y.Modulus.ToByteArray();
RSACryptoServiceProvider RSAVerifier = new RSACryptoServiceProvider();
//Read public Key From Text File.
StreamReader PubKeyReader = File.OpenText(txtPublicKeyFile.Text);
string publicKey = PubKeyReader.ReadToEnd();
//Adding public key to RSACryptoServiceProvider object.
RSAVerifier.FromXmlString(publicKey);
//Reading the Signature to verify.
FileStream Signature = new FileStream(txtVerifySign.Text, FileMode.Open, FileAccess.Read);
BinaryReader SignatureReader = new BinaryReader(Signature);
byte[] SignatureData = SignatureReader.ReadBytes((int)Signature.Length);
//Reading the Signed File for Verification.
FileStream Verifyfile = new FileStream(txtVerifyFile.Text, FileMode.Open, FileAccess.Read);
BinaryReader VerifyFileReader = new BinaryReader(Verifyfile);
byte[] VerifyFileData = VerifyFileReader.ReadBytes((int)Verifyfile.Length);
//Comparing.
bool isValidsignature = RSAVerifier.VerifyData(VerifyFileData, "SHA1", SignatureData);
if (isValidsignature)
{
Signature.Close();
Verifyfile.Close();
}
else
{
Signature.Close();
Verifyfile.Close();
}
我不同意提议的答案。
RSACryptoServiceProvider 无法通过"FromXMLString"读取 PEM 文件。
但是,它给了我将PEM文件导出为XML的想法。
我发现这个网站可以进行在线转换
然后它完美地工作了!
不确定,但可能是您忘记反斜杠的问题:
var reader21 = File.OpenText(@"C:'GTLpublicKey.pem");
// ^