【问题标题】:Alexa Skill Signature validation failingAlexa 技能签名验证失败
【发布时间】: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


【解决方案1】:

从您共享的代码来看,它看起来是正确的。在没有看到其余代码的情况下,我不确定您得到的签名是否正确。您可以在https://github.com/sophtron/Alexa-Skill-Sophtron查看我们通过认证的 C# 代码,并与您自己的代码进行比较,看看是否有任何差异。

我尝试使用 .net 库中的 RSACryptoServiceProvider 进行签名验证,但它从未奏效。所以我也不得不切换到 BouncyCastle.1.8.1,它对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 2017-11-28
    • 2013-10-28
    相关资源
    最近更新 更多