예제 #1
0
        /// <summary>
        /// DES加密,向量随机生成
        /// </summary>
        /// <param name="plaintext">明文</param>
        /// <returns></returns>
        public static string DesEncrypt(string plaintext)
        {
            //随机生成8位向量
            string iv = GenerateRandom();

            byte[] rgbKey = System.Text.Encoding.ASCII.GetBytes("SolinArt");
            byte[] rgbIV  = System.Text.Encoding.ASCII.GetBytes(iv);
            byte[] _data  = System.Text.Encoding.UTF8.GetBytes(plaintext);

            System.Security.Cryptography.DESCryptoServiceProvider _des     = new System.Security.Cryptography.DESCryptoServiceProvider();
            System.Security.Cryptography.ICryptoTransform         _encrypt = _des.CreateEncryptor(rgbKey, rgbIV);
            byte[] _result = _encrypt.TransformFinalBlock(_data, 0, _data.Length);
            _encrypt.Dispose();
            _des.Clear();

            string result = BitConverter.ToString(_result);

            result = iv + result.Replace("-", "");

            //首尾各加16位共计32位的MD5校验码
            #region 32位的MD5校验码
            //string _md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(result, "MD5");
            string _md5 = Md5(result);
            result = string.Format("{0}{1}{2}"
                                   , _md5.Substring(0, 16)
                                   , result
                                   , _md5.Substring(16)
                                   );
            #endregion

            return(result);
        }
예제 #2
0
        /// <summary>
        /// DES解密
        /// </summary>
        public static string DesDecrypt(string encrypted)
        {
            //小于40位时,不是有效的加密串
            if (encrypted.Length <= 40)
            {
                return("");
            }
            else if (encrypted.Length % 2 != 0)
            {
                return("");  //非偶数位不是有效的加密串
            }
            //32位MD5校验码
            string _md5 = encrypted.Substring(0, 16);

            _md5 += encrypted.Substring(encrypted.Length - 16);
            //移除MD5
            encrypted = encrypted.Substring(16);
            encrypted = encrypted.Substring(0, encrypted.Length - 16);

            //进行MD5验证是否被修改
            //if (_md5 != System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(encrypted, "MD5"))
            if (_md5 != Md5(encrypted))
            {
                return("");
            }

            //截取,移除前8位向量长度
            string iv = encrypted.Substring(0, 8);

            //移除向量值
            encrypted = encrypted.Substring(8);

            //32位整型转换器
            System.ComponentModel.Int32Converter _int32 = new System.ComponentModel.Int32Converter();
            //字符串转换成数组
            byte[] _datas = new byte[encrypted.Length / 2];
            for (int i = 0; i < _datas.Length; i++)
            {
                _datas[i] = Convert.ToByte(_int32.ConvertFromInvariantString("0x" + encrypted.Substring(i * 2, 2)));
            }

            byte[] rgbKey = System.Text.Encoding.ASCII.GetBytes("SolinArt");
            byte[] rgbIV  = System.Text.Encoding.ASCII.GetBytes(iv);

            System.Security.Cryptography.DESCryptoServiceProvider _des     = new System.Security.Cryptography.DESCryptoServiceProvider();
            System.Security.Cryptography.ICryptoTransform         _encrypt = _des.CreateDecryptor(rgbKey, rgbIV);
            byte[] _result = _encrypt.TransformFinalBlock(_datas, 0, _datas.Length);
            _encrypt.Dispose();
            _des.Clear();
            return(System.Text.Encoding.UTF8.GetString(_result));
        }