【发布时间】:2017-07-03 19:26:13
【问题描述】:
我无法进行签名验证,就像here 所描述的那样。我正在使用BouncyCastle.NetCore 1.8.1.3,该项目是 .NETCoreApp 1.0。
我在 macOS 10.12.1 上进行开发,运行 dotnet core 1.0.4,我的服务器运行 Ubuntu 16.04.2-x64,运行发行版,构建为 netcore1.0 和 ubuntu16.04-x64 应用程序。
系统的其余部分运行没有问题,除了签名验证。
我的验证总是返回 false。
这是我验证签名和正文的服务:
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace MySkill.Main.Services
{
public class CertificationValidationService : ICertificationValidationService
{
private const string Algorithm = "SHA1withRSA";
public async Task<bool> IsValidSiganture(Stream body, Stream certData, string signature)
{
var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(new StreamReader(certData));
var cert = (Org.BouncyCastle.X509.X509Certificate)pemReader.ReadObject();
using (var sr = new StreamReader(body))
{
var content = await sr.ReadToEndAsync();
var result = CheckRequestSignature(Encoding.UTF8.GetBytes(content), signature, cert);
return result;
}
}
private static bool CheckRequestSignature(byte[] bodyData, string signature, Org.BouncyCastle.X509.X509Certificate cert)
{
byte[] sig = Convert.FromBase64String(signature);
var pubKey = (Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters)cert.GetPublicKey();
var signer = Org.BouncyCastle.Security.SignerUtilities.GetSigner(Algorithm);
signer.Init(false, pubKey);
signer.BlockUpdate(bodyData, 0, bodyData.Length);
return signer.VerifySignature(sig);
}
}
}
有没有人有提示,我做错了什么或使用了不同的框架或 api?
【问题讨论】:
-
1.您确定以 UTF8 编码的“内容”吗? 2. 你尝试过不同的算法吗?
-
1.是的,内容肯定是 UTF8。 2. 我尝试了 System.Security.Cryptography.X509Certificates 类。但它也不起作用。 dotnet 崩溃了。
标签: c# .net-core bouncycastle alexa