Cryptography,一個C#寫的加解密演算法的類

來源:互聯網
上載者:User

一個加解密演算法的類,如下:

 

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);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.