Share an encryption algorithm and an encryption algorithm
What is encryption? Can I eat it?
We will share with you today that BASE64, MD5, and AES algorithms are implemented together. However, after encryption, the size will increase and he cannot eat them.
I. Overview
The encryption process is probably soy sauce:
Original → BASE64 encoding
→ Encrypted by AES to obtain the ciphertext
Password → MD5 Encryption
The decryption process is probably sauce purple:
Password → MD5 Encryption
→ Decrypt BASE64 to obtain the original text
Ciphertext → AES decryption
Ii. Encoding
According to the above process, we first need to encode the original text with BASE64:
Var bt = Encoding. Your Encoding format. GetBytes (original); var base64Str = Convert. ToBase64String (bt );
Next, perform MD5 encryption on the password. Because AES encryption requires a 32-bit password, MD5 can meet this condition.
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: (use 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:
1 var str = TextEncrypt ("after BASE64 encoding", "After MD5 encryption ");
The str is the encrypted ciphertext, which completes the encryption.
Since there is encryption, decryption is required, and the next step is decryption.
According to the process, we first need to encrypt the password with MD5. the MD5 encryption method has been described above, so we will not talk about it here.
Next we will perform AES decryption on the 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:
1 TextDecrypt ("ciphertext", "After MD5 encryption ")
Finally, we need to perform BASE64 decryption on the processed text:
Var str = Convert. FromBase64String (processed text );
Var sd = Encoding. Your Encoding format. GetString (str );
The final str is the decrypted Original article (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 the encrypted password"); 34 var psw = MD5.EncryptToMD5string (Console. readLine (); 35 var sw = TextEncrypt (base64Str, psw); 36 Console. writeLine (sw); 37 Console. writeLine ("encrypted"); 38 Console. readLine (); 39 Console. writeLine ("decryption"); 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 md1_{ 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. Test
Test successful! O (*  ̄ *) records
This tutorial is over now. You are welcome to pay attention to me!