• Cryptography,一个C#写的加解密算法的类


    一个加解密算法的类,如下:

    Code
    using System;
    using System.IO;
    using System.Security.Cryptography;

    using System.Text;

    namespace SurvIT.Business.UserPrivilege
    {
        
    /// <summary>   
        
    /// Symmtric Crypto   
        
    /// </summary>   
        public class RijndaelCrypto
        {
            
    private SymmetricAlgorithm _objCryptoService;
            
    private string _strKey = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
            
    private string _strIV = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
            
    /// <summary>   
            
    /// Conctructor
            
    /// </summary>   
            public RijndaelCrypto(string key, string IV)
            {
                _objCryptoService 
    = new RijndaelManaged();
                _objCryptoService 
    = new TripleDESCryptoServiceProvider();
                _strKey 
    = key;
                _strIV 
    = IV;
            }
            
    public RijndaelCrypto()
            {
                _objCryptoService 
    = new RijndaelManaged();
                _objCryptoService 
    = new TripleDESCryptoServiceProvider();
            }
            
    /// <summary>   
            
    /// Get the key   
            
    /// </summary>   
            
    /// <returns>key</returns>   
            private byte[] GetLegalKey()
            {
                
    string sTemp = _strKey;
                _objCryptoService.GenerateKey();
                
    byte[] bytTemp = _objCryptoService.Key;
                
    int KeyLength = bytTemp.Length;
                
    if (sTemp.Length > KeyLength)
                    sTemp 
    = sTemp.Substring(0, KeyLength);
                
    else if (sTemp.Length < KeyLength)
                    sTemp 
    = sTemp.PadRight(KeyLength, ' ');
                
    return ASCIIEncoding.ASCII.GetBytes(sTemp);
            }
            
    /// <summary>   
            
    /// Initialize IV   
            
    /// </summary>   
            
    /// <returns>Initialize IV</returns>   
            private byte[] GetLegalIV()
            {
                _objCryptoService.GenerateIV();
                
    byte[] bytTemp = _objCryptoService.IV;
                
    int IVLength = bytTemp.Length;
                
    if (_strIV.Length > IVLength)
                    _strIV 
    = _strIV.Substring(0, IVLength);
                
    else if (_strIV.Length < IVLength)
                    _strIV 
    = _strIV.PadRight(IVLength, ' ');
                
    return ASCIIEncoding.ASCII.GetBytes(_strIV);
            }
            
    /// <summary>   
            
    /// Encrypto   
            
    /// </summary>   
            
    /// <param name="Source">The source string need to encrypto</param>   
            
    /// <returns>The string after crypto</returns>   
            public string Encrypto(string Source)
            {
                
    byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
                MemoryStream ms 
    = new MemoryStream();
                _objCryptoService.Key 
    = GetLegalKey();
                _objCryptoService.IV 
    = GetLegalIV();
                ICryptoTransform encrypto 
    = _objCryptoService.CreateEncryptor();
                
    using (CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write))
                {
                    cs.Write(bytIn, 
    0, bytIn.Length);
                    cs.FlushFinalBlock();
                    ms.Close();
                    
    byte[] bytOut = ms.ToArray();
                    
    string strRet = Convert.ToBase64String(bytOut);
                    cs.Clear();
                    
    return strRet;
                }
            }
            
    /// <summary>   
            
    /// Decrypto   
            
    /// </summary>   
            
    /// <param name="Source">The srouce need to decrypto</param>   
            
    /// <returns>The result string after decrypto</returns>   
            public string Decrypto(string Source)
            {
                
    byte[] bytIn = Convert.FromBase64String(Source);
                
    using (MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length))
                {
                    _objCryptoService.Key 
    = GetLegalKey();
                    _objCryptoService.IV 
    = GetLegalIV();
                    ICryptoTransform encrypto 
    = _objCryptoService.CreateDecryptor();
                    CryptoStream cs 
    = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
                    StreamReader sr 
    = new StreamReader(cs);
                    
    string strRet = sr.ReadToEnd();
                    cs.Clear();
                    
    return strRet;
                }
            }
        }
    }

    用法如下:

    Code
    public static string EncryptoPassword(string pwd)
    {
            RijndaelCrypto rijndael 
    = new RijndaelCrypto();
            
    return rijndael.Encrypto(pwd);
    }

    public static string DecryptoPassword(string CryptoPwd)
    {
           RijndaelCrypto rijndael 
    = new RijndaelCrypto();
           
    return rijndael.Decrypto(CryptoPwd);
    }
  • 相关阅读:
    Linux-netstat
    API接口防止参数篡改和重放攻击
    Java中遍历Map的几种方式
    Java泛型中的标记符含义
    Iterator 和 for...of 循环
    Promise 对象
    Reflect
    正则要求密码长度最少12位,包含至少1个特殊字符,2个数字,2个大写字母和一些小写字母。
    一个JS正则表达式,一个正实数,整数部分最多11位 小数部分最多 8位
    java阿里云短信发送配置
  • 原文地址:https://www.cnblogs.com/michaelxu/p/1495746.html
Copyright © 2020-2023  润新知