Ejemplo n.º 1
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         _crypto?.Dispose();
     }
 }
Ejemplo n.º 2
0
        //数据签名
        public static byte[] SignData(string key, byte[] data)
        {
            byte[] bytesKey = Encoding.UTF8.GetBytes(key);
            HMAC   alg      = new HMACMD5();

            //设置密钥
            alg.Key = bytesKey;
            //计算哈希值
            byte[] hash = alg.ComputeHash(data);
            alg.Dispose();

            //返回具有签名的数据(哈希值+数组本身)
            return(hash.Concat(data).ToArray());
        }
Ejemplo n.º 3
0
        //数据认证
        public static bool VerityData(string key, byte[] data)
        {
            byte[] bytesKey = Encoding.UTF8.GetBytes(key);
            HMAC   alg      = new HMACMD5();
            //提取收到的哈希值
            var receivedHash = data.Take(alg.HashSize >> 3);
            //提取数据本身
            var dataContent = data.Skip(alg.HashSize >> 3).ToArray();

            //设置密钥
            alg.Key = bytesKey;
            //计算数据哈希值和收到的哈希值
            var computedHash = alg.ComputeHash(dataContent);

            alg.Dispose();

            //如果相等则数据正确
            return(receivedHash.SequenceEqual(computedHash));
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 获取MD5
 /// </summary>
 /// <param name="text">文本</param>
 /// <param name="key">加密密钥</param>
 /// <returns></returns>
 public static string MD5_Encrypt(string text, string key)
 {
     try
     {
         byte[] dataToEncrypt = Encoding.UTF8.GetBytes(text);
         byte[] bytesKey      = Encoding.UTF8.GetBytes(key);
         HMAC   hmac          = new HMACMD5(bytesKey);
         byte[] hash          = hmac.ComputeHash(dataToEncrypt);
         hmac.Dispose();
         StringBuilder result = new StringBuilder();
         for (int i = 0; i < hash.Length; i++)
         {
             result.Append(hash[i].ToString("X2")); // hex format
         }
         return(result.ToString());
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         return(null);
     }
 }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            con("MachineKey Generator v0.1 for ASP.NET / xsiteman WebForms Application");
            con("");

            string _decKeyMode = "AES";
            string _hashMode   = "HMACSHA512";

            if (args.Length != 0)
            {
                if ((args.Length != 2) || (args[0] == "-h") || (args[0] == "--help"))
                {
                    error(-1);
                }

                if ((args[0] != "AES") && (args[0] != "DES") && (args[0] != "3DES"))
                {
                    error(1);
                }
                if ((args[1] != "MD5") && (args[1] != "SHA1") && (args[1] != "HMACSHA256") && (args[1] != "HMACSHA384") && (args[1] != "HMACSHA512"))
                {
                    error(2);
                }

                _decKeyMode = args[0];
                _hashMode   = args[1];

                con("FOUND OPTIONS: " + args[0] + ", " + args[1]);
            }
            else
            {
                con("USING DEFAULTS: AES + HMACSHA512");
            }

            con("");

            string _decKey;
            string _hashKey;

            switch (_decKeyMode)
            {
            case "3DES":
                TripleDESCryptoServiceProvider _3DES = new TripleDESCryptoServiceProvider();
                _3DES.GenerateKey();
                _decKey = BinToHexStr(_3DES.Key);
                _3DES.Dispose();
                break;

            case "DES":
                DESCryptoServiceProvider _DES = new DESCryptoServiceProvider();
                _DES.GenerateKey();
                _decKey = BinToHexStr(_DES.Key);
                _DES.Dispose();
                break;

            default:
                AesCryptoServiceProvider _AES = new AesCryptoServiceProvider();
                _AES.GenerateKey();
                _decKey = BinToHexStr(_AES.Key);
                _AES.Dispose();
                break;
            }

            switch (_hashMode)
            {
            case "MD5":
                HMACMD5 _MD5 = new HMACMD5();
                _hashKey = BinToHexStr(_MD5.Key);
                _MD5.Dispose();
                break;

            case "SHA1":
                HMACSHA1 _SHA1 = new HMACSHA1();
                _hashKey = BinToHexStr(_SHA1.Key);
                _SHA1.Dispose();
                break;

            case "SHA256":
                HMACSHA256 _SHA256 = new HMACSHA256();
                _hashKey = BinToHexStr(_SHA256.Key);
                _SHA256.Dispose();
                break;

            case "SHA384":
                HMACSHA384 _SHA384 = new HMACSHA384();
                _hashKey = BinToHexStr(_SHA384.Key);
                _SHA384.Dispose();
                break;

            default:
                HMACSHA512 _SHA512 = new HMACSHA512();
                _hashKey = BinToHexStr(_SHA512.Key);
                _SHA512.Dispose();
                break;
            }


            string _mkstring = string.Concat("<machineKey decryption=\"", _decKeyMode, "\" decryptionKey=\"", _decKey, "\" validation=\"", _hashMode, "\" validationKey=\"", _hashKey, "\" />");

            con(_mkstring);
        }