0

这是我的代码,我应该使用什么格式的输入字符串或输入字符串的实际格式是什么?prn号码应该是多少?或者我们可以假设任何 10 位数字作为 prn 来获得输出?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Web;

namespace HMCApp2
{
    class Sha1HashProgram
    {
        static void Main(string[] args)
        {
            string key = "<my private key>";
          //  string data = "'mrj'.'json'.'539ff0f815ca697c681fe01d32ba52e3'";

            string data = "json539ff0f815ca697c681fe01d32ba52e31234567890";
            string secret = Sha1HashProgram.ShaHash(data, key);
            Console.WriteLine(secret);
            Console.ReadKey();
        }

        static string ShaHash(string value, string key)
        {
            using (var hmac = new HMACSHA1(Encoding.ASCII.GetBytes(key)))
            {
                return ByteToString(hmac.ComputeHash(Encoding.ASCII.GetBytes(value)));
            }
        }

        static string ByteToString(IEnumerable<byte> data)
        {
            return string.Concat(data.Select(b => b.ToString("x2")));
        }
    }
}

我也以另一种方式尝试过,但仍然无法正常工作,这是我的另一个代码,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        public static string Encode(string input, byte[] key)
        {
            HMACSHA1 myhmacsha1 = new HMACSHA1(key);
            byte[] byteArray = Encoding.ASCII.GetBytes(input);
            MemoryStream stream = new MemoryStream(byteArray);
            return myhmacsha1.ComputeHash(stream).Aggregate("", (s, e) => s + String.Format("{0:x2}", e), s => s);
        }


        static void Main(string[] args)
        {
            byte[] key = Encoding.ASCII.GetBytes("my private key");
            string input = "";
    //        foreach (string s in new string[] { "Marry", " had", " a", " little", " //lamb" })
    //        {
     //           input ="'mrj'.'json'.'539ff0f815ca697c681fe01d32ba52e3'";
                input = "json539ff0f815ca697c681fe01d32ba52e31234567890";
                System.Console.WriteLine(Encode(input, key));
   //         }
            return;
        }
    }
}

我按照马丁所说的那样尝试过,现在我得到了 HMAC 签名,但是当我将它粘贴到 URL 时它没有向我显示 json,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Web;
using System.Globalization;
using System.Runtime.Remoting.Metadata.W3cXsd2001;

namespace HMCApp2
{
    class Sha1HashProgram
    {
        static void Main(string[] args)
        {
            string key = "<my private key>";

         //   string data = "json539ff0f815ca697c681fe01d32ba52e31234567890";
            string data = "539ff0f815ca697c681fe01d32ba52e3";
            string secret = Sha1HashProgram.ShaHash(data, key);
            Console.WriteLine(secret);
 //           Console.ReadKey();
        }

        static string ShaHash(string value, string key)
        {
           // using (var hmac = new HMACSHA1(Encoding.ASCII.GetBytes(key)))
            using (var hmac = new HMACSHA1(StringToBytes(key)))
            {
               // return ByteToString(hmac.ComputeHash(Encoding.ASCII.GetBytes(value)));
                return ByteToString(hmac.ComputeHash(StringToBytes(value)));
            }
        }

        static string ByteToString(IEnumerable<byte> data)
        {
            return string.Concat(data.Select(b => b.ToString("x2")));
        }

        static Byte[] StringToBytes(String hexString)
        {

            return Enumerable.Range(0, hexString.Length / 2).Select(i => (Byte)Int32.Parse(hexString.Substring(2 * i, 2), NumberStyles.HexNumber)).ToArray();

        }

    }
}
4

1 回答 1

0

正如 Anton Tykhyy 在评论中指出的那样,该代码Encoding.ASCII.GetBytes不能用于将十六进制数字字符串转换为字节。相反,您可以使用此函数(如果输入不是有效的十六进制字符串,它将引发各种异常):

Byte[] StringToBytes(String hexString) {
  return Enumerable
    .Range(0, hexString.Length/2)
    .Select(i => (Byte) Int32.Parse(hexString.Substring(2*i, 2), NumberStyles.HexNumber))
    .ToArray();
}

调用StringToBytes("539ff0f815ca697c681fe01d32ba52e31234567890")将返回一个字节数组:0x53, 0x9F, 0xF0, ....

于 2014-04-04T07:56:39.203 回答