AES加密DES加密MD5加密

本文介绍并演示了AES、DES和MD5加密算法的应用实例,包括加密、解密过程及MD5散列计算。

using System;

using System.Collections.Generic;

using System.Text;

using System.Security.Cryptography;

using System.IO;

namespace ToolsUtil

{

   //AES加密DES加密MD5加密

    public class EncryptionAlgorithm

    {

        public static AESEncryptor fAESEncryptor = new AESEncryptor("My Password", AESBits.BITS256);

        public static TripleDESEncryptor fTripleDESEncryptor = new TripleDESEncryptor("My Password");

        public static MD5Encryptor fMD5Encryptor = new MD5Encryptor();

        public static void main()

        {

            string originalText = "Hello! This is Elixis...";

            string password = "My Password";

            Console.WriteLine("AES Encryption \n ");

            // Encrypt with AES.

            string encryptedAESString = fAESEncryptor.Encrypt(originalText);

            Console.WriteLine("Encrypted AES: " + encryptedAESString);

            // Decrypt with AES.

            string decryptedAESString = fAESEncryptor.Decrypt(encryptedAESString);

            Console.WriteLine("Decrypted AES: " + decryptedAESString);

            Console.WriteLine("\n\nTripleDES Encryption \n ");

            // Encrypt with TripleDES.

            byte[] tripleDESEncryptedString = fTripleDESEncryptor.Encrypt(Encoding.ASCII.GetBytes(originalText));

            Console.WriteLine("Encrypted TripleDES: " + Encoding.Default.GetString(tripleDESEncryptedString));

            // Decrypt with TripleDES.

            byte[] tripleDESDecryptedString = fTripleDESEncryptor.Decrypt(tripleDESEncryptedString);

            Console.WriteLine("Decrypted TripleDES: " + Encoding.Default.GetString(tripleDESDecryptedString));

            Console.WriteLine("\n\nMD5 Encryption \n ");

            // Encrypt with MD5.

            string md5 = fMD5Encryptor.GetMD5(originalText);

            Console.WriteLine("MD5: " + md5);

            string md5_bytes = fMD5Encryptor.GetMD5(Encoding.ASCII.GetBytes(originalText));

            Console.WriteLine("MD5_bytes: " + md5_bytes);

            Console.Read();

        }

    }

    public enum AESBits

    {

        BITS128,

        BITS192,

        BITS256

    };

    public class AESEncryptor

    {

        private string fPassword;

        private AESBits fEncryptionBits;

        private byte[] fSalt = new byte[] { 0x00, 0x01, 0x02, 0x1C, 0x1D, 0x1E, 0x03, 0x04, 0x05, 0x0F, 0x20, 0x21, 0xAD, 0xAF, 0xA4 };

        /// <summary>

        /// Initialize new AESEncryptor.

        /// </summary>

        /// <param name="password">The password to use for encryption/decryption.</param>

        /// <param name="encryptionBits">Encryption bits (128,192,256).</param>

        public AESEncryptor(string password, AESBits encryptionBits)

        {

            fPassword = password;

            fEncryptionBits = encryptionBits;

        }

        /// <summary>

        /// Initialize new AESEncryptor.

        /// </summary>

        /// <param name="password">The password to use for encryption/decryption.</param>

        /// <param name="encryptionBits">Encryption bits (128,192,256).</param>

        /// <param name="salt">Salt bytes. Bytes length must be 15.</param>

        public AESEncryptor(string password, AESBits encryptionBits, byte[] salt)

        {

            fPassword = password;

            fEncryptionBits = encryptionBits;

            fSalt = salt;

        }

        private byte[] iEncrypt(byte[] data, byte[] key, byte[] iV)

        {

            MemoryStream ms = new MemoryStream();

            Rijndael alg = Rijndael.Create();

            alg.Key = key;

            alg.IV = iV;

            CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);

            cs.Write(data, 0, data.Length);

            cs.Close();

            byte[] encryptedData = ms.ToArray();

            return encryptedData;

        }

        /// <summary>

        /// Encrypt string with AES algorith.

        /// </summary>

        /// <param name="data">String to encrypt.</param>

        public string Encrypt(string data)

