Idea: Use random vectors to put the random vector into the ciphertext. Each time we decrypt, the first 16 bits are intercepted from the ciphertext. In fact, it is the random vector we encrypted before.
Code:
public static string Encrypt(string plainText, string AESKey) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); byte[] inputByteArray = Encoding.(plainText);//Get the byte array that needs to be encrypted = Convert.FromBase64String(AESKey);//The two parties agreed on the key: AESKey (); byte[] keyIv = ; byte[] cipherBytes = null; using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, (), )) { (inputByteArray, 0, ); (); cipherBytes = ();//Get the encrypted byte array (); (); } } var allEncrypt = new byte[ + ]; (keyIv, 0, allEncrypt, 0, ); (cipherBytes, 0, allEncrypt, * sizeof(byte), ); return Convert.ToBase64String(allEncrypt); } public static string Decrypt(string showText, string AESKey) { string result = ; try { byte[] cipherText = Convert.FromBase64String(showText); int length = ; SymmetricAlgorithm rijndaelCipher = (); = Convert.FromBase64String(AESKey);//Encrypt and decrypt the key agreed upon by both parties byte[] iv = new byte[16]; (cipherText, 0, iv, 0, 16); = iv; byte[] decryptBytes = new byte[length - 16]; byte[] passwdText = new byte[length - 16]; (cipherText, 16, passwdText, 0, length - 16); using (MemoryStream ms = new MemoryStream(passwdText)) { using (CryptoStream cs = new CryptoStream(ms, (), )) { (decryptBytes, 0, ); (); (); } } result = Encoding.(decryptBytes).Replace("\0", ""); /// Remove the '\0' at the end of the string } catch { } return result; }
Called:
string jiaMi = (, "abcdefgh12345678abcdefgh12345678"); string jieMi = (, "abcdefgh12345678abcdefgh12345678");