对称加密算法,非对称加密算法常见的加密算法可以分成三类,
对称加密算法,非对称加密算法和Hash算法。
对称加密(也叫私钥加密)指加密和解密使用相同密钥的加密算法。对称加密算法的优点在于加解密的高速度和使用长密钥时的难破解性。假设两个用户需要使用对称加密方法加密然后交换数据,则用户最少需要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);
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();f
sOut.Close();
用图片加密(RC2CryptoServiceProvider )
FileStreamfsPic = 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 IVbyte[] byIv = new byte[8];
fsPic.Read(bykey, 0, 16);
fsPic.Read(byIv, 0, 8);
RC2CryptoServiceProvider desc = newRC2CryptoServiceProvider();//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 IVbyte[] 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 = newRC2CryptoServiceProvider();//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 = newMD5CryptoServiceProvider();
txtJia.Text =ASCIIEncoding.ASCII.GetString(M5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(txtStart.Text)));
对称加密算法,非对称加密算法和Hash算法。
对称加密(也叫私钥加密)指加密和解密使用相同密钥的加密算法。对称加密算法的优点在于加解密的高速度和使用长密钥时的难破解性。假设两个用户需要使用对称加密方法加密然后交换数据,则用户最少需要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);
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();f
sOut.Close();
用图片加密(RC2CryptoServiceProvider )
FileStreamfsPic = 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 IVbyte[] byIv = new byte[8];
fsPic.Read(bykey, 0, 16);
fsPic.Read(byIv, 0, 8);
RC2CryptoServiceProvider desc = newRC2CryptoServiceProvider();//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 IVbyte[] 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 = newRC2CryptoServiceProvider();//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 = newMD5CryptoServiceProvider();
txtJia.Text =ASCIIEncoding.ASCII.GetString(M5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(txtStart.Text)));