0

I have a project where .NET core Web App needs to verify a json file signed by Nodejs app using crypto lib and private key. The file contains the following json objects. Please help in guidance. Humble thanks :

    {
   "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"
  }
}

public key format

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

I tried to use RSACryptoServiceProvider in .Net, it return bad data, either the public key isnt as RSACryptoServiceProvider() is expecting or the I am not doing it the right

  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;
    }

**There is a nodejs app that is verifying the file in the following way but I need to do it in asp.net core, i dont even know if its possible **

// 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;
    }
2
  • Make sure your Node code converts the signedMessage to base64 and then make sure you can convert it back to a regular string in C#. Thats the first part that seems to not match up. Aug 27, 2017 at 18:14
  • Following is the node app encrypting using private key const sign = crypto.createSign('RSA-SHA256'); sign.update(JSON.stringify(arrayInfo)); const sig = sign.sign(privateKey); const LicenseModel = this; const licenseDoc = new LicenseModel({ info: object, signature: { type: 'Buffer', data: Array.prototype.slice.call(sig, 0), }, });
    – Rohit
    Aug 27, 2017 at 19:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.