예제 #1
0
        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加密密钥,要求为8位</param>
        /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
        public static string Encrypt(string encryptString, string encryptKey, bool compress)
        {
            try
            {
                string rnd            = encryptKey.Substring(0, 8);
                byte[] rgbKey         = Encoding.UTF8.GetBytes(rnd);
                byte[] rgbIV          = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(compress ? Compress.EnCompress(encryptString) : encryptString);
                var    dCSP           = new DESCryptoServiceProvider();
                var    mStream        = new MemoryStream();
                var    cStream        = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                byte[] msbty;
                msbty = mStream.ToArray();
                var ret = new StringBuilder();
                foreach (var b in msbty)
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                ret.ToString();
                return(rnd + (compress ? "1" : "0") + ret.ToString());
            }

            catch
            {
                return(encryptString);
            }
        }
예제 #2
0
        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
        public static string Decrypt(string decryptString, string decryptKey, bool compress)
        {
            if (compress)
            {
                decryptString = Compress.DeCompress(decryptString);
            }
            var inputByteArray = new byte[decryptString.Length / 2];

            for (int x = 0; x < decryptString.Length / 2; x++)
            {
                int i = (Convert.ToInt32(decryptString.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }
            var btys    = inputByteArray;
            var rgbKey  = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
            var rgbIV   = Keys;
            var DCSP    = new DESCryptoServiceProvider();
            var mStream = new MemoryStream();
            var cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);

            cStream.Write(btys, 0, btys.Length);
            cStream.FlushFinalBlock();
            return(Encoding.UTF8.GetString(mStream.ToArray()));
        }