图片 6

【日常笔记】字符串加密

====对称加密(向量)=====

便民md5加密:

        //SymmetricAlgorithm 所有对称算法的实现都必须从中继承的抽象基类
        private SymmetricAlgorithm mobjCryptoService;
        private string key;
        public Encrypt()
        {
            mobjCryptoService = new RijndaelManaged();
            key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";//自定义的密串
        }
        /// <summary>
        /// 获得密钥
        /// </summary>
        /// <returns>密钥</returns>
        private byte[] GetLegalKey()
        {
            string sTemp = key;
            mobjCryptoService.GenerateKey();// 当在派生类中重写时,生成用于该算法的随机密钥
            byte[] bytTemp = mobjCryptoService.Key;
            int KeyLength = bytTemp.Length;
            if (sTemp.Length > KeyLength)
                sTemp = sTemp.Substring(0, KeyLength);
            else if (sTemp.Length < KeyLength)
                sTemp = sTemp.PadRight(KeyLength, ' ');
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }
        /// <summary>
        /// 获得初始向量IV
        /// </summary>
        /// <returns>初试向量IV</returns>
        private byte[] GetLegalIV()
        {
            string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
            mobjCryptoService.GenerateIV();
            byte[] bytTemp = mobjCryptoService.IV;// 获取或设置对称算法的初始化向量
            int IVLength = bytTemp.Length;//获得一个 32 位整数,表示 System.Array 的所有维数中元素的总数
            if (sTemp.Length > IVLength)
                sTemp = sTemp.Substring(0, IVLength);
            else if (sTemp.Length < IVLength)
                sTemp = sTemp.PadRight(IVLength, ' ');
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }
        /// <summary>
        /// 加密方法(请先实例化Encrypt类)
        /// </summary>
        /// <param name="Source">待加密的串</param>
        /// <returns>经过加密的串</returns>
        public string EncrypStrByIV(string Source)
        {
            byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
            MemoryStream ms = new MemoryStream();
            mobjCryptoService.Key = GetLegalKey();
            mobjCryptoService.IV = GetLegalIV();
            ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
            cs.Write(bytIn, 0, bytIn.Length);
            cs.FlushFinalBlock();
            ms.Close();
            byte[] bytOut = ms.ToArray();
            return Convert.ToBase64String(bytOut);
        }
        /// <summary>
        /// 解密方法(请先实例化Encrypt类)
        /// </summary>
        /// <param name="Source">待解密的串</param>
        /// <returns>经过解密的串</returns>
        public string DecrypStrByIV(string Source)
        {
            byte[] bytIn = Convert.FromBase64String(Source);
            MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
            mobjCryptoService.Key = GetLegalKey();
            mobjCryptoService.IV = GetLegalIV();
            ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
            StreamReader sr = new StreamReader(cs);
            return sr.ReadToEnd();
        }

图片 1

 ========DES加密========

百度md5加密:

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="Text">待加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string DESEncrypt(string Text)
        {
            return DESEncrypt(Text, "ijnskijnse");
        }

        /// <summary> 
        /// DES加密数据 
        /// </summary> 
        /// <param name="Text">待加密的字符串</param> 
        /// <param name="sKey">加密密钥</param> 
        /// <returns>加密后的字符串</returns> 
        public static string DESEncrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray;
            inputByteArray = Encoding.Default.GetBytes(Text);
            des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }




        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="Text">待解密的字符串</param>
        /// <returns>解密后的明文</returns>
        public static string DESDecrypt(string Text)
        {
            return DESDecrypt(Text, "ijnskijnse");
        }

        /// <summary> 
        /// DES解密数据 
        /// </summary> 
        /// <param name="Text">待解密的字符串</param> 
        /// <param name="sKey">解密密钥</param> 
        /// <returns>解密后的明文</returns> 
        public static string DESDecrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            int len;
            len = Text.Length / 2;
            byte[] inputByteArray = new byte[len];
            int x, i;
            for (x = 0; x < len; x++)
            {
                i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Encoding.Default.GetString(ms.ToArray());
        }

图片 2

 ====倒序加1加密解密====

 

 /// <summary>
        /// 倒序加1加密
        /// </summary>
        /// <param name="rs"></param>
        /// <returns></returns>
        public static string EncryptOrderStr(string rs) //倒序加1加密 
        {
            byte[] by = new byte[rs.Length];
            for (int i = 0; i <= rs.Length - 1; i++)
            {
                by[i] = (byte)((byte)rs[i] + 1);
            }
            rs = "";
            for (int i = by.Length - 1; i >= 0; i--)
            {
                rs += ((char)by[i]).ToString();
            }
            return rs;
        }
        /// <summary>
        /// 顺序减1解码 
        /// </summary>
        /// <param name="rs"></param>
        /// <returns></returns>
        public static string DecryptOrderStr(string rs) //顺序减1解码 
        {
            byte[] by = new byte[rs.Length];
            for (int i = 0; i <= rs.Length - 1; i++)
            {
                by[i] = (byte)((byte)rs[i] - 1);
            }
            rs = "";
            for (int i = by.Length - 1; i >= 0; i--)
            {
                rs += ((char)by[i]).ToString();
            }
            return rs;
        }

MySQL

======Base64编解码=====

加密语法

        /// <summary>
        /// Base64编码
        /// </summary>
        /// <param name="code_type">编码类型</param>
        /// <param name="code">待编码的字符串</param>
        /// <returns></returns>
        public static string Base64Encode(string code_type, string code)
        {
            string encode = "";
            byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = code;
            }
            return encode;
        }
        /// <summary>
        /// Base64解码
        /// </summary>
        /// <param name="code_type">编码类型</param>
        /// <param name="code">带解码的字符串</param>
        /// <returns></returns>
        public static string Base64Decode(string code_type, string code)
        {
            string decode = "";
            byte[] bytes = Convert.FromBase64String(code);
            try
            {
                decode = Encoding.GetEncoding(code_type).GetString(bytes);
            }
            catch
            {
                decode = code;
            }
            return decode;
        }

MD5(加密字符串)

 

 

-- 中文加密
SELECT MD5('你好')

-- 中文加密匹配查询
SELECT * FROM 表名 WHERE 字段名 = MD5('你好')

加密结果:

图片 3

 

 

SQLSERVER

加密语法

substring(sys.fn_sqlvarbasetostr(HashBytes('MD5','加密字符串')),3,32)

 

select substring(sys.fn_sqlvarbasetostr(HashBytes('MD5','你好')),3,32)

加密结果:

图片 4

由此可知sqlserver加密与百度加密还是有区别的.在不知道在sqlserver怎么加密时,还是不要使用sqlserver加密。至于字母、数字、英文特殊字符还是可以用sqlserver加密的。

 

C# Md5加密:

加密代码:

public static string Sign(string prestr, string _input_charset)
 {
     StringBuilder sb = new StringBuilder(32);
     System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
     byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(prestr));
     for (int i = 0; i < t.Length; i++)
     {
         sb.Append(t[i].ToString("x").PadLeft(2, '0'));
     }

     return sb.ToString();
 }

加密结果:

图片 5

 

 Java加密:

加密代码:

 

package com;

import java.security.MessageDigest;


public class Md5Test {

    public static void main(String[] args) {

        System.out.println(Md5("你好"));
    }

    public static String Md5(String str) {                            
        String re_md5 = new String();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");

            byte[] data = str.getBytes("UTF-8");

            md.update(data);
            byte b[] = md.digest();

            int i;

            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }

            re_md5 = buf.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

       return re_md5;
    }
}

 

加密结果:

图片 6