using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace MyEncrypt { /// <summary> /// DES AES Blowfish /// 对称加密算法的优点是速度快, /// 缺点是密钥管理不方便,要求共享密钥。 /// 可逆对称加密 密钥长度8 /// </summary> public class DesEncrypt { //8位长度 private static string KEY = "ruanmou1"; private static byte[] rgbKey = ASCIIEncoding.ASCII.GetBytes(KEY.Substring(0, 8)); private static byte[] rgbIV = ASCIIEncoding.ASCII.GetBytes(KEY.Insert(0, "w").Substring(0, 8)); /// <summary> /// DES 加密 /// </summary> /// <param name="strValue"></param> /// <returns></returns> public static string Encrypt(string strValue) { DESCryptoServiceProvider dsp = new DESCryptoServiceProvider(); using (MemoryStream memStream = new MemoryStream()) { CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); StreamWriter sWriter = new StreamWriter(crypStream); sWriter.Write(strValue); sWriter.Flush(); crypStream.FlushFinalBlock(); memStream.Flush(); return Convert.ToBase64String(memStream.GetBuffer(), 0, (int)memStream.Length); } } /// <summary> /// DES解密 /// </summary> /// <param name="EncValue"></param> /// <returns></returns> public static string Decrypt(string EncValue) { DESCryptoServiceProvider dsp = new DESCryptoServiceProvider(); byte[] buffer = Convert.FromBase64String(EncValue); using (MemoryStream memStream = new MemoryStream()) { CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); crypStream.Write(buffer, 0, buffer.Length); crypStream.FlushFinalBlock(); return ASCIIEncoding.UTF8.GetString(memStream.ToArray()); } } } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace MyEncrypt { /// <summary> /// 不可逆加密 /// 1 防止被篡改 /// 2 防止明文存储 /// 3 防止抵赖,数字签名 /// </summary> public class MD5Encrypt { #region MD5 /// <summary> /// MD5加密,和动网上的16/32位MD5加密结果相同, /// 使用的UTF8编码 /// </summary> /// <param name="strSource">待加密字串</param> /// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param> /// <returns>加密后的字串</returns> public static string Encrypt(string source, int length = 32)//默认参数 { HashAlgorithm provider = CryptoConfig.CreateFromName("MD5") as HashAlgorithm; if (string.IsNullOrEmpty(source)) return string.Empty; byte[] bytes = Encoding.UTF8.GetBytes(source);// Encoding.ASCII.GetBytes(source); byte[] hashValue = provider.ComputeHash(bytes); StringBuilder sb = new StringBuilder(); switch (length) { case 16://16位密文是32位密文的9到24位字符 for (int i = 4; i < 12; i++) sb.Append(hashValue[i].ToString("x2")); break; case 32: for (int i = 0; i < 16; i++) { sb.Append(hashValue[i].ToString("x2")); } break; default: for (int i = 0; i < hashValue.Length; i++) { sb.Append(hashValue[i].ToString("x2")); } break; } return sb.ToString(); } #endregion MD5 } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyEncrypt { /// <summary> /// 1:MD5 不可逆加密 /// 2:Des 对称可逆加密 /// 3:RSA 非对称可逆加密 /// 4:数字证书 SSL /// </summary> class Program { static void Main(string[] args) { try { Console.WriteLine(MD5Encrypt.Encrypt("123456小杨中交一公局第六工程有限公司")); Console.WriteLine(MD5Encrypt.Encrypt("123456小夏中交一公局第六工程有限公司")); Console.WriteLine(MD5Encrypt.Encrypt("123456小吴中交一公局第六工程有限公司")); Console.WriteLine(MD5Encrypt.Encrypt("123456李小中交一公局第六工程有限公司")); Console.WriteLine(MD5Encrypt.Encrypt("123456小李中交一公局第六工程有限公司")); Console.WriteLine(MD5Encrypt.Encrypt("123456小李")); Console.WriteLine(MD5Encrypt.Encrypt("1")); //不可逆 Console.WriteLine(MD5Encrypt.Encrypt("1冉娃娃是.net高级班的一名vip学员,他是四川的小伙子,目前好像还没结婚47926363838753576475637566573654756735438547385434")); Console.WriteLine(MD5Encrypt.Encrypt("2冉娃娃是.net高级班的一名vip学员,他是四川的小伙子,目前好像还没结婚47926363838753576475637566573654756735438547385434")); Console.WriteLine(MD5Encrypt.Encrypt("3冉娃娃是.net高级班的一名vip学员,他是四川的小伙子,目前好像还没结婚47926363838753576475637566573654756735438547385434")); Console.WriteLine(MD5Encrypt.Encrypt("1")); Console.WriteLine(MD5Encrypt.Encrypt("xiaoduirensheng123455")); Console.WriteLine(MD5Encrypt.Encrypt("kuaileyangguang123455")); Console.WriteLine(MD5Encrypt.Encrypt("haha123455")); Console.WriteLine(MD5Encrypt.Encrypt("haha123456")); Console.WriteLine(MD5Encrypt.Encrypt("张三李四")); Console.WriteLine(MD5Encrypt.Encrypt("张三李四")); //可逆对称加密 string desEn = DesEncrypt.Encrypt("王殃殃"); string desDe = DesEncrypt.Decrypt(desEn); string desEn1 = DesEncrypt.Encrypt("张三李四"); string desDe1 = DesEncrypt.Decrypt(desEn1); //非对称加密 //string publicKey = ""; //string privateKey = ""; //string rsaEn = RsaEncrypt.Encrypt("netnetnetnetnetnetnetne", out publicKey, out privateKey); //string rsaDe = RsaEncrypt.Decrypt(rsaEn, privateKey); KeyValuePair<string, string> publicPrivate = RsaEncrypt.GetKeyPair(); string rsaEn1 = RsaEncrypt.Encrypt("net", publicPrivate.Key); string rsaDe1 = RsaEncrypt.Decrypt(rsaEn1, publicPrivate.Value); //加密key 解密key //公开加密key,接受加密消息,因为只有我一个人能解密 //公开解密key,用于签名,表明数据一定是我发的,因为只有我有加密的key //公钥私钥只是跟公开与否有关 } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); } } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace MyEncrypt { /// <summary> /// RSA ECC /// 可逆非对称加密 /// 非对称加密算法的优点是密钥管理很方便,缺点是速度慢。 /// </summary> public class RsaEncrypt { /// <summary> /// publicKey,privateKey /// </summary> /// <returns></returns> public static KeyValuePair<string, string> GetKeyPair() { RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); string publicKey = RSA.ToXmlString(false); string privateKey = RSA.ToXmlString(true); return new KeyValuePair<string, string>(publicKey, privateKey); } /// <summary> /// 加密:内容+公钥 /// </summary> /// <param name="content"></param> /// <param name="publicKey"></param> /// <returns></returns> public static string Encrypt(string content, string publicKey) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(publicKey); UnicodeEncoding ByteConverter = new UnicodeEncoding(); byte[] DataToEncrypt = ByteConverter.GetBytes(content); byte[] resultBytes = rsa.Encrypt(DataToEncrypt, false); return Convert.ToBase64String(resultBytes); } /// <summary> /// 加密 /// </summary> /// <param name="content"></param> /// <param name="publicKey">返还公钥</param> /// <param name="privateKey">返回密钥</param> /// <returns>加密后结果</returns> public static string Encrypt(string content, out string publicKey, out string privateKey) { RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(); publicKey = rsaProvider.ToXmlString(false); privateKey = rsaProvider.ToXmlString(true); UnicodeEncoding ByteConverter = new UnicodeEncoding(); byte[] DataToEncrypt = ByteConverter.GetBytes(content); byte[] resultBytes = rsaProvider.Encrypt(DataToEncrypt, false); return Convert.ToBase64String(resultBytes); } /// <summary> /// 解密 内容+私钥 /// </summary> /// <param name="content"></param> /// <param name="privateKey"></param> /// <returns></returns> public static string Decrypt(string content, string privateKey) { byte[] dataToDecrypt = Convert.FromBase64String(content); RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSA.FromXmlString(privateKey); byte[] resultBytes = RSA.Decrypt(dataToDecrypt, false); UnicodeEncoding ByteConverter = new UnicodeEncoding(); return ByteConverter.GetString(resultBytes); } } }