Exemplo n.º 1
0
        /// <summary>
        ///     加密数据
        /// </summary>
        public static string Encrypt(string text, string sKey)
        {
            var des            = new DESCryptoServiceProvider();
            var inputByteArray = Encoding.Default.GetBytes(text);

            des.Key = Encoding.ASCII.GetBytes(Md5EncryptHelper.Encrypt(sKey, 32).Substring(0, 8));
            des.IV  = Encoding.ASCII.GetBytes(Md5EncryptHelper.Encrypt(sKey, 32).Substring(0, 8));
            var ms = new MemoryStream();
            var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

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

            foreach (var b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return(ret.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        ///     解密数据
        /// </summary>
        /// <param name="Text">  </param>
        /// <param name="sKey">  </param>
        /// <returns>  </returns>
        public static string Decrypt(string Text, string sKey)
        {
            var des            = new DESCryptoServiceProvider();
            var len            = Text.Length / 2;
            var inputByteArray = new byte[len];
            int x;

            for (x = 0; x < len; x++)
            {
                var i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }

            des.Key = Encoding.ASCII.GetBytes(Md5EncryptHelper.Encrypt(sKey, 32).Substring(0, 8).Substring(0, 8));
            des.IV  = Encoding.ASCII.GetBytes(Md5EncryptHelper.Encrypt(sKey, 32).Substring(0, 8).Substring(0, 8));
            var ms = new MemoryStream();
            var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return(Encoding.Default.GetString(ms.ToArray()));
        }