What kind of encryption is this? Can I eat it?
What I want to share with you today should be BASE64,MD5,AES The algorithm is implemented together, but the volume of encryption will be larger, he can't eat it.
Overview
The encryption process is probably purple:
Text BASE64 Coding
_AES Encryption Get ciphertext
Password MD5 Encryption
The process of decryption is probably purple sauce:
Password MD5 Encryption
The original text is obtained from BASE64 decryption
Ciphertext AES Decryption
Coding
In accordance with the above process, the original text needs to be BASE64 encoding first: \\\\\\
var bt = Encoding.Your coding format.GetBytes(original text); var base64Str = Convert.ToBase64String(bt);
The next step is to encrypt the password with MD5, because AES encryption requires 32-bit passwords, which can be met by using 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 }
Call method:
1 MD5.EncryptToMD5string("Your password");
The next step is to use them for AES encryption: (using ECB and 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 }
Usage method:
1 var str = TextEncrypt("original text BASE64 After encoding","Password MD5 After encryption");
The obtained str is the encrypted ciphertext, which completes the encryption work.
Now that there's encryption, there's decryption, and then there's decryption.
According to the process, first of all, we need to encrypt the password with MD5. The method of MD5 encryption has been explained above, so I won't talk more about it here.
Next is the AES decryption of ciphertext:
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 }
Usage method:
1 TextDecrypt("ciphertext","Password MD5 After treatment")
Finally, the processed text should be decrypted by BASE64:
Var STR = Convert. FromBase64String (processed text);
var sd = Encoding. Your encoding format. GetString(str);
The final str is our decrypted original (Q Q)
Complete code:
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("Enter encrypted text"); 30 var s = Console.ReadLine(); 31 var bt = Encoding.Default.GetBytes(s); 32 var base64Str = Convert.ToBase64String(bt); 33 Console.WriteLine("Enter Encrypted Password"); 34 var psw = MD5.EncryptToMD5string(Console.ReadLine()); 35 var sw = TextEncrypt(base64Str, psw); 36 Console.WriteLine(sw); 37 Console.WriteLine("Encryption completed"); 38 Console.ReadLine(); 39 Console.WriteLine("Decrypting"); 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 }
III. Testing
The test was successful! o(**)
This is the end of the course. Welcome to pay attention to me.