c# - Alexa 技能签名验证失败

标签 c# .net-core bouncycastle alexa

我无法使签名验证正常工作,就像描述的那样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/

相关文章:

c# - 在 .net 核心中配置依赖注入(inject)

c# - 如何使用 JSONConvert 将变量 Result 转换为对象?

java - Bouncy caSTLe 分离签名更改 API

c# - Blowfish 引擎的 Bouncy CaSTLe CTS 模式未按预期工作

c# - 尽管答案是正确的,但为什么我的代码输出 "Incorrect"?

c# - 我应该将哪些参数传递给 ViewModel 的构造函数?

c# - 将 JSON 数据发布到 ASP.NET 应用程序

c# - 套接字行为 - 乱序写入的数据

c# - WPF 控件库目标 .NET Core 3 或 .NET Framework?

java - 如何使用 BouncycaSTLe Java 将 PGP 公钥存储和读取为字符串?