加解密总结(附助手类)
Posted fanfan-90
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加解密总结(附助手类)相关的知识,希望对你有一定的参考价值。
对称加密算法:DES、AES、IDEA、RC2、RC4、SKIPJACK……
加解密使用相同密钥,这个是对称加密。对称加密优点是速度快
非对称加密算法:RSA、DSA、DH、ECC、EL GAMAL……
公钥加密数据,然后私钥解密的情况被称为加密解密;
因为公钥加密的数据只有它相对应的私钥可以解开,所以你可以把公钥给人和人,让他加密他想要传送给你的数据,这个数据只有到了有私钥的你这里,才可以解开成有用的数据,其他人就是得到了,也看懂内容
实际应用中,一般都是和对方交换公钥,然后你要发给对方的数据,用他的公钥加密,他得到后用他的私钥解密,他要发给你的数据,用你的公钥加密,你得到后用你的私钥解密,这样最大程度保证了安全性.
缺点速度慢
Hash算法:MD5、SHA1、SHA256……
无论消息有多长,计算出的摘要长度都是固定的,MD5没有SHA算法安全
数字签名:
私钥签名和公钥验证签名;RSA可以用来做签名、验签
如果你用你的私钥对数据进行签名,那这个数据就只有配对的公钥可以解开,有这个私钥的只有你,所以如果配对的公钥解开了数据,就说明这数据是你发的,相反,则不是.这个被称为签名。数字签名的意义就是这些数据与原文数据比对是否修改过。
CA证书如何保证网站数据安全:
网站向CA机构申请证书,网站方提供网站信息及公钥给CA机构,CA机构使用自己的私钥对公钥签名,以防篡改。
网站服务器安装CA证书
浏览器发起请求,先请求CA证书,浏览器端保存了CA机构的公钥,对证书验签,验证通过,取出证书中的公钥
使用公钥对http请求信息加密,发起http请求
网站服务器接收到http请求后使用私钥解密。这样就可以防止http请求中的重要信息被拦截者看到,如密码等信息
如果网站没有安装CA证书,也可以在客户端使用RSA加密,公钥保存在浏览器端就可以,我有一篇文章对此有介绍。
DES助手类:
1 /// <summary> 2 /// 可逆对称加密 密钥长度8 3 /// </summary> 4 public class DesEncrypt 5 { 6 private static string _key = "fanfanfanfanfan"; 7 private static byte[] _rgbKey = ASCIIEncoding.ASCII.GetBytes(_key.Substring(0, 8)); 8 private static byte[] _rgbIV = ASCIIEncoding.ASCII.GetBytes(_key.Insert(0, "w").Substring(0, 8)); 9 10 /// <summary> 11 /// DES 加密 12 /// </summary> 13 /// <param name="text">需要加密的值</param> 14 /// <returns>加密后的结果</returns> 15 public static string Encrypt(string text) 16 { 17 DESCryptoServiceProvider dsp = new DESCryptoServiceProvider(); 18 using (MemoryStream memStream = new MemoryStream()) 19 { 20 CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateEncryptor(_rgbKey, _rgbIV), CryptoStreamMode.Write); 21 StreamWriter sWriter = new StreamWriter(crypStream); 22 sWriter.Write(text); 23 sWriter.Flush(); 24 crypStream.FlushFinalBlock(); 25 memStream.Flush(); 26 return Convert.ToBase64String(memStream.GetBuffer(), 0, (int)memStream.Length); 27 } 28 } 29 30 /// <summary> 31 /// DES 解密 32 /// </summary> 33 /// <param name="encryptText"></param> 34 /// <returns>解密后的结果</returns> 35 public static string Decrypt(string encryptText) 36 { 37 DESCryptoServiceProvider dsp = new DESCryptoServiceProvider(); 38 byte[] buffer = Convert.FromBase64String(encryptText); 39 40 using (MemoryStream memStream = new MemoryStream()) 41 { 42 CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateDecryptor(_rgbKey, _rgbIV), CryptoStreamMode.Write); 43 crypStream.Write(buffer, 0, buffer.Length); 44 crypStream.FlushFinalBlock(); 45 return ASCIIEncoding.UTF8.GetString(memStream.ToArray()); 46 } 47 } 48 }
AES助手类:
1 /// <summary> 2 /// AES加密解密 4 /// 密码必须是16位,否则会报错哈 5 /// </summary> 6 public class AESCryptoHelper 7 { 8 /// <summary> 9 /// APP默认加密Key 10 /// </summary> 11 public static string Default_AESKey = "1111111111111111";//必须是十六位 12 13 /// <summary> 14 /// AES 加密 15 /// </summary> 16 /// <param name="plainText">明文</param> 17 /// <param name="key">密码必须是16位,否则会报错哈</param> 18 /// <returns>密文</returns> 19 public static string Encrypt(string plainText, string key) 20 { 21 string result = null; 22 if (string.IsNullOrEmpty(plainText)) 23 { 24 return result; 25 } 26 byte[] plainTextArray = Encoding.UTF8.GetBytes(plainText); 27 using (RijndaelManaged rijndaelManaged = new RijndaelManaged 28 { 29 Key = Encoding.UTF8.GetBytes(key), 30 Mode = CipherMode.ECB, 31 Padding = PaddingMode.PKCS7 32 }) 33 { 34 using (ICryptoTransform cryptoTransform = rijndaelManaged.CreateEncryptor()) 35 { 36 byte[] resultArray = cryptoTransform.TransformFinalBlock(plainTextArray, 0, plainTextArray.Length); 37 result = Convert.ToBase64String(resultArray, 0, resultArray.Length); 38 Array.Clear(resultArray, 0, resultArray.Length); 39 resultArray = null; 40 } 41 } 42 Array.Clear(plainTextArray, 0, plainTextArray.Length); 43 plainTextArray = null; 44 return result; 45 } 46 47 /// <summary> 48 /// AES 解密 49 /// </summary> 50 /// <param name="encryptText">密文</param> 51 /// <param name="key">密码必须是16位,否则会报错哈</param> 52 /// <returns>明文</returns> 53 public static string Decrypt(string encryptText, string key) 54 { 55 string result = null; 56 if (string.IsNullOrEmpty(encryptText)) 57 { 58 return result; 59 } 60 byte[] encryptTextArray = Convert.FromBase64String(encryptText); 61 using (RijndaelManaged rijndaelManaged = new RijndaelManaged 62 { 63 Key = Encoding.UTF8.GetBytes(key), 64 Mode = CipherMode.ECB, 65 Padding = PaddingMode.PKCS7 66 }) 67 { 68 using (ICryptoTransform cryptoTransform = rijndaelManaged.CreateDecryptor()) 69 { 70 byte[] resultArray = cryptoTransform.TransformFinalBlock(encryptTextArray, 0, encryptTextArray.Length); 71 result = Encoding.UTF8.GetString(resultArray); 72 Array.Clear(resultArray, 0, resultArray.Length); 73 resultArray = null; 74 } 75 } 76 Array.Clear(encryptTextArray, 0, encryptTextArray.Length); 77 encryptTextArray = null; 78 return result; 79 } 80 81 /// <summary> 82 /// AES 加密(URL传参数加密) 83 /// </summary> 84 /// <param name="plainText">明文</param> 85 /// <param name="key"></param> 86 /// <returns>加密之后URL编码</returns> 87 public static string UrlEncrypt(string plainText, string key) 88 { 89 string encryptText = Encrypt(plainText, key); 90 return HttpUtilityHelper.UrlEncode(encryptText); 91 } 92 93 /// <summary> 94 /// AES 解密(URL传参数解密) 95 /// </summary> 96 /// <param name="encodeEncryptText">URL编码之后的密文</param> 97 /// <param name="key"></param> 98 /// <returns>明文</returns> 99 public static string UrlDecrypt(string encodeEncryptText, string key) 100 { 101 if (!string.IsNullOrEmpty(encodeEncryptText)) 102 { 103 string encodeText = HttpUtilityHelper.UrlDecode(encodeEncryptText); 104 return Decrypt(encodeText, key); 105 } 106 return ""; 107 } 108 }
MD5助手类:
1 /// <summary> 2 /// MD5 3 /// </summary> 4 public class MD5Encrypt 5 { 6 #region MD5 7 /// <summary> 8 /// MD5加密,和动网上的16/32位MD5加密结果相同, 9 /// 使用的UTF8编码 10 /// </summary> 11 /// <param name="source">待加密字串</param> 12 /// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param> 13 /// <returns>加密后的字串</returns> 14 public static string Encrypt(string source, int length = 32)//默认参数 15 { 16 if (string.IsNullOrEmpty(source)) return string.Empty; 17 HashAlgorithm provider = CryptoConfig.CreateFromName("MD5") as HashAlgorithm; 18 byte[] bytes = Encoding.UTF8.GetBytes(source);//这里需要区别编码的 19 byte[] hashValue = provider.ComputeHash(bytes); 20 StringBuilder sb = new StringBuilder(); 21 switch (length) 22 { 23 case 16://16位密文是32位密文的9到24位字符 24 for (int i = 4; i < 12; i++) 25 { 26 sb.Append(hashValue[i].ToString("x2")); 27 } 28 break; 29 case 32: 30 for (int i = 0; i < 16; i++) 31 { 32 sb.Append(hashValue[i].ToString("x2")); 33 } 34 break; 35 default: 36 for (int i = 0; i < hashValue.Length; i++) 37 { 38 sb.Append(hashValue[i].ToString("x2")); 39 } 40 break; 41 } 42 return sb.ToString(); 43 } 44 #endregion MD5 45 46 #region MD5摘要 47 /// <summary> 48 /// 获取文件的MD5摘要 49 /// </summary> 50 /// <param name="fileName"></param> 51 /// <returns></returns> 52 public static string AbstractFile(string fileName) 53 { 54 using (FileStream file = new FileStream(fileName, FileMode.Open)) 55 { 56 return AbstractFile(file); 57 } 58 } 59 60 /// <summary> 61 /// 根据stream获取文件摘要 62 /// </summary> 63 /// <param name="stream"></param> 64 /// <returns></returns> 65 public static string AbstractFile(Stream stream) 66 { 67 MD5 md5 = new MD5CryptoServiceProvider(); 68 byte[] retVal = md5.ComputeHash(stream); 69 70 StringBuilder sb = new StringBuilder(); 71 for (int i = 0; i < retVal.Length; i++) 72 { 73 sb.Append(retVal[i].ToString("x2")); 74 } 75 return sb.ToString(); 76 } 77 #endregion 78 }
RSA助手类:
1 /// <summary> 2 /// RSA 3 /// </summary> 4 public class RsaEncrypt 5 { 6 /// <summary> 7 /// 生成公钥、私钥 8 /// </summary> 9 /// <returns>Encrypt Decrypt</returns> 10 public static KeyValuePair<string, string> GetKeyPair() 11 { 12 RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); 13 string publicKey = RSA.ToXmlString(false); 14 string privateKey = RSA.ToXmlString(true); 15 return new KeyValuePair<string, string>(publicKey, privateKey); 16 } 17 18 /// <summary> 19 /// 加密:内容+加密key 20 /// </summary> 21 /// <param name="content"></param> 22 /// <param name="publicKey">加密key</param> 23 /// <returns></returns> 24 public static string Encrypt(string content, string publicKey) 25 { 26 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 27 rsa.FromXmlString(publicKey); 28 UnicodeEncoding ByteConverter = new UnicodeEncoding(); 29 byte[] DataToEncrypt = ByteConverter.GetBytes(content); 30 byte[] resultBytes = rsa.Encrypt(DataToEncrypt, false); 31 return Convert.ToBase64String(resultBytes); 32 } 33 34 /// <summary> 35 /// 解密 内容+解密key 36 /// </summary> 37 /// <param name="content"></param> 38 /// <param name="privateKey">解密key</param> 39 /// <returns></returns> 40 public static string Decrypt(string content, string privateKey) 41 { 42 byte[] dataToDecrypt = Convert.FromBase64String(content); 43 RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); 44 RSA.FromXmlString(privateKey); 45 byte[] resultBytes = RSA.Decrypt(dataToDecrypt, false); 46 UnicodeEncoding ByteConverter = new UnicodeEncoding(); 47 return ByteConverter.GetString(resultBytes); 48 } 49 50 51 /// <summary> 52 /// 可以合并在一起的,,每次产生一组新的密钥 53 /// </summary> 54 /// <param name="content"></param> 55 /// <param name="encryptKey">加密key</param> 56 /// <param name="decryptKey">解密key</param> 57 /// <returns>加密后结果</returns> 58 private static string Encrypt(string content, out string publicKey, out string privateKey) 59 { 60 RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(); 61 publicKey = rsaProvider.ToXmlString(false); 62 privateKey = rsaProvider.ToXmlString(true); 63 64 UnicodeEncoding ByteConverter = new UnicodeEncoding(); 65 byte[] DataToEncrypt = ByteConverter.GetBytes(content); 66 byte[] resultBytes = rsaProvider.Encrypt(DataToEncrypt, false); 67 return Convert.ToBase64String(resultBytes); 68 } 69 }
以上是关于加解密总结(附助手类)的主要内容,如果未能解决你的问题,请参考以下文章