我无法使签名验证正常工作,就像描述的那样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?
最佳答案
从您分享的代码来看,它看起来是正确的。在没有看到其余代码的情况下,我不确定您获得的签名是否正确。您可以在https://github.com/sophtron/Alexa-Skill-Sophtron查看我们通过认证的C#代码。并与您自己的进行比较,看看是否有任何差异。
我尝试使用 .net 库中的 RSACryptoServiceProvider 进行签名验证,但它从未奏效。所以我也不得不切换到 BouncyCaSTLe.1.8.1,它对我有用。
关于c# - Alexa 技能签名验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44892713/