MD5与hash加密的方法概要收藏

来源:互联网 发布:linux less 查找 编辑:程序博客网 时间:2024/06/08 13:30

  MD5与hash加密的方法概要收藏
 MD5的全称是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由Mit Laboratory for Computer Science和Rsa data security inc的Ronald l. rivest开发出来,经md2、md3和md4发展而来。它的作用是让大容量信息在用数字签名软件签署私人密匙前被“压缩”成一种保密的格式(就是把一个任意长度的字节串变换成一定长的大整数)。不管是md2、md4还是md5,它们都需要获得一个随机长度的信息并产生一个128位的信息摘要。

  加密哈希函数将任意长度的二进制字符串映射为固定长度的小型二进制字符串。加密哈希函数有这样一个属性:在计算上不大可能找到散列为相同的值的两个不同的输入;也就是说,两组数据的哈希值仅在对应的数据也匹配时才会匹配。数据的少量更改会在哈希值中产生不可预知的大量更改。所以你很难从加密后的文字中找到蛛丝马迹。

  SHA1的全称是Secure Hash Algorithm(安全哈希算法)

  MD5算法的哈希值大小为128位。而SHA1算法的哈希值大小为160位。两种算法都是不可逆。

  虽说2004年8月17日的美国加州圣巴巴拉的国际密码学会议(Crypto’2004)上,来自中国山东大学的王小云教授做了破译MD5、HAVAL-128、MD4和RIPEMD算法的报告,公布了MD系列算法的破解结果。宣告了固若金汤的世界通行密码标准MD5的堡垒轰然倒塌,引发了密码学界的轩然大波。但是我觉得对于我们做普通的软件来说,这个加密安全程度已经足够使用了。

  我们平常用的最多的无非就是加密用户密码,把加密好的密码存储到数据库中,进行密码比较的时候,把用户输入的密码再进行加密,然后与数据库中的密文进行比较。至于asp.net类中是如何实现加密算法的,这个我们不需要关心,会用就行了。

  下面就是Asp.net中几种加密方法。加密算法有两种,也就是上面提到的MD5和SHA1,这里我举的例子是以MD5为例,SHA1大致相同,只是使用的类不一样。

  MD5相关类:

System.Security.Cryptography.MD5
System.Security.Cryptography.MD5CryptoServiceProvider()
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5")

SHA1相关类:

System.Security.Cryptography.SHA1
System.Security.Cryptography.SHA1CryptoServiceProvider()
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "SHA1")

1/**//// <summary>///
2方法一:通过使用 new 运算符创建对象

3/// 〈/summary〉
4 ///
〈param name="strSource"〉需要加密的明文〈/param〉5 ///
〈returns〉返回16位加密结果,该结果取32位加密结果的第9位到25位〈/returns〉6 public string Get_MD5_Method1(string strSource)
7 {

8  //new
9 System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
10
11  //获取密文字节数组

12  byte[] bytResult = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strSource));

13

14  //转换成字符串,并取9到25位
15  string strResult = BitConverter.ToString(bytResult, 4, 8);
16  //转换成字符串,32位

17  //string strResult = BitConverter.ToString(bytResult);
18
19  //BitConverter转换出来的字符串会在每个字符中间产生一个分隔符,需要去除掉
20  strResult = strResult.Replace("-", "");
21  return strResult;

