Using System.Security.Cryptography;
AES can be called directly
As if only the ECB CBC CFB can be used directly, CTR OFB does not know how to use
Static classMyaes {Staticpaddingmode[] padding ={PADDINGMODE.PKCS7, paddingmode.ansix923, paddingmode.iso10126, PADDINGM Ode. None, Paddingmode.zeros};//This is the Fill methodStatic Public stringEncrypt (stringMessage,stringKeystringIV, Ciphermode Mode,intPadintlength) {//plaintext key vector encryption mode fill mode key lengthTry { //rijndaelmanaged aes = new RijndaelManaged ();Rijndael AES =rijndael.create (); //AES. BlockSize = 128; //AES. Feedbacksize =;Aes. KeySize =length; Aes. Padding=Padding[pad]; Aes. Mode=Mode; //AES. Key = Encoding.UTF8.GetBytes (key); //Aes.iv = Encoding.UTF8.GetBytes (IV); byte[] Keybytes =Encoding.UTF8.GetBytes (key); byte[] Keyiv =Encoding.UTF8.GetBytes (IV); byte[] Inputbytearray =Encoding.UTF8.GetBytes (Message); MemoryStream Memstream=NewMemoryStream (); CryptoStream Crypstream=NewCryptoStream (Memstream, AES. CreateEncryptor (Keybytes, Keyiv), cryptostreammode.write); Crypstream.write (Inputbytearray,0, inputbytearray.length); Crypstream.flushfinalblock (); Aes. Clear (); returnconvert.tobase64string (Memstream.toarray ()); } Catch{MessageBox.Show ("Encryption failed");return ""; } } //AES Encryption Static Public stringDecrypt (stringCiphertext,stringKeystringIV, Ciphermode Mode,intPadintlength) { Try { //rijndaelmanaged aes = new RijndaelManaged ();Rijndael AES =rijndael.create (); //AES. BlockSize = 128; //AES. feedbacksize = 128; //AES. Key = Encoding.UTF8.GetBytes (key); //Aes.iv = Encoding.UTF8.GetBytes (IV);Aes. KeySize =length; Aes. Padding=Padding[pad]; Aes. Mode=Mode; byte[] Keybytes =Encoding.UTF8.GetBytes (key); byte[] Keyiv =Encoding.UTF8.GetBytes (IV); byte[] Outputbytearray =convert.frombase64string (ciphertext); MemoryStream Memstream=NewMemoryStream (); CryptoStream Crypstream=NewCryptoStream (Memstream, AES. CreateDecryptor (Keybytes, Keyiv), cryptostreammode.write); Crypstream.write (Outputbytearray,0, outputbytearray.length); Crypstream.flushfinalblock (); Aes. Clear (); returnEncoding.UTF8.GetString (Memstream.toarray ()); } Catch{MessageBox.Show ("Encryption failed");return ""; } } //AES Decryption }
C # AES Encryption