互联网技术发展的很快,带来的问题也是很多,密码安全泄露事件、数据库事件等等层出不穷。
1、13 年腾讯 QQ 账号密码泄露事件。
2、15 年携程出现删库的事件。
3、前两天亚马逊出现四个小时的宕机事件,原因是因为程序员打错了一个字符,导致半个互联网瘫痪(说多少次了,我敲代码的时候不要打扰我,打错了一个字母可能导致整个互联网瘫痪)!
言归正传,今天主要讲一下加密的可逆与不可逆!
前两天我们讲了 RSA 和 DES 两种加密方式,他们都以加密密钥 key 进行加解密,这样可以得到加密后串,同时根据 key 和串可以得到解密密码,这两个加密按照分类是可逆的。
当然在一些场景下,我们可能安全着想,进行不可逆加密,平常见到的 MD5、SHA 就是最典型的代表!
MD5 和 SHA 加密的时候不用所谓的 key,同时一般情况下也不能进行解密,因为他是不可逆的,这样做能够保证一定的安全性,即使你有加密后的串,也不能进行破解(大牛勿论),比如你的数据库用户表 password 存的是 md5 加密后的串,那么即使别人得到也无可奈何!
下面通过两段代码展示一下 MD5 和 SHA 加密方法:
MD5:
- using System;
- using System.Security.Cryptography;
- using System.Text;
- namespace Utils.PassWord
- {
- /// <summary>
- /// MD5加解密
- /// </summary>
- public static class MD5
- {
- /// <summary>
- /// MD5 16位加密,不可逆
- /// </summary>
- /// <param name="password"></param>
- public static string Encrypt16Bit(string password)
- {
- MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
- string str = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(password)), 4, 8);
- str = str.Replace("-", "");
- return str;
- }
- /// <summary>
- /// MD5 32位加密,不可逆
- /// </summary>
- /// <param name="password"></param>
- public static string Encrypt32Bit(string password)
- {
- string pwd = "";
- System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
- byte[] str = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
- for (int i = 0; i < str.Length; i++) // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
- pwd = pwd + str[i].ToString("X"); // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
- return pwd;
- }
- /// <summary>
- /// MD5 64位加密,不可逆
- /// </summary>
- /// <param name="password"></param>
- public static string Encrypt64Bit(string password)
- {
- System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
- byte[] str = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
- return Convert.ToBase64String(str);
- }
- }
- }
- 调用方式
- public static void md5()
- {
- var str1 = MD5.Encrypt16Bit("123456789");
- var str2 = MD5.Encrypt32Bit("123456789");
- var str3 = MD5.Encrypt64Bit("123456789");
- }
SHA:
- using System.Linq;
- namespace Utils.PassWord
- {
- /// <summary>
- /// SHA加解密
- /// </summary>
- public static class SHA
- {
- /// <summary>
- /// SHA-1
- /// </summary>
- /// <param name="str"></param>
- public static string SHA_1(string str)
- {
- System.Security.Cryptography.SHA1CryptoServiceProvider SHA1CSP = new System.Security.Cryptography.SHA1CryptoServiceProvider();
- byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(str);
- byte[] bytHash = SHA1CSP.ComputeHash(bytValue);
- SHA1CSP.Clear();
- string hashstr = "", tempstr = "";
- for (int counter = 0; counter < bytHash.Count(); counter++)
- {
- long i = bytHash[counter] / 16;
- if (i > 9)
- tempstr = ((char)(i - 10 + 0x41)).ToString();
- else
- tempstr = ((char)(i + 0x30)).ToString();
- i = bytHash[counter] % 16;
- if (i > 9)
- tempstr += ((char)(i - 10 + 0x41)).ToString();
- else
- tempstr += ((char)(i + 0x30)).ToString();
- hashstr += tempstr;
- }
- return hashstr;
- }
- /// <summary>
- /// SHA-256
- /// </summary>
- /// <param name="str"></param>
- public static string SHA_256(string str)
- {
- System.Security.Cryptography.SHA256CryptoServiceProvider SHA256CSP = new System.Security.Cryptography.SHA256CryptoServiceProvider();
- byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(str);
- byte[] bytHash = SHA256CSP.ComputeHash(bytValue);
- SHA256CSP.Clear();
- string hashstr = "", tempstr = "";
- for (int counter = 0; counter < bytHash.Count(); counter++)
- {
- long i = bytHash[counter] / 16;
- if (i > 9)
- tempstr = ((char)(i - 10 + 0x41)).ToString();
- else
- tempstr = ((char)(i + 0x30)).ToString();
- i = bytHash[counter] % 16;
- if (i > 9)
- tempstr += ((char)(i - 10 + 0x41)).ToString();
- else
- tempstr += ((char)(i + 0x30)).ToString();
- hashstr += tempstr;
- }
- return hashstr;
- }
- /// <summary>
- /// SHA-384
- /// </summary>
- /// <param name="str"></param>
- public static string SHA_384(string str)
- {
- System.Security.Cryptography.SHA384CryptoServiceProvider SHA384CSP = new System.Security.Cryptography.SHA384CryptoServiceProvider();
- byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(str);
- byte[] bytHash = SHA384CSP.ComputeHash(bytValue);
- SHA384CSP.Clear();
- string hashstr = "", tempstr = "";
- for (int counter = 0; counter < bytHash.Count(); counter++)
- {
- long i = bytHash[counter] / 16;
- if (i > 9)
- tempstr = ((char)(i - 10 + 0x41)).ToString();
- else
- tempstr = ((char)(i + 0x30)).ToString();
- i = bytHash[counter] % 16;
- if (i > 9)
- tempstr += ((char)(i - 10 + 0x41)).ToString();
- else
- tempstr += ((char)(i + 0x30)).ToString();
- hashstr += tempstr;
- }
- return hashstr;
- }
- /// <summary>
- /// SHA-512
- /// </summary>
- /// <param name="str"></param>
- public static string SHA_512(string str)
- {
- System.Security.Cryptography.SHA512CryptoServiceProvider SHA512CSP = new System.Security.Cryptography.SHA512CryptoServiceProvider();
- byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(str);
- byte[] bytHash = SHA512CSP.ComputeHash(bytValue);
- SHA512CSP.Clear();
- string hashstr = "", tempstr = "";
- for (int counter = 0; counter < bytHash.Count(); counter++)
- {
- long i = bytHash[counter] / 16;
- if (i > 9)
- tempstr = ((char)(i - 10 + 0x41)).ToString();
- else
- tempstr = ((char)(i + 0x30)).ToString();
- i = bytHash[counter] % 16;
- if (i > 9)
- tempstr += ((char)(i - 10 + 0x41)).ToString();
- else
- tempstr += ((char)(i + 0x30)).ToString();
- hashstr += tempstr;
- }
- return hashstr;
- }
- }
- }
- 调用方式:
- public static void sha()
- {
- var aaa1 = SHA.SHA_1("123456789");
- var aaa2 = SHA.SHA_256("123456789");
- var aaa3 = SHA.SHA_384("123456789");
- var aaa4 = SHA.SHA_512("123456789");
- }
加解密这一块这几天也写了几篇,一些加解密原理性的东西我也不懂,这个涉及到更高深的密码学,臣妾做不到!
下一期我跟大家讨论讨论常用的 Office 组件功能,包括常用的 csv,excel,html,pdf,word 导入导出方法,预知详情,下回分解。
在这里感谢各位看官的支持,你的支持就是我的动力!
个人头条号:
来源: http://www.cnblogs.com/heimalang/p/6505187.html