当前位置: 首页>>代码示例>>C#>>正文


C# Cryptography.DESCryptoServiceProvider类代码示例

本文整理汇总了C#中System.Security.Cryptography.DESCryptoServiceProvider的典型用法代码示例。如果您正苦于以下问题:C# DESCryptoServiceProvider类的具体用法?C# DESCryptoServiceProvider怎么用?C# DESCryptoServiceProvider使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


DESCryptoServiceProvider类属于System.Security.Cryptography命名空间,在下文中一共展示了DESCryptoServiceProvider类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Decrypt

        public static string Decrypt(string sourceData)
        {
            // set key and initialization vector values
              byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
              byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
              try
              {
            // convert data to byte array
            byte[] encryptedDataBytes =
               Convert.FromBase64String(sourceData);

            // get source memory stream and fill it
            MemoryStream tempStream =
               new MemoryStream(encryptedDataBytes, 0,
              encryptedDataBytes.Length);

            // get decryptor and decryption stream
            DESCryptoServiceProvider decryptor =
               new DESCryptoServiceProvider();
            CryptoStream decryptionStream =
               new CryptoStream(tempStream,
              decryptor.CreateDecryptor(key, iv),
              CryptoStreamMode.Read);

            // decrypt data
            StreamReader allDataReader =
               new StreamReader(decryptionStream);
            return allDataReader.ReadToEnd();
              }
              catch
              {
            throw new StringEncryptorException(
               "Unable to decrypt data.");
              }
        }
开发者ID:moacap,项目名称:BalloonShop-2-MVC,代码行数:35,代码来源:StringEncryptor.cs

示例2: Encode

        public static string Encode(string str, string key)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();

            provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));

            provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));

            byte[] bytes = Encoding.UTF8.GetBytes(str);

            MemoryStream stream = new MemoryStream();

            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);

            stream2.Write(bytes, 0, bytes.Length);

            stream2.FlushFinalBlock();

            StringBuilder builder = new StringBuilder();

            foreach (byte num in stream.ToArray())
            {

                builder.AppendFormat("{0:X2}", num);

            }

            stream.Close();

            return builder.ToString();
        }
开发者ID:MrNor,项目名称:WxMp,代码行数:31,代码来源:CryptHelper.cs

示例3: Encrypt

        public static string Encrypt( string strEncryptString, string strEncryptionKey)
        {
            byte[] inputByteArray;

            try
            {
                key = Encoding.UTF8.GetBytes(strEncryptionKey.Substring(0, 8));

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Encoding.UTF8.GetBytes(strEncryptString);

                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);

                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();

                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception eX)
            {

                throw eX;
            }
        }
开发者ID:hugohcn,项目名称:sge_webapp,代码行数:25,代码来源:Encryption64.cs

示例4: Encrypt

        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="pToEncrypt">需要加密字符串</param>
        /// <param name="sKey">密钥</param>
        /// <returns>加密后的字符串</returns>
        public static string Encrypt(string pToEncrypt, string sKey)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                //把字符串放到byte数组中

                //原来使用的UTF8编码,我改成Unicode编码了,不行
                byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);

                //建立加密对象的密钥和偏移量

                //使得输入密码必须输入英文文本
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new 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);
                }
                ret.ToString();
                return ret.ToString();
            }
            catch (Exception ex)
            {

            }

            return "";
        }
开发者ID:SuperRay,项目名称:MoMoWeb,代码行数:41,代码来源:CommonFunction.cs

示例5: Decrypt

 /// <summary>
 /// 解密
 /// </summary>
 /// <param name="text">要被解密字符</param>
 /// <param name="sKey">密钥</param>
 /// <returns></returns>
 public static string Decrypt(this string text, string sKey)
 {
     var provider = new DESCryptoServiceProvider();
     int num = text.Length / 2;
     byte[] buffer = new byte[num];
     try
     {
         for (int i = 0; i < num; i++)
         {
             int num3 = Convert.ToInt32(text.Substring(i * 2, 2), 0x10);
             buffer[i] = (byte)num3;
         }
     }
     catch
     {
         return string.Empty;
     }
     provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
     provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
     MemoryStream stream = new MemoryStream();
     CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
     try
     {
         stream2.Write(buffer, 0, buffer.Length);
         stream2.FlushFinalBlock();
     }
     catch
     {
         return string.Empty;
     }
     return Encoding.Default.GetString(stream.ToArray());
 }
开发者ID:NameIsBad,项目名称:wuliao,代码行数:38,代码来源:MD5Helper.cs