        {

            byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(data);

            PasswordDeriveBytes pdb = new PasswordDeriveBytes(fPassword, fSalt);

            switch (fEncryptionBits)

            {

                case AESBits.BITS128:

                    return Convert.ToBase64String(iEncrypt(clearBytes, pdb.GetBytes(16), pdb.GetBytes(16)));

                case AESBits.BITS192:

                    return Convert.ToBase64String(iEncrypt(clearBytes, pdb.GetBytes(24), pdb.GetBytes(16)));

                case AESBits.BITS256:

                    return Convert.ToBase64String(iEncrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16)));

            }

            return null;

        }

        /// <summary>

        /// Encrypt byte array with AES algorithm.

        /// </summary>

        /// <param name="data">Bytes to encrypt.</param>

        public byte[] Encrypt(byte[] data)

        {

            PasswordDeriveBytes pdb = new PasswordDeriveBytes(fPassword, fSalt);

            switch (fEncryptionBits)

            {

                case AESBits.BITS128:

                    return iEncrypt(data, pdb.GetBytes(16), pdb.GetBytes(16));

                case AESBits.BITS192:

                    return iEncrypt(data, pdb.GetBytes(24), pdb.GetBytes(16));

                case AESBits.BITS256:

                    return iEncrypt(data, pdb.GetBytes(32), pdb.GetBytes(16));

            }

            return null;

        }

        private byte[] iDecrypt(byte[] data, byte[] key, byte[] iv)

        {

            MemoryStream ms = new MemoryStream();

            Rijndael alg = Rijndael.Create();

            alg.Key = key;

            alg.IV = iv;

            CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);

            cs.Write(data, 0, data.Length);

            cs.Close();

            byte[] decryptedData = ms.ToArray();

            return decryptedData;

        }

        /// <summary>

        /// Decrypt string with AES algorithm.

        /// </summary>

        /// <param name="data">Encrypted string.</param>

        public string Decrypt(string data)

        {

            byte[] dataToDecrypt = Convert.FromBase64String(data);

            PasswordDeriveBytes pdb = new PasswordDeriveBytes(fPassword, fSalt);

            switch (fEncryptionBits)

            {

                case AESBits.BITS128:

                    return System.Text.Encoding.Unicode.GetString(iDecrypt(dataToDecrypt, pdb.GetBytes(16), pdb.GetBytes(16)));

                case AESBits.BITS192:

                    return System.Text.Encoding.Unicode.GetString(iDecrypt(dataToDecrypt, pdb.GetBytes(24), pdb.GetBytes(16)));

                case AESBits.BITS256:

                    return System.Text.Encoding.Unicode.GetString(iDecrypt(dataToDecrypt, pdb.GetBytes(32), pdb.GetBytes(16)));

            }

            return null;

        }

        /// <summary>

        /// Decrypt byte array with AES algorithm.

        /// </summary>

        /// <param name="data">Encrypted byte array.</param>

        public byte[] Decrypt(byte[] data)

        {

            PasswordDeriveBytes pdb = new PasswordDeriveBytes(fPassword, fSalt);

            switch (fEncryptionBits)

            {

                case AESBits.BITS128:

                    return iDecrypt(data, pdb.GetBytes(16), pdb.GetBytes(16));

                case AESBits.BITS192:

                    return iDecrypt(data, pdb.GetBytes(24), pdb.GetBytes(16));

                case AESBits.BITS256:

                    return iDecrypt(data, pdb.GetBytes(32), pdb.GetBytes(16));

            }

            return null;

        }

        /// <summary>

        /// Encryption/Decryption password.

        /// </summary>

        public string Password

        {

            get { return fPassword; }

            set { fPassword = value; }

        }

        /// <summary>

        /// Encryption/Decryption bits.

        /// </summary>

        public AESBits EncryptionBits

        {

            get { return fEncryptionBits; }

            set { fEncryptionBits = value; }

        }

        /// <summary>

        /// Salt bytes (bytes length must be 15).

        /// </summary>

        public byte[] Salt

        {

            get { return fSalt; }

            set { fSalt = value; }

        }

    }

    public class MD5Encryptor

    {

        public MD5Encryptor()

        {

        }

        public string GetMD5(byte[] data)

        {

            MD5 md5 = new MD5CryptoServiceProvider();

            return BitConverter.ToString(md5.ComputeHash(data));

        }

        public string GetMD5(string data)

        {

            return GetMD5(ASCIIEncoding.Default.GetBytes(data));

        }

    }

    public class TripleDESEncryptor

    {

        private string fPassword;

        private byte[] fSalt = new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 };

        /// <summary>

        /// Initialize new TripleDESEncryptor.

        /// </summary>

        /// <param name="password">The password to use for encryption/decryption.</param>

        public TripleDESEncryptor(string password)

        {

            fPassword = password;

        }

        /// <summary>

        /// Initialize new TripleDESEncryptor.

        /// </summary>

        /// <param name="password">The password to use for encryption/decryption.</param>

        /// <param name="salt">Salt bytes. Bytes length must be 13.</param>

        public TripleDESEncryptor(string password, byte[] salt)

        {

            fPassword = password;

            fSalt = salt;

        }

        private byte[] iEncrypt(byte[] data, byte[] key, byte[] iv)

        {

            MemoryStream ms = new MemoryStream();

            TripleDES alg = TripleDES.Create();

            alg.Key = key;

            alg.IV = iv;

            CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);

            cs.Write(data, 0, data.Length);

            cs.Close();

            return ms.ToArray();

        }

        /// <summary>

        /// Encrypt string with TripleDES algorith.

        /// </summary>

        /// <param name="data">String to encrypt.</param>

        /// <returns>Encrypted string.</returns>

        public string Encrypt(string data)

        {

            byte[] dataToEncrypt = System.Text.Encoding.Unicode.GetBytes(data);

            PasswordDeriveBytes pdb = new PasswordDeriveBytes(fPassword, fSalt);

            return Convert.ToBase64String(iEncrypt(dataToEncrypt, pdb.GetBytes(16), pdb.GetBytes(8)));

        }

        /// <summary>

        /// Encrypt byte array with TripleDES algorithm.

        /// </summary>

        /// <param name="data">Bytes to encrypt.</param>

        /// <returns>Encrypted bytes.</returns>

        public byte[] Encrypt(byte[] data)

        {

            PasswordDeriveBytes pdb = new PasswordDeriveBytes(fPassword, fSalt);

            return iEncrypt(data, pdb.GetBytes(16), pdb.GetBytes(8));

        }

        private byte[] iDecrypt(byte[] data, byte[] key, byte[] iv)

        {

            MemoryStream ms = new MemoryStream();

            TripleDES alg = TripleDES.Create();

            alg.Key = key;

            alg.IV = iv;

            CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);

            cs.Write(data, 0, data.Length);

            cs.Close();

            return ms.ToArray();

        }

        /// <summary>

        /// Decrypt string with TripleDES algorithm.

        /// </summary>

        /// <param name="data">Encrypted string.</param>

        /// <returns>Decrypted string.</returns>

        public string Decrypt(string data)

        {

            byte[] cipherBytes = Convert.FromBase64String(data);

            PasswordDeriveBytes pdb = new PasswordDeriveBytes(fPassword, fSalt);

            return System.Text.Encoding.Unicode.GetString(iDecrypt(cipherBytes, pdb.GetBytes(16), pdb.GetBytes(8)));

        }

        /// <summary>

        /// Decrypt byte array with TripleDES algorithm.

        /// </summary>

        /// <param name="data">Encrypted byte array.</param>

        /// <returns>Decrypted byte array.</returns>

        public byte[] Decrypt(byte[] data)

        {

            PasswordDeriveBytes pdb = new PasswordDeriveBytes(fPassword, fSalt);

            return iDecrypt(data, pdb.GetBytes(16), pdb.GetBytes(8));

        }

        /// <summary>

        /// Encryption/Decryption password.

        /// </summary>

        public string Password

        {

            get { return fPassword; }

            set { fPassword = value; }

        }

        /// <summary>

        /// Salt bytes (bytes length must be 15).

        /// </summary>

        public byte[] Salt

        {

            get { return fSalt; }

            set { fSalt = value; }

        }

    }

}

转载于:https://www.cnblogs.com/genelau/archive/2011/10/15/2212814.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值