Winform 字符串加密/解密,锁屏小工具的拓展(二)
这是我的舞台,虽然我很草根,写的东西没什么技术含量,也没有人鼓掌,但是有人看,这就足够了。
大家好才是真的好,我将一如既往的曝光我的草根Code。
上次我们完成了锁屏工具的雏形,但是没有实现用户自定义密码。今天将实现密码的加密与解密,为后面把密码保存在注册表中做准备。
我所使用的字符串加密方法并不是我写的,也不是很火的MD5等加密方法,仅仅是用.net中现有的类来处理字符串,实现加密的过程。先看代码,再讲解使用方法:
using System; using System.Security.Cryptography; using System.Text; namespace LockScreen { /// <summary> /// DES加密/解密类。 /// </summary> public class DESEncrypt { public DESEncrypt() { } #region ========加密======== /// <summary> /// 加密 /// </summary> /// <param name="Text"></param> /// <returns></returns> public static string Encrypt(string Text) { return Encrypt(Text,"zane"); } /// <summary> /// 加密数据 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Encrypt(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(); } #endregion #region ========解密======== /// <summary> /// 解密 /// </summary> /// <param name="Text"></param> /// <returns></returns> public static string Decrypt(string Text) { return Decrypt(Text, "zane"); } /// <summary> /// 解密数据 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Decrypt(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()); } #endregion } }
加密和解密方法都有+2重载,因为这个算法中需要两个加密/解密“种子”,这两个种子用于参与加密/解密的运算过程,种子不同,加密/解密的结果也就不一样,所以就算别人知道你的算法,但是不知道你的种子参数,还是不能进行解密(我说的不能解密是狭义的,别老拿FBI级别的队伍说事)。
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5"),这里有两个参数,这两个参数就是刚刚提到的加密/解密种子,为求简单,方法中只把第一个种子作为参数作为调用时输入,第二个参数直接写死,为“MD5”。虽然这里写了个md5,顺便也通过一个我们公司的笑话说说md5加密。
某天,BOSS不知道在哪搞到了md5加密的源码,然后召集队伍,下达了命令,要求一个星期之内研究出md5解密。因为BOSS知道:md5加密是让数据的每一个0和1参与运算,加密结果为一个长度为32的字符串。由于是让每一个字节都参与了运算,所以加密的结果跟每一个字节都有关。既然加密后的结果跟加密前的每个字节都有关,那么肯定能根据加密的结果反推出加密前的数据。
忽然间老板雄起了,因为一旦实现md5解密,那么就能根据一个长度为32的字符串反推出加密前的数据,加密前的数据,它可能是一部20G的高清大片。这个意义就非比寻常了,那将是世界上最先进的压缩技术——再大的数据也能压成32长度的字符串。解压即解密。
如果在U盘里面内置一个md5加解密程序,一个U盘的容量将达到理论上的无限大…BOSS越想越兴奋。
但是当我们告诉他不可能实现的时候老板显得很不解:“源代码都给你们了,这都搞不定!这么简单的事情!点一下就OK了嘛!……”。
事情最终不了了之,讲到这里也说明md5加密是不可逆的,是不能解密的,它是一种有损加密。也就是加密后的数据比加密前要少。上面贴出来的加密算法是可以被解密的,它加密后的数据比加密前要多,属于无损加密。
今天就写到这里了,明天我们将会把加密后的密码储存在注册表中,同时讲解注册表的“增删改查”。