Idea: using random vectors, the random vector into the ciphertext, each time the decryption from the ciphertext to intercept the first 16 bits, in fact, we have previously encrypted random vectors.
Code
public static string Encrypt (string plaintext, string aeskey) {rijndaelmanaged rijndaelcipher = new RijndaelManaged (); byte[] Inputbytearray = Encoding.UTF8.GetBytes (plaintext);//Get byte array to be encrypted Rijndaelcipher.key = Convert.frombase64stri Ng (Aeskey);//Add decryption both parties agreed to a good key: Aeskey Rijndaelcipher.generateiv (); byte[] Keyiv = RIJNDAELCIPHER.IV; byte[] cipherbytes = null; using (MemoryStream ms = new MemoryStream ()) {using (CryptoStream cs = new CryptoStream (MS, Rijndaelcipher.crea Teencryptor (), cryptostreammode.write)) {cs. Write (Inputbytearray, 0, inputbytearray.length); Cs. FlushFinalBlock (); Cipherbytes = Ms. ToArray ();//Gets the encrypted byte array after CS. Close (); Ms. Close (); }} var allencrypt = new Byte[keyiv.length + cipherbytes.length]; Buffer.blockcopy (Keyiv, 0, Allencrypt, 0, keyiv.length); Buffer.blockcopy (cipherbytes, 0, Allencrypt, keyiv.length * sizeof (BYTE), cipherbytes.length); Return Convert.tobaSe64string (Allencrypt);} public static string Decrypt (String showtext, String aeskey) {string result = string. Empty; try {byte[] ciphertext = convert.frombase64string (Showtext); int length = Ciphertext.length; SymmetricAlgorithm rijndaelcipher = Rijndael.create (); Rijndaelcipher.key = convert.frombase64string (Aeskey);//decryption of the mutually agreed key byte[] IV = new BYTE[16]; Buffer.blockcopy (ciphertext, 0, IV, 0, 16); RIJNDAELCIPHER.IV = IV; byte[] decryptbytes = new BYTE[LENGTH-16]; byte[] Passwdtext = new BYTE[LENGTH-16]; Buffer.blockcopy (ciphertext, passwdtext, 0, length-16); using (MemoryStream ms = new MemoryStream (Passwdtext)) {using (CryptoStream cs = new CryptoStream (MS, R Ijndaelcipher.createdecryptor (), CryptoStreamMode.Read)) {cs. Read (decryptbytes, 0, decryptbytes.length); Cs. Close (); Ms. Close (); } }result = Encoding.UTF8.GetString (decryptbytes). Replace ("n", ""); Remove the ' rear ' of the string (} catch {} return result;}
Call:
String Jiami = Myaestools.encrypt (TextBox1.Text, "abcdefgh12345678abcdefgh12345678"); string Jiemi = Myaestools.decrypt (TextBox3.Text, "abcdefgh12345678abcdefgh12345678");
C # symmetric encryption (AES encryption) each generated ciphertext results different ideas code sharing