示例6: Decrypt

		/// <summary> 
		/// 解密数据 
		/// </summary> 
		/// <param name="Text"></param> 
		/// <param name="sKey"></param> 
		/// <returns></returns> 
		public static string Decrypt(string Text,string sKey) 
		{
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                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;
                }

                //.net framework 4.5方法过期
                //des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 
                //des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
                MD5 md5Hash = MD5.Create();
                des.Key = ASCIIEncoding.ASCII.GetBytes(GetMd5Hash(sKey, md5Hash).Substring(0, 8));
                des.IV = ASCIIEncoding.ASCII.GetBytes(GetMd5Hash(sKey, md5Hash).Substring(0, 8));

                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.Default.GetString(ms.ToArray());
            }
            catch 
            {
                return null;
            }
		} 
开发者ID:YouToCode,项目名称:SQLite.Demo,代码行数:39,代码来源:DESEncrypt.cs

示例7: DecodeStr

        /// <summary>
        /// DES 解密算法
        /// </summary>
        /// <param name="str">待解密字符串</param>
        /// <returns>解密后字符串</returns>
        public static string DecodeStr(string str)
        {
            try
            {
                byte[] temp = new byte[str.Length / 2];

                using (DESCryptoServiceProvider desc = new DESCryptoServiceProvider())
                {
                    for (int i = 0; i < str.Length / 2; i++)
                    {
                        int j = Convert.ToInt32(str.Substring(i * 2, 2), 16);

                        temp[i] = (byte)j;
                    }

                    desc.Key = ASCIIEncoding.ASCII.GetBytes(key); desc.IV = ASCIIEncoding.ASCII.GetBytes(iv);

                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, desc.CreateDecryptor(), CryptoStreamMode.Write))
                        {
                            cs.Write(temp, 0, temp.Length);

                            cs.FlushFinalBlock();
                        }
                        return System.Text.Encoding.UTF8.GetString(ms.ToArray());
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
开发者ID:sclynton,项目名称:CrazyBull,代码行数:39,代码来源:Des.cs

示例8: DESEncrypt

        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="pToEncrypt">需要加密字符串</param>
        /// <param name="sKey">密钥</param>
        /// <returns>加密后的字符串</returns>
        public static string DESEncrypt(string pToEncrypt)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                //把字符串放到byte数组中

                //原来使用的UTF8编码,我改成Unicode编码了,不行
                byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);

                //建立加密对象的密钥和偏移量

                //使得输入密码必须输入英文文本
                des.Key = Encoding.UTF8.GetBytes(sKey);
                des.IV = Encoding.UTF8.GetBytes(sKey);
                MemoryStream ms = new 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);
                }
                ret.ToString();
                return ret.ToString();
            }
            catch (Exception ex)
            {
                System.Web.HttpContext.Current.Response.Write("写入配置信息失败,详细信息:" + ex.Message.Replace("\r\n", "").Replace("'", ""));
            }

            return "";
        }
开发者ID:vingi,项目名称:TEAMFIRST,代码行数:41,代码来源:Encrypt.cs

示例9: Decrypt

 public static string Decrypt(string strText)
 {
     string text1;
     if (strText == String.Empty)
     {
         return strText;
     }
     byte[] buffer1;
     byte[] buffer2;
     byte[] buffer3 = GetCryptArr();
     try
     {
         buffer1 = Encoding.UTF8.GetBytes(GetCryptKey().Substring(0,8));
         DESCryptoServiceProvider provider1 =  new DESCryptoServiceProvider();
         buffer2 = Convert.FromBase64String(strText);
         MemoryStream stream2 = new MemoryStream();
         using (CryptoStream stream1 = new CryptoStream(stream2, provider1.CreateDecryptor(buffer1, buffer3), CryptoStreamMode.Write))
         {
             stream1.Write(buffer2, 0, buffer2.Length);
             stream1.FlushFinalBlock();
             text1 = Encoding.UTF8.GetString(stream2.ToArray());
         }
     }
     catch
     {
         return "Error";
     }
     return text1;
 }
开发者ID:rogerlio,项目名称:csharp-proj,代码行数:29,代码来源:Utils.cs

示例10: DESDeCode

        public static string DESDeCode(string pToDecrypt, string sKey)
        {
            //    HttpContext.Current.Response.Write(pToDecrypt + "<br>" + sKey);   
            //    HttpContext.Current.Response.End();   
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }

            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            StringBuilder ret = new StringBuilder();

            // return HttpContext.Current.Server.UrlDecode(System.Text.Encoding.Default.GetString(ms.ToArray()));
            return System.Text.Encoding.Default.GetString(ms.ToArray());
        }