22 }
23
24 /**//// 〈summary〉
25 /// 方法二:通过调用特定加密算法的抽象类上的 Create 方法,创建实现特定加密算法的对象。
26 /// 〈/summary〉
27 /// 〈param name="strSource"〉需要加密的明文〈/param〉
28 /// 〈returns〉返回32位加密结果〈/returns〉
29 public string Get_MD5_Method2(string strSource)
30 {
31  string strResult = "";
32
33  //Create
34  System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create(); 3536  //注意编码UTF8、UTF7、Unicode等的选择 37  byte[] bytResult = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strSource)); 3839  //字节类型的数组转换为字符串
40  for (int i = 0; i 〈 bytResult.Length; i++)
41  {
42 //16进制转换 43 strResult = strResult + bytResult[i].ToString("X"); 44  }
45  return strResult;
46 }
47
48 /**//// 〈summary〉
49 /// 方法三:直接使用HashPasswordForStoringInConfigFile生成
50 /// 〈/summary〉51 /// 〈param name="strSource"〉需要加密的明文〈/param〉
52 /// 〈returns〉返回32位加密结果〈/returns〉53 public string Get_MD5_Method3(string strSource)
54 {
55  return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5");
56 }

这些加密函数都是在服务器端执行,也就是说,当用户输入密码后,从客户端到服务器端传输时,用户的密码没有任何保护,很危险。银行的做法是在客户端安装ActiveX控件,在客户端就把一些重要信息进行加密,再发送。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yefengzhixia/archive/2009/05/22/4209312.aspx

 

对称加密算法,非对称加密算法和Hash算法
 
常见的加密算法可以分成三类,对称加密算法,非对称加密算法和Hash算法。(System.Security.Cryptography)

对称加密(也叫私钥加密)

指加密和解密使用相同密钥的加密算法。对称加密算法的优点在于加解密的高速度和使用长密钥时的难破解性。假设两个用户需要使用对称加密方法加密然后交换数据,则用户最少需要2个密钥并交换使用,如果企业内用户有n个,则整个企业共需要n×(n-1) 个密钥,密钥的生成和分发将成为企业信息部门的恶梦。对称加密算法的安全性取决于加密密钥的保存情况,但要求企业中每一个持有密钥的人都保守秘密是不可能的,他们通常会有意无意的把密钥泄漏出去——如果一个用户使用的密钥被入侵者所获得,入侵者便可以读取该用户密钥加密的所有文档,如果整个企业共用一个加密密钥,那整个企业文档的保密性便无从谈起。

DESCryptoServiceProvider

RC2CryptoServiceProvider

RijndaelManaged

TripleDESCryptoServiceProvider

//例加密文本文件(RijndaelManaged )

byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };

byte[] IV ={ 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 };

RijndaelManaged myRijndael = new RijndaelManaged();

FileStream fsOut = File.Open(strOutName, FileMode.Create, FileAccess.Write);//strOutName文件名及路径

FileStream fsIn = File.Open(strPath, FileMode.Open, FileAccess.Read);//strPath原文本文件

CryptoStream csDecrypt=new CryptoStream(fsOut, myRijndael.CreateEncryptor(key, IV), CryptoStreamMode.Write);

//读加密文本

 BinaryReader br = new BinaryReader(fsIn);

 csDecrypt.Write(br.ReadBytes((int)fsIn.Length), 0, (int)fsIn.Length);

 csDecrypt.FlushFinalBlock();

 csDecrypt.Close();

 fsIn.Close();

 fsOut.Close();

//解密文件

byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };

byte[] IV ={ 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 };

RijndaelManaged myRijndael = new RijndaelManaged();

FileStream fsOut = File.Open(strPath, FileMode.Open, FileAccess.Read);

CryptoStream csDecrypt = new CryptoStream(fsOut, myRijndael.CreateDecryptor(key, IV), CryptoStreamMode.Read);

StreamReader sr = new StreamReader(csDecrypt);//把文件读出来

StreamWriter sw = new StreamWriter(strInName);//解密后文件写入一个新的文件

sw.Write(sr.ReadToEnd());

sw.Flush();

sw.Close();

sr.Close();

fsOut.Close();

用图片加密(RC2CryptoServiceProvider )

 FileStream fsPic = new FileStream(pictureBox1.ImageLocation, FileMode.Open, FileAccess.Read);

 //加密文件流(textBox1.Text是文件名及路径)

FileStream fsText = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);

byte[] bykey = new byte[16]; //初始化Key IV

byte[] byIv = new byte[8];

fsPic.Read(bykey, 0, 16);

fsPic.Read(byIv, 0, 8)

RC2CryptoServiceProvider desc = new RC2CryptoServiceProvider();//desc进行加密

BinaryReader br = new BinaryReader(fsText);//从要加密的文件中读出文件内容

FileStream fsOut = File.Open(strLinPath, FileMode.Create, FileAccess.Write); // strLinPath临时加密文件路径

CryptoStream cs = new CryptoStream(fsOut, desc.CreateEncryptor(bykey, byIv), CryptoStreamMode.Write);//写入临时加密文件 cs.Write(br.ReadBytes((int)fsText.Length), 0, (int)fsText.Length);//写入加密流

cs.FlushFinalBlock();

cs.Flush();

cs.Close();

fsPic.Close();

fsText.Close();

fsOut.Close();

用图片解密

FileStream fsPic = new FileStream(pictureBox1.ImageLocation, FileMode.Open, FileAccess.Read); //图片流

FileStream fsOut = File.Open(textBox1.Text, FileMode.Open, FileAccess.Read); //解密文件流

byte[] bykey = new byte[16]; //初始化Key IV

byte[] byIv = new byte[8];

fsPic.Read(bykey, 0, 16);

