Exemplo n.º 1
0
        /// <summary>
        /// 解密数据
        /// </summary>
        /// <param name="Text"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string Decrypt(string Text, string sKey)
        {
            var des = Aes.Create();
            int len;

            len = Text.Length / 2;
            byte[] inputByteArray = new byte[len];
            int    x, i;

            for (x = 0; x < len; x++)
            {
                i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }
            string md5   = Md5.Create(sKey, 32);
            string md5X4 = md5.Substring(16);

            des.Key = ASCIIEncoding.ASCII.GetBytes(md5X4);
            des.IV  = ASCIIEncoding.ASCII.GetBytes(md5X4);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream           cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return(Encoding.ASCII.GetString(ms.ToArray()));
        }
Exemplo n.º 2
0
        /// <summary>
        /// create sign -- base
        /// </summary>
        /// <param name="apiKey"></param>
        /// <param name="secretKey"></param>
        /// <param name="paramDict"></param>
        /// <returns></returns>
        private Dictionary <string, string> CreateSign_Base(string apiKey, string secretKey, Dictionary <string, string> paramDict)
        {
            try
            {
                paramDict.Remove("sign");

                var newDict = paramDict.OrderBy(item => item.Key);
                var buffer  = new StringBuilder();
                var i       = 0;
                foreach (var s in newDict)
                {
                    buffer.AppendFormat(i > 0 ? "&{0}={1}" : "{0}={1}", s.Key, s.Value);
                    i++;
                }

                var sk = paramDict.Where(c => c.Key == "secret_key");

                if (!sk.Any())
                {
                    buffer.Append($"&secret_key={secretKey}");
                }

                var sign = Md5.Create(buffer.ToString(), 32);
                buffer.Clear();

                paramDict.Add("sign", sign);

                return(paramDict);
            }
            catch (Exception ex)
            {
                var mess = new StringBuilder();
                mess.AppendLine("sign error :");
                mess.AppendLine(ex.Message);
                Log.Error(mess);
                return(null);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 加密数据
        /// </summary>
        /// <param name="Text"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string Encrypt(string Text, string sKey)
        {
            var des = Aes.Create();

            byte[] inputByteArray;
            inputByteArray = Encoding.ASCII.GetBytes(Text);
            string md5   = Md5.Create(sKey, 32);
            string md5X4 = md5.Substring(16);

            des.Key = ASCIIEncoding.ASCII.GetBytes(md5X4);
            des.IV  = ASCIIEncoding.ASCII.GetBytes(md5X4);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream           cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();

            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return(ret.ToString());
        }