AES可逆加密算法

分享一段前段时间看到的AES可逆加密算法。

除去常见的MD5等加密方式,如果想要使用一些更加隐蔽的加密方式,则可以使用AES的RijndaelManaged加密算法。

关于加密,有很多复杂的算法,今天只跟大家分享一段摘取的结合动态密钥的对称AES RijndaelManaged加密解密算法,如果大家有兴趣了解更多,我们可以一起深入研究……

static void Main(string[] args)

        {

            string key = "8H[=}dKw+@3uV4D(";

            CryptoHelper helper = new CryptoHelper(key);

            if (args.Length == 2 && args[0] == "-e")

            {

                Console.WriteLine("Encrypt password: {0}", args[1]);

                string a = helper.Encrypt(args[1]);

                Console.WriteLine("Encrypt result is: {0}",a);

            }

            else if (args.Length == 2 && args[0] == "-d")

            {

                Console.WriteLine("Decrypt password: {0}", args[1]);

                string a = helper.Decrypt(args[1]);

                Console.WriteLine("Decrypt result is: {0}", a);

            }

        }

 

public class CryptoHelper

    {

        private const string _DefaultIntializationVector = "%1Az=-@qTx7h)5:-";

        private RijndaelManaged _crypto;

        private static Encoding _encoding = Encoding.ASCII;

        private CryptoHelper()

        {

        }

        ///

        /// RijndaelManaged (AES)

        ///

        /// the key for encrypt

        public CryptoHelper(string key)

        {

            this._crypto = new RijndaelManaged();

            this._crypto.Key = CryptoHelper._encoding.GetBytes(key);

            this._crypto.IV = CryptoHelper._encoding.GetBytes("%1Az=-@qTx7h)5:-");

        }


        ///

        /// encrypt the string

        ///

        /// encrypt string

        /// encrypt result

        public string Encrypt(string s)

        {

            byte[] bytes = CryptoHelper._encoding.GetBytes(s);

            MemoryStream memoryStream = new MemoryStream();

            CryptoStream cryptoStream = new CryptoStream(memoryStream, this._crypto.CreateEncryptor(), CryptoStreamMode.Write);

            cryptoStream.Write(bytes, 0, bytes.Length);

            cryptoStream.Close();

            memoryStream.Close();

            byte[] array = memoryStream.ToArray();

            if (array == null || array.Length == 0)

            {

                return "";

            }

            StringBuilder stringBuilder = new StringBuilder();

           byte[] array2 = array;

            for (int i = 0; i < array2.Length; i++)

            {

                byte b = array2[i];

                stringBuilder.Append(string.Format("{0:X2}", b));

            }

            return stringBuilder.ToString();

        }


        ///

        /// decrypt string

        ///

        ///

        ///

        public string Decrypt(string hexValue)

        {

            byte[] array;

            if (hexValue == null || hexValue.Length == 0)

            {

                array = null;

            }

            else

            {

                int num = Convert.ToInt32(hexValue.Length / 2);

                array = new byte[num];

                for (int i = 0; i <= num - 1; i++)

                {

                    array[i] = Convert.ToByte(hexValue.Substring(i * 2, 2), 16);

                }

            }

            MemoryStream stream = new MemoryStream(array, 0, array.Length);

            byte[] array2 = new byte[array.Length - 1];

            CryptoStream cryptoStream = new CryptoStream(stream, this._crypto.CreateDecryptor(), CryptoStreamMode.Read);

            try

            {

                cryptoStream.Read(array2, 0, array.Length - 1);

            }

            catch (CryptographicException inner)

            {

                throw new CryptographicException("Unable to decrypt data. The provided key may be invalid.", inner);

            }

            finally

            {

                cryptoStream.Close();

            }

            int num2 = Array.IndexOf(array2, 0);

            if (num2 >= 0)

            {

                return CryptoHelper._encoding.GetString(array2, 0, num2);

            }

            return CryptoHelper._encoding.GetString(array2);

        }

    }

文章转载来源:https://www.cnblogs.com/cocoan/p/4415209.html

你可能感兴趣的:(AES可逆加密算法)