此代码不创建HMAC签名。我必须通过C#.NET创建HMAC签名,我该如何创建?

时间:2014-04-04 06:52:16

标签: c# hmac hmacsha1

这是我的代码,我应该使用什么格式的输入字符串或输入字符串的实际格式是什么?什么应该是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;
        }
    }
}

我按照Martin的说法尝试过,现在我收到了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();

        }

    }
}

1 个答案:

答案 0 :(得分: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, ...