程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> AES加密類,aes加密

AES加密類,aes加密

編輯:C#入門知識

AES加密類,aes加密


代碼:

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Utils
{
    /// <summary>
    /// AES加密解密
    /// </summary>
    public class AES
    {
        #region 加密
        #region 加密字符串
        /// <summary>
        /// AES 加密(高級加密標准,是下一代的加密算法標准,速度快,安全級別高,目前 AES 標准的一個實現是 Rijndael 算法)
        /// </summary>
        /// <param name="EncryptString">待加密密文</param>
        /// <param name="EncryptKey">加密密鑰</param>
        public static string AESEncrypt(string EncryptString, string EncryptKey)
        {
            return Convert.ToBase64String(AESEncrypt(Encoding.Default.GetBytes(EncryptString), EncryptKey));
        }
        #endregion

        #region 加密字節數組
        /// <summary>
        /// AES 加密(高級加密標准,是下一代的加密算法標准,速度快,安全級別高,目前 AES 標准的一個實現是 Rijndael 算法)
        /// </summary>
        /// <param name="EncryptString">待加密密文</param>
        /// <param name="EncryptKey">加密密鑰</param>
        public static byte[] AESEncrypt(byte[] EncryptByte, string EncryptKey)
        {
            if (EncryptByte.Length == 0) { throw (new Exception("明文不得為空")); }
            if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密鑰不得為空")); }
            byte[] m_strEncrypt;
            byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
            byte[] m_salt = Convert.FromBase64String("gsf4jvkyhye5/d7k8OrLgM==");
            Rijndael m_AESProvider = Rijndael.Create();
            try
            {
                MemoryStream m_stream = new MemoryStream();
                PasswordDeriveBytes pdb = new PasswordDeriveBytes(EncryptKey, m_salt, "SHA256", 1000);
                CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateEncryptor(pdb.GetBytes(32), m_btIV), CryptoStreamMode.Write);
                m_csstream.Write(EncryptByte, 0, EncryptByte.Length); m_csstream.FlushFinalBlock();
                m_strEncrypt = m_stream.ToArray();
                m_stream.Close(); m_stream.Dispose();
                m_csstream.Close(); m_csstream.Dispose();
            }
            catch (IOException ex) { throw ex; }
            catch (CryptographicException ex) { throw ex; }
            catch (ArgumentException ex) { throw ex; }
            catch (Exception ex) { throw ex; }
            finally { m_AESProvider.Clear(); }
            return m_strEncrypt;
        }
        #endregion
        #endregion

        #region 解密
        #region 解密字符串
        /// <summary>
        /// AES 解密(高級加密標准,是下一代的加密算法標准,速度快,安全級別高,目前 AES 標准的一個實現是 Rijndael 算法)
        /// </summary>
        /// <param name="DecryptString">待解密密文</param>
        /// <param name="DecryptKey">解密密鑰</param>
        public static string AESDecrypt(string DecryptString, string DecryptKey)
        {
            return Convert.ToBase64String(AESDecrypt(Encoding.Default.GetBytes(DecryptString), DecryptKey));
        }
        #endregion

        #region 解密字節數組
        /// <summary>
        /// AES 解密(高級加密標准,是下一代的加密算法標准,速度快,安全級別高,目前 AES 標准的一個實現是 Rijndael 算法)
        /// </summary>
        /// <param name="DecryptString">待解密密文</param>
        /// <param name="DecryptKey">解密密鑰</param>
        public static byte[] AESDecrypt(byte[] DecryptByte, string DecryptKey)
        {
            if (DecryptByte.Length == 0) { throw (new Exception("密文不得為空")); }
            if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密鑰不得為空")); }
            byte[] m_strDecrypt;
            byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
            byte[] m_salt = Convert.FromBase64String("gsf4jvkyhye5/d7k8OrLgM==");
            Rijndael m_AESProvider = Rijndael.Create();
            try
            {
                MemoryStream m_stream = new MemoryStream();
                PasswordDeriveBytes pdb = new PasswordDeriveBytes(DecryptKey, m_salt, "SHA256", 1000);
                CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateDecryptor(pdb.GetBytes(32), m_btIV), CryptoStreamMode.Write);
                m_csstream.Write(DecryptByte, 0, DecryptByte.Length); m_csstream.FlushFinalBlock();
                m_strDecrypt = m_stream.ToArray();
                m_stream.Close(); m_stream.Dispose();
                m_csstream.Close(); m_csstream.Dispose();
            }
            catch (IOException ex) { throw ex; }
            catch (CryptographicException ex) { throw ex; }
            catch (ArgumentException ex) { throw ex; }
            catch (Exception ex) { throw ex; }
            finally { m_AESProvider.Clear(); }
            return m_strDecrypt;
        }
        #endregion
        #endregion

    }
}

 


怎破解AES加密方法

破解wifi密碼AES加密類型的現在可以破了!AES密碼加密存在漏洞已經不是什麼秘密啦,甚至連很多品牌的路由器wps碼都存在漏洞!破解方法一般有兩種:一種為握手包破解,另一種為WPS碼破解。我專業做這方面的!
 

AES加密算法中為何要將數據定義為unsigned類型的

這與AES算法中定義的S_BOX數組有關。S_BOX中的元素為0X00至0XFF之間的所有字符,unsigned char類型正好表示的0-255之間ASCII表中的所有字符;如果使用char,則表示范圍為-128至+127,只能表示0-127之間ASCII表中的字符。
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved