DES加密解密

作者:追风剑情 发布于:2017-6-2 17:36 分类:C#

转载: http://blog.csdn.net/tammy520/article/details/23434931


public class DESCrypto
{
        private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        private static string KEY = "abcdefgh";

        ///<summary>
        ///DES加密字符串
        ///</summary>
        ///<param name="encryptString">待加密的字符串</param>
        ///<param name="encryptKey">加密密钥,要求为8位</param>
        ///<returns>加密成功返回加密后的字符串,失败返回源串</returns>
        public static string Encrypt(string encryptString, string encryptKey = "")
        {
            try
            {
                if (string.IsNullOrEmpty(encryptKey))
                    encryptKey = KEY;
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                byte[] bytes = Encrypt(inputByteArray, rgbKey);
                return Convert.ToBase64String(bytes);
            }
            catch (Exception e)
            {
                return encryptString;
            }
        }

        public static byte[] Encrypt(byte[] inputByteArray, byte[] rgbKey)
        {
            try
            {
                byte[] rgbIV = Keys;
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                cStream.Close();
                return mStream.ToArray();
            }
            catch (Exception e)
            {
                return inputByteArray;
            }
        }

        ///<summary>
        ///DES解密字符串
        ///</summary>
        ///<param name="decryptString">待解密的字符串</param>
        ///<param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        ///<returns>解密成功返回解密后的字符串,失败返源串</returns>
        public static string Decrypt(string decryptString, string decryptKey = "")
        {
            try
            {
                if (string.IsNullOrEmpty(decryptKey))
                    decryptKey = KEY;
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                byte[] bytes = Decrypt(inputByteArray, rgbKey);
                return Encoding.UTF8.GetString(bytes);
            }
            catch (Exception e)
            {
                return decryptString;
            }
        }

        public static byte[] Decrypt(byte[] inputByteArray, byte[] rgbKey)
        {
            try
            {
                byte[] rgbIV = Keys;//加密和解密必须使用相同的IV和Key
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                cStream.Close();
                return mStream.ToArray();
            }
            catch (Exception e)
            {
                return inputByteArray;
            }
        }

        ///<summary>
        /// 随机生成一个初始向量(IV)
        ///</summary>
        public static byte[] RandomIV()
        {
            byte[] randomBytes = new byte[8];
            System.Security.Cryptography.RNGCryptoServiceProvider rngServiceProvider = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rngServiceProvider.GetBytes(randomBytes);
            return randomBytes;
        }

        //生成一个随机key
        public static string RandomKey()
        {
            //ASCII码可显示字符范围
            int ASCII_MIN = 32;
            int ASCII_MAX = 126;
            char[] chars = new char[8];
            long tick = DateTime.Now.Ticks;
            System.Random rand = new System.Random();
            for (int i = 0; i < 8; i++)
            {
                char c = (char)rand.Next(ASCII_MIN, ASCII_MAX);
                chars[i] = c;
            }
            string key = new string(chars);
            return key;
        }
}


这个类已在Unity项目中使用。


标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号