解决方案 »

  1.   

    看看这些讨论:http://stackoverflow.com/questions/10410420/implementing-perls-pack-function-in-net
    http://stackoverflow.com/questions/18122041/is-there-an-equivalent-in-c-sharp-to-pack-and-unpack-from-perl
      

  2.   

      private static byte[] strToToHexByte(string hexString)
            {
                hexString = hexString.Replace(" ", "");
                if ((hexString.Length % 2) != 0)
                    hexString += " ";
                byte[] returnBytes = new byte[hexString.Length / 2];
                for (int i = 0; i < returnBytes.Length; i++)
                    returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
                return returnBytes;
            } public static string Encode(string keyStr, string sourceStr)
            {
                byte[] key = strToToHexByte(keyStr);
                
         
                HMACSHA1 hmac = new HMACSHA1(key);
                // Convert the input string to a byte array and compute the hash. 
                byte[] data = hmac.ComputeHash(Encoding.ASCII.GetBytes(sourceStr));
           
                // Create a new Stringbuilder to collect the bytes 
                // and create a string.
                StringBuilder sBuilder = new StringBuilder();            // Loop through each byte of the hashed data  
                // and format each one as a hexadecimal string. 
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }                        return sBuilder.ToString();
            }我认为我上面的程序已和perl的相同了,为什么得出来的结果不样,请大家帮看看 用perl跑出来的是46f058768347c2aa67311576fdbc09a6bbb70423
    用c#
      challenge = "27c81c82d52252c2fd0567e859df3c31";
            string login_params = "res=success&challenge={0}";
            login_params = string.Format(login_params, challenge);
            //string portal_secret = "AirTight";
            string portal_secret = "ABCD";        string digest = tool.Encode(portal_secret, login_params);算出来的却是7f06024932cfd3143ed9c4adfb2fe0a58646b97e请大家看看为什么
      

  3.   

    PERL的参数和返回结果和C#的一样吗?
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Security.Cryptography;
    using System.Security;
    namespace 9989889
    {
        class Program
        {
            public static byte[] PackH(string hex)
            {
                if ((hex.Length % 2) == 1) hex += '0';
                byte[] bytes = new byte[hex.Length / 2];
                for (int i = 0; i < hex.Length; i += 2)
                {
                    bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
                }
                return bytes;
            }
            public static string Encode(string keyStr, string sourceStr)
            {
                byte[] key = PackH(keyStr);
                HMACSHA1 hmac = new HMACSHA1(key);
                // Convert the input string to a byte array and compute the hash. 
                byte[] data = hmac.ComputeHash(Encoding.ASCII.GetBytes(sourceStr));            // Create a new Stringbuilder to collect the bytes 
                // and create a string.
                StringBuilder sBuilder = new StringBuilder();            // Loop through each byte of the hashed data  
                // and format each one as a hexadecimal string. 
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }            return sBuilder.ToString();
            }
            static void Main(string[] args)
            {
                string challenge = "27c81c82d52252c2fd0567e859df3c31";
                string login_params = "res=success&challenge={0}";
                login_params = string.Format(login_params, challenge);
                //string portal_secret = "AirTight";
                string portal_secret = "ABCD";            string digest = Encode(portal_secret, login_params);
            }
        }
    }
      

  5.   

    还是一样的,你那个packh 函数和我的是一样的,所以结果和我的是一样但与perl执行的不样,