开发者ID:FuChuan7,项目名称:WeiXin-ASP.NET-MVC3,代码行数:25,代码来源:DES.cs

示例11: Decrypt

        public static string Decrypt(string text)
        {
            try
            {
                key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                Byte[] byteArray = Convert.FromBase64String(text);

                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream,
                    des.CreateDecryptor(key, IV), CryptoStreamMode.Write);

                cryptoStream.Write(byteArray, 0, byteArray.Length);
                cryptoStream.FlushFinalBlock();

                return Encoding.UTF8.GetString(memoryStream.ToArray());
            }
            catch (Exception ex)
            {
                // Handle Exception Here
            }

            return string.Empty;
        }
开发者ID:jjabney,项目名称:Lucid.Web.Portal,代码行数:25,代码来源:Cryptography.cs

示例12: EncryptString

 /// <summary>
 /// 加密字符串
 /// </summary>
 /// <param name="inputStr">输入字符串</param>
 /// <param name="keyStr">密码,可以为“”</param>
 /// <returns>输出加密后字符串</returns>
 public string EncryptString(string inputStr, string keyStr)
 {
     DESCryptoServiceProvider des = new DESCryptoServiceProvider();
     if (keyStr == "")
         keyStr = key;
     byte[] inputByteArray = Encoding.Default.GetBytes(inputStr);
     byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
     SHA1 ha = new SHA1Managed();
     byte[] hb = ha.ComputeHash(keyByteArray);
     sKey = new byte[8];
     sIV = new byte[8];
     for (int i = 0; i < 8; i++)
         sKey[i] = hb[i];
     for (int i = 8; i < 16; i++)
         sIV[i - 8] = hb[i];
     des.Key = sKey;
     des.IV = sIV;
     MemoryStream ms = new 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);
     }
     cs.Close();
     ms.Close();
     return ret.ToString();
 }
开发者ID:EricBlack,项目名称:.NET-Collection,代码行数:36,代码来源:MySecurity.cs

示例13: DESEnCode

        public static string DESEnCode(string pToEncrypt, string sKey)
        {
            // string pToEncrypt1 = HttpContext.Current.Server.UrlEncode(pToEncrypt);   
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);

            //建立加密对象的密钥和偏移量    
            //原文使用ASCIIEncoding.ASCII方法的GetBytes方法    
            //使得输入密码必须输入英文文本    
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new 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);
            }
            ret.ToString();
            return ret.ToString();
        }
开发者ID:FuChuan7,项目名称:WeiXin-ASP.NET-MVC3,代码行数:25,代码来源:DES.cs

示例14: Decrypt

        /// <summary>
        /// 用给定的Key进行解密
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="decryptString">要解密的字符串</param>
        /// <returns>解密后的字符串</returns>
        public static string Decrypt(string decryptString, string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                key = _Key;
            else
                key += _Key;

            if (string.IsNullOrWhiteSpace(decryptString))
                decryptString = string.Empty;

            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            byte[] keyIV = keyBytes;

            byte[] 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;
            }

            using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
            {
                using (MemoryStream mStream = new MemoryStream())
                {
                    using (CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write))
                    {
                        cStream.Write(inputByteArray, 0, inputByteArray.Length);
                        cStream.FlushFinalBlock();
                        return Encoding.UTF8.GetString(mStream.ToArray());
                    }
                }
            }
        }
开发者ID:JodenSoft,项目名称:JodenSoft,代码行数:39,代码来源:DESHelper.cs

示例15: Encrypt

        /// <summary>
        /// 用给定的Key进行加密
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="encryptString">要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string Encrypt(string encryptString, string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                key = _Key;
            else
                key += _Key;

            if (string.IsNullOrWhiteSpace(encryptString))
                encryptString = string.Empty;

            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            byte[] keyIV = keyBytes;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
            {
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();

                //Get the data back from the memory stream, and into a string
                StringBuilder ret = new StringBuilder();
                foreach (byte b in mStream.ToArray())
                {
                    //Format as hex
                    ret.AppendFormat("{0:X2}", b);
                }
                return ret.ToString();
            }
        }
开发者ID:JodenSoft,项目名称:JodenSoft,代码行数:36,代码来源:DESHelper.cs


注:本文中的System.Security.Cryptography.DESCryptoServiceProvider类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。