1. 程式人生 > 實用技巧 >基礎類庫積累--Encrypt類

基礎類庫積累--Encrypt類

前言:

我們在資料傳輸的過程中,肯定不能使用明文傳輸,這樣太容易被人截獲資訊,一般會對傳輸的資料進行加密操作。

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="value">需要加密的字串(明文)</param>
        /// <returns>加密之後的字串(密文)</returns>
        public static string DesEncrypt(string value)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(value);

            string key = "s1u&n*dy";

            des.Key = ASCIIEncoding.ASCII.GetBytes(key);
            des.IV = ASCIIEncoding.ASCII.GetBytes(key);
            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);
            }
            return ret.ToString();
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="value">需要加密的字串(明文)</param>
        /// <param name="value">金鑰</param>
        /// <returns>加密之後的字串(密文)</returns>
       public static string Md5Encrypt(string value ,string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.Default.GetBytes(value);
            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();
        }
        /// <summary>
        /// Base64加密
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static string Base64Encrypt(Encoding encode,string msg)
        {
            byte[] encbuff = encode.GetBytes(msg);
            return Convert.ToBase64String(encbuff);
        }

     
             /// <summary>
        /// AES加密(無向量)
        /// </summary>
        /// <param name="plainBytes">被加密的明文</param>
        /// <param name="key">金鑰</param>
        /// <returns>密文</returns>
        public static string AESEncrypt(String Data, String Key)
        {
            MemoryStream mStream = new MemoryStream();
            RijndaelManaged aes = new RijndaelManaged();

            byte[] plainBytes = Encoding.UTF8.GetBytes(Data);
            Byte[] bKey = new Byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);

            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;
            aes.KeySize = 128;
            //aes.Key = _key;
            aes.Key = bKey;
            //aes.IV = _iV;
            CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
            try
            {
                cryptoStream.Write(plainBytes, 0, plainBytes.Length);
                cryptoStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            finally
            {
                cryptoStream.Close();
                mStream.Close();
                aes.Clear();
            }
        }

       /// <summary>
        /// RSA產生金鑰
        /// </summary>
        /// <param name="xmlKeys">私鑰</param>
        /// <param name="xmlPublicKey">公鑰</param>
        public static void RSAKey(out string xmlKeys, out string xmlPublicKey)
        {
            try
            {
                System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                xmlKeys = rsa.ToXmlString(true);
                xmlPublicKey = rsa.ToXmlString(false);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// RSA加密
        /// </summary>
        /// <param name="publickey"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RSAEncrypt(string publickey, string content)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            byte[] cipherbytes;
            rsa.FromXmlString(publickey);
            cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);

            return Convert.ToBase64String(cipherbytes);
        }

        /// <summary>SHA256加密
        /// </summary>
        /// <param name="publicKey">明文</param>
        /// <returns>密文字串</returns>
       public static string SHA256(string str)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(str);
            SHA256CryptoServiceProvider SHA256 = new SHA256CryptoServiceProvider();
            byte[] byteArr = SHA256.ComputeHash(buffer);
            return BitConverter.ToString(byteArr);
        }
       /// <summary>SHA128加密【20位元組,160位】
        /// </summary>
        /// <param name="publicKey">明文</param>
        /// <returns>密文字串</returns>
        public static string SHA128(string str)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(str);
            SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
            byte[] byteArr = SHA1.ComputeHash(buffer);
            return BitConverter.ToString(byteArr);
        }
         /// <summary>SHA384加密【48位元組,384位】
        /// </summary>
        /// <param name="publicKey">明文</param>
        /// <returns>密文字串</returns>
        public static string SHA384(string str)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(str);
            SHA384CryptoServiceProvider SHA384 = new SHA384CryptoServiceProvider();
            byte[] byteArr = SHA384.ComputeHash(buffer);
            return BitConverter.ToString(byteArr);
        }
        /// <summary>SHA512加密【64位元組,512位】
        /// </summary>
        /// <param name="publicKey">明文</param>
        /// <returns>密文字串</returns>
        public static string SHA512(string str)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(str);
            SHA512CryptoServiceProvider SHA512 = new SHA512CryptoServiceProvider();
            byte[] byteArr = SHA512.ComputeHash(buffer);
            return BitConverter.ToString(byteArr);
        }

簡介:

  • 加密的方式有很多:MD5,DES,AES,RSA,SHA128,SHA256,SHA384,SHA512,Base64等等
  • 加密的簡單介紹:
    • MD5:對字元或密碼進行的16位或32位加密【不可逆的加密】
    • AES:採用對稱分組密碼體制,金鑰長度可以為128、192或256位,分組長度128位【對稱加密】
    • DES:明文長度為1-7之間,則密文長度為8;明文長度為8-15之間時,加密後的密文長度為16,一般做金融資料加密【對稱加密】
    • RSA:明文只有117位元組,公鑰加密演算法【非對稱加密】,你需要先用RSAKey生成一個公鑰和一個私鑰,加密的使用公鑰,解密使用私鑰
    • SHA128,SHA256,SHA384:安全雜湊演算法(Secure Hash Algorithm)【非對稱加密】
    • Base64:從二進位制到字元的過程,可用於在HTTP環境下傳遞較長的標識資訊,保密電子郵件密碼
  • 對稱加密:對稱加密指的就是加密和解密使用同一個祕鑰,所以叫做對稱加密。對稱加密只有一個祕鑰,作為私鑰。 常見的對稱加密演算法:DES,AES,3DES等等。
  • 非對稱加密:非對稱加密指的是:加密和解密使用不同的祕鑰,一把作為公開的公鑰,另一把作為私鑰。公鑰加密的資訊,只有私鑰才能解密。私鑰加密的資訊,只有公鑰才能解密。
  • 對稱加密和非對稱的區別:對稱加密演算法相比非對稱加密演算法來說,加解密的效率要高得多。但是缺陷在於對於祕鑰的管理上,以及在非安全通道中通訊時,金鑰交換的安全性不能保障。所以在實際的網路環境中,會將兩者混合使用.

參考:

C#加密解密(DES,AES,Base64,md5,SHA256,RSA,RC4)
加密原理介紹,程式碼實現DES、AES、RSA、Base64、MD5

總結:

在百度的海洋中,瞭解RC4加密不安全,就沒有貼有關的程式碼。其他加密原理有些還不是很清楚,部落格園裡面有不少講原理,我慢慢看,這篇主要是做積累,以後如果用到需要加密的需求,而我是有的選擇,而不至於一臉懵逼。

原文地址:https://www.cnblogs.com/2828sea/p/13490367.html