这是什么加密?可以吃吗?
一、概述
加密的流程大概就是酱紫的:
原文→BASE64编码
→AES加密 得到密文
密码→MD5加密
解密的流程大概是酱紫:
密码 → MD5加密
→ BASE64解密 得到原文
密文 → AES解密
二、编码
按照上面的流程,首先需要对原文进行BASE64编码:
var bt = Encoding.你的编码格式.GetBytes(原文); var base64Str = Convert.ToBase64String(bt);
接下来就到对密码进行MD5加密,因为AES加密需要32位密码,使用MD5正好可以满足这一条件。
1 public class MD5 2 { 3 public static byte[] EncryptToMD5(string str) 4 { 5 MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 6 byte[] str1 = System.Text.Encoding.UTF8.GetBytes(str); 7 byte[] str2 = md5.ComputeHash(str1, 0, str1.Length); 8 md5.Clear(); 9 (md5 as IDisposable).Dispose(); 10 return str2; 11 } 12 public static string EncryptToMD5string(string str) 13 { 14 byte[] bytHash = EncryptToMD5(str); 15 string sTemp = ""; 16 for (int i = 0; i < bytHash.Length; i++) 17 { 18 sTemp += bytHash[i].ToString("X").PadLeft(2, '0'); 19 } 20 return sTemp.ToLower(); 21 } 22 }
调用方法:
1 MD5.EncryptToMD5string("你的密码");
接下来就是使用他们进行AES加密:(使用ECB和PKCS7)
1 private string TextEncrypt(string encryptStr, string key) 2 { 3 byte[] keyArray = Encoding.UTF8.GetBytes(key); 4 byte[] toEncryptArray = Encoding.UTF8.GetBytes(encryptStr); 5 RijndaelManaged rDel = new RijndaelManaged(); 6 rDel.Key = keyArray; 7 rDel.Mode = CipherMode.ECB; 8 rDel.Padding = PaddingMode.PKCS7; 9 ICryptoTransform cTransform = rDel.CreateEncryptor(); 10 byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); 11 return Convert.ToBase64String(resultArray, 0, resultArray.Length); 12 }
使用方法:
1 var str = TextEncrypt("原文BASE64编码后","密码MD5加密后");
得到的str就是加密后的密文,这样就完成了加密的工作。
既然有加密,就要有解密,接下来就是解密工作啦
按照流程,首先需要对密码进行MD5加密,关于MD5加密的方法在上面已经说明,此处就不再多说啦。
接下来是对密文进行AES解密:
1 private string TextDecrypt(string decryptStr, string key) 2 { 3 byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); 4 byte[] toEncryptArray = Convert.FromBase64String(decryptStr); 5 RijndaelManaged rDel = new RijndaelManaged(); 6 rDel.Key = keyArray; 7 rDel.Mode = CipherMode.ECB; 8 rDel.Padding = PaddingMode.PKCS7; 9 ICryptoTransform cTransform = rDel.CreateDecryptor(); 10 byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); 11 return UTF8Encoding.UTF8.GetString(resultArray); 12 }
使用方法:
1 TextDecrypt("密文","密码MD5处理后")
最后要对处理后的文本进行BASE64解密:
var str = Convert.FromBase64String(处理后的文本);
var sd = Encoding.你的编码格式.GetString(str);
最终得到的str就是我们解密后的原文惹(QωQ)
完整代码:
1 class Program 2 { 3 public static string TextEncrypt(string encryptStr, string key) 4 { 5 byte[] keyArray = Encoding.UTF8.GetBytes(key); 6 byte[] toEncryptArray = Encoding.UTF8.GetBytes(encryptStr); 7 RijndaelManaged rDel = new RijndaelManaged(); 8 rDel.Key = keyArray; 9 rDel.Mode = CipherMode.ECB; 10 rDel.Padding = PaddingMode.PKCS7; 11 ICryptoTransform cTransform = rDel.CreateEncryptor(); 12 byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); 13 return Convert.ToBase64String(resultArray, 0, resultArray.Length); 14 } 15 public static string TextDecrypt(string decryptStr, string key) 16 { 17 byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); 18 byte[] toEncryptArray = Convert.FromBase64String(decryptStr); 19 RijndaelManaged rDel = new RijndaelManaged(); 20 rDel.Key = keyArray; 21 rDel.Mode = CipherMode.ECB; 22 rDel.Padding = PaddingMode.PKCS7; 23 ICryptoTransform cTransform = rDel.CreateDecryptor(); 24 byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); 25 return UTF8Encoding.UTF8.GetString(resultArray); 26 } 27 static void Main(string[] args) 28 { 29 Console.WriteLine("输入加密的文本"); 30 var s = Console.ReadLine(); 31 var bt = Encoding.Default.GetBytes(s); 32 var base64Str = Convert.ToBase64String(bt); 33 Console.WriteLine("输入加密的密码"); 34 var psw = MD5.EncryptToMD5string(Console.ReadLine()); 35 var sw = TextEncrypt(base64Str, psw); 36 Console.WriteLine(sw); 37 Console.WriteLine("加密完成"); 38 Console.ReadLine(); 39 Console.WriteLine("正在解密"); 40 var td = TextDecrypt(sw, psw); 41 var str = Convert.FromBase64String(td); 42 var sd = Encoding.Default.GetString(str); 43 Console.WriteLine(sd); 44 Console.ReadLine(); 45 46 } 47 } 48 public class MD5 49 { 50 public static byte[] EncryptToMD5(string str) 51 { 52 MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 53 byte[] str1 = System.Text.Encoding.UTF8.GetBytes(str); 54 byte[] str2 = md5.ComputeHash(str1, 0, str1.Length); 55 md5.Clear(); 56 (md5 as IDisposable).Dispose(); 57 return str2; 58 } 59 public static string EncryptToMD5string(string str) 60 { 61 byte[] bytHash = EncryptToMD5(str); 62 string sTemp = ""; 63 for (int i = 0; i < bytHash.Length; i++) 64 { 65 sTemp += bytHash[i].ToString("X").PadLeft(2, '0'); 66 } 67 return sTemp.ToLower(); 68 } 69 }
三、测试
测试成功!o(* ̄▽ ̄*)ブ
这次的教程到这里就结束啦,欢迎大家关注我哦!