加密方式:将 明文 +  六位随机码 进行加密

string safeCode = RandSafeCode.RandCode(6, true);
string temp = pwd + safeCode;
string ciphertext = MD5.GetMD5Upper(temp);
View Code

 

生成随机码:

    public class RandSafeCode
    {
        /// <summary>
        /// 生成随机字母与数字
        /// </summary>
        /// <param name="Length">生成长度</param>
        /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
        /// <returns></returns>
        public static string RandCode(int Length, bool Sleep)
        {
            if (Sleep)
                System.Threading.Thread.Sleep(3);
            char[] Pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
            string result = "";
            int n = Pattern.Length;
            System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            for (int i = 0; i < Length; i++)
            {
                int rnd = random.Next(0, n);
                result += Pattern[rnd];
            }
            return result;
        }
    }
View Code

 

加解密:

    public class MD5
    {
        public static string GetMD5Upper(string value)
        {
            byte[] b = Encoding.Default.GetBytes(value);
            b = new MD5CryptoServiceProvider().ComputeHash(b);
            string ret = "";
            for (int i = 0; i < b.Length; i++)
            {
                ret += b[i].ToString("X").PadLeft(2, '0');
            }
            return DesEncrypt(ret);
        }
 
        public const string DEFAULT_ENCRYPT_KEY = "1234567890123";
        // <summary>
        /// 使用默认加密
        /// </summary>
        /// <param name="strText"></param>
        /// <returns></returns>
        public static string DesEncrypt(string text)
        {
            if (string.IsNullOrEmpty(text))
                return "";
            try
            {
                return DesEncrypt(text, DEFAULT_ENCRYPT_KEY);
            }
            catch
            {
                return "";
            }
        }

        public static string DesEncrypt(string strText, string strEncrKey)
        {
            byte[] byKey = null;
            byte[] IV = { 0x22, 0x37, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            byKey = Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }

        /// <summary>
        /// 使用默认解密
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string DesDecrypt(string cleartext)
        {
            if (string.IsNullOrEmpty(cleartext))
                return "";
            try
            {
                return DesDecrypt(cleartext, DEFAULT_ENCRYPT_KEY);
            }
            catch
            {
                return "";
            }
        }
 
        public static string DesDecrypt(string text, string decrKey)
        {
            byte[] byKey = null;
            byte[] IV = { 0x22, 0x37, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            byte[] inputByteArray = new Byte[text.Length];
 
            byKey = Encoding.UTF8.GetBytes(decrKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(text);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            Encoding encoding = new UTF8Encoding();
            return encoding.GetString(ms.ToArray());
        }
    ​}    ​
View Code

 

DES是对称密码,是可逆的,要解密时,需要先从数据库取出随机码,所以在保存数据时,要保存密文和随机码

还有其他的对称密码和非对称密码,转一篇文章以便学习

http://www.cnblogs.com/wuhuacong/archive/2010/09/30/1839119.html

文章出处撰写人:伍华聪  http://www.iqidi.com 

posted on 2016-01-25 09:52  雪落千寒  阅读(239)  评论(0编辑  收藏  举报