c#加密算法

#region DESEnCode DES加密
         ///   <summary>
        
///  默认Key
        
///   </summary>
         private  const  string Default_Key =  " A3F2DEI569WBCJSJEOTY45DYQWF68H1Y ";
         ///   <summary>
        
///  默认IV 矢量,矢量可以为空 
        
///   </summary>
         private  const  string Default_IV =  " qcDX+Y6aPLw= ";
         ///   <summary>
        
///  加密字符串
        
///   </summary>
        
///   <param name="value"></param>
        
///   <returns></returns>
         public  static  string EncryptString( string value)
        {
             return EncryptString(Default_Key, Default_IV, value);
        }
         ///   <summary>
        
///  加密字符串
        
///   </summary>
        
///   <param name="key"> Key键 </param>
        
///   <param name="iv"> 向量 </param>
        
///   <param name="value"> 要加密的串 </param>
        
///   <returns> 加密后的字串 </returns>
         public  static  string EncryptString( string key,  string iv,  string value)
        {
             using (SymmetricAlgorithm desCSP =  new TripleDESCryptoServiceProvider())
            {
                desCSP.Key = Convert.FromBase64String(key);
                desCSP.IV = Convert.FromBase64String(iv);     // 指定加密的运算模式 
                desCSP.Mode = System.Security.Cryptography.CipherMode.ECB;     // 获取或设置加密算法的填充模式 
                desCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ICryptoTransform ctTrans = desCSP.CreateEncryptor(desCSP.Key, desCSP.IV);
                 byte[] bytes = Encoding.UTF8.GetBytes(value);
                 using (MemoryStream memStream =  new MemoryStream())
                {
                     using (CryptoStream csEncrypt =  new CryptoStream(memStream, ctTrans, CryptoStreamMode.Write))
                    {
                        csEncrypt.Write(bytes,  0, bytes.Length);
                        csEncrypt.FlushFinalBlock();
                        csEncrypt.Close();
                    }
                    bytes =  null// 清理内存
                     return Convert.ToBase64String(memStream.ToArray());
                }
            }
        }
         ///   <summary>
        
///  解密字符串
        
///   </summary>
        
///   <param name="value"></param>
        
///   <returns></returns>
         public  static  string DecryptString( string value)
        {
             return DecryptString(Default_Key, Default_IV, value);
        }
         ///   <summary>
        
///  解密字符串
        
///   </summary>
        
///   <param name="key"> Key键 </param>
        
///   <param name="iv"> 向量 </param>
        
///   <param name="value"> 要解密的串 </param>
        
///   <returns> 解密后的字串 </returns>
         public  static  string DecryptString( string key,  string iv,  string value)
        {
             using (SymmetricAlgorithm desCSP =  new TripleDESCryptoServiceProvider())
            {
                desCSP.Key = Convert.FromBase64String(key);
                desCSP.IV = Convert.FromBase64String(iv);     // 指定加密的运算模式 
                desCSP.Mode = System.Security.Cryptography.CipherMode.ECB;     // 获取或设置加密算法的填充模式 
                desCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ICryptoTransform ctTrans = desCSP.CreateDecryptor(desCSP.Key, desCSP.IV);
                 byte[] bytes = Convert.FromBase64String(value);
                 byte[] bytesOut =  new  byte[ 10240];
                 int iReadLen =  0;
                 using (MemoryStream memStream =  new MemoryStream(bytes))
                {
                     using (CryptoStream csDecrypt =  new CryptoStream(memStream, ctTrans, CryptoStreamMode.Read))
                    {
                         using (MemoryStream outStream =  new MemoryStream())
                        {
                             while ((iReadLen = csDecrypt.Read(bytesOut,  0, bytesOut.Length)) >  0)
                            {
                                outStream.Write(bytesOut,  0, iReadLen);
                            }
                            bytes =  null;
                            bytesOut =  null; // 清理内存
                             return System.Text.Encoding.UTF8.GetString(outStream.ToArray());
                        }
                    }
                }
            }
        }
         #endregion

你可能感兴趣的:(c#加密算法)