如何在.Net C中验证Nodejs加密签名的json文件数据#

时间:2017-08-27 15:58:15

标签: c# node.js asp.net-core

我有一个项目,其中.NET核心Web应用程序需要使用加密lib和私钥来验证由Nodejs应用程序签名的json文件。该文件包含以下json对象。请帮忙指导。谦虚的谢谢:

    {
   "info": {
    "validto": 1514678400000,
    "validfrom": 1498608000000,
    "AllowedDevices":15,
    "email": "eample.com",
    "name": "john williams",
    "phone": "123456789",
    "address": "example road"
  },
  "signature": {
    "data": [
      159,
      1,
      92,
      151,
      110,
      33,
      93,
      239,
      243,...
      ],
    "type": "Buffer"
  }
}

公钥格式

-----BEGIN RSA PUBLIC KEY-----\nMIIBCgKCAQEAqao1ZkAYKDybHSeoy79ySQDcXODByDRaZKT2nYwT8-----END RSA PUBLIC KEY-----

我试图在.Net中使用RSACryptoServiceProvider,它会返回错误的数据,公钥不像RSACryptoServiceProvider()所期望的那样,或者我没有做正确的

  public static bool VerifyData(License license, string signedMessage, RSAParameters publicKey)
    {
        bool success = false;
        using (var rsa = new RSACryptoServiceProvider())
        {
            var encoder = new UTF8Encoding();
            byte[] bytesToVerify = encoder.GetBytes(license);

            byte[] signedBytes = Convert.FromBase64String(signedMessage);
            try
            {
                rsa.ImportParameters(publicKey);

                SHA256Managed Hash = new SHA256Managed();

                byte[] hashedData = Hash.ComputeHash(signedBytes);

                success = rsa.VerifyData(bytesToVerify, CryptoConfig.MapNameToOID("SHA256"), signedBytes);
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                rsa.PersistKeyInCsp = false;
            }
        }
        return success;
    }

**有一个nodejs应用程序正在以下列方式验证文件,但我需要在asp.net核心中进行,我甚至不知道它是否可能**

// veriying file, taking values from file "license" and constructing these 

objects to verify

const infoString = [
        license.info.name,
        license.info.address,
        license.info.phone,
        license.info.email,
        license.info.nodes.toString(),
        new Date(license.info.validfrom).toString(),
        new Date(license.info.validto).toString(),
    ];

    const infoMS = [
        license.info.name,
        license.info.address,
        license.info.phone,
        license.info.email,
        license.info.nodes.toString(),
        license.info.validfrom,
        license.info.validto,
    ];

    const verifyString = crypto.createVerify('RSA-SHA256'), verifyMS = crypto.createVerify('RSA-SHA256'), licenseInfoString = JSON.stringify(infoString), licenseInfoMS = JSON.stringify(infoMS);
// Currently the signature must be a buffer and the type field is ignored.


 let buf;
    try {
        buf = new Buffer(license.signature.data);
    } catch (parseError) {
        return false;
    }

0 个答案:

没有答案