fsPic.Read(byIv, 0, 8);

string strPath = textBox1.Text;//加密文件的路径

int intLent = strPath.LastIndexOf("""") + 1;

int intLong = strPath.Length;

string strName = strPath.Substring(intLent, intLong - intLent);//要加密的文件名称

string strLinPath = "C:""" + strName;//临时解密文件路径

FileStream fs = new FileStream(strLinPath, FileMode.Create, FileAccess.Write);

RC2CryptoServiceProvider desc = new RC2CryptoServiceProvider();//desc进行解密

CryptoStream csDecrypt = new CryptoStream(fsOut, desc.CreateDecryptor(bykey, byIv), CryptoStreamMode.Read);//读出加密文件

BinaryReader sr = new BinaryReader(csDecrypt);//从要加密流中读出文件内容

BinaryWriter sw = new BinaryWriter(fs);//写入解密流

sw.Write(sr.ReadBytes(Convert.ToInt32(fsOut.Length)));

sw.Flush();

sw.Close();

sr.Close();

fs.Close();

fsOut.Close();

fsPic.Close();

csDecrypt.Flush();

File.Delete(textBox1.Text.TrimEnd());//删除原文件

File.Copy(strLinPath, textBox1.Text);//复制加密文件

File.Delete(strLinPath);//删除临时文件

非对称加密(公钥加密)

指加密和解密使用不同密钥的加密算法,也称为公私钥加密。假设两个用户要加密交换数据,双方交换公钥,使用时一方用对方的公钥加密,另一方即可用自己的私钥解密。如果企业中有n个用户,企业需要生成n对密钥,并分发n个公钥。由于公钥是可以公开的,用户只要保管好自己的私钥即可,因此加密密钥的分发将变得 十分简单。同时,由于每个用户的私钥是唯一的,其他用户除了可以可以通过信息发送者的公钥来验证信息的来源是否真实,还可以确保发送者无法否认曾发送过该信息。非对称加密的缺点是加解密速度要远远慢于对称加密,在某些极端情况下,甚至能比非对称加密慢上1000倍。

DSACryptoServiceProvider

RSACryptoServiceProvider

//加密

UnicodeEncoding   encoding   =   new   UnicodeEncoding();

byte[]   PasswordBytes   =   encoding.GetBytes(password);//将密码转换为字节数组

RSACryptoServiceProvider   crypt=new   RSACryptoServiceProvider();//RSA加密算法,非对称

PasswordBytes=crypt.Encrypt(password   ,false);//加密字节数组,这是加密后的密码值,放入数据库中的表字段中。

string   key=crypt.ToXmlString(true);//输出密钥为XML格式的字符串,且包含私钥,

这个字符串要作为数据库表中的一个字段同用户的密码放在一起。

//解密

RSACryptoServiceProvider   crypt=new   RSACryptoServiceProvider();//已随机生成了一个密钥对

crypt.Clear();//毁掉当前密钥对

crypt.FromXmlString(key)//输入密钥对,key是从数据库表字段中读取的那个XML格式的字符串,即密钥字段

PasswordBytes=crypt.Decrypt(password   ,false);//解密字节数组,返回原始密码给用户

上面方法的一个特点是每个用户对应一个密钥(包含公钥和私钥),它们都是随机生成的,所以各不相同。不过缺点也是很明显的,

就是密钥存储在数据库中,如果数据库被攻破密钥就泄漏了。

还有另外一个方法就是依照上面方法随机生成一个密钥对(包含公钥和私钥),通过ToXmlString(true)方法导出,

然后把这个XML字符串格式的密钥放到你的Web程序的Web.config文件的AppSetting节点里面,

然后通过FromXmlString(key)方法读入密钥,这样就意味着所有的用户密码都用同一个密钥对加密和解密。

Hash算法(哈希值)

Hash算法特别的地方在于它是一种单向算法,用户可以通过Hash算法对目标信息生成一段特定长度的唯一的Hash值,却不能通过这个Hash值重新获得目标信息。因此Hash算法常用在不可还原的密码存储、信息完整性校验等。

HMACSHA1

MACTripleDES

MD5CryptoServiceProvider

SHA1Managed

SHA256Managed

SHA384Managed

SHA512Managed

MD5CryptoServiceProvider M5 = new MD5CryptoServiceProvider();

txtJia.Text =ASCIIEncoding.ASCII.GetString(M5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(txtStart.Text)));

这里也有一个加密文章:http://www.cnblogs.com/erwin/archive/2009/04/14/1435551.html

 

 

 

原创粉丝点击