For ECB, CBC (Cipher Block Chaining) and CFB (Cipher Feedback Block) are block cipher modes, whereas OFB (Output feedback) and CTR (Counter) are stream cipher modes. In this case we will use a 256-bit encryption key, and with a 16-byte random salt value (IV).
AES CBC, ECB, CFB, CTS and OFB Encryption with C# |
Method
For ECB, CBC (Cipher Block Chaining) and CFB (Cipher Feedback Block) are block cipher modes, whereas OFB (Output feedback) and CTR (Counter) are stream cipher modes. In this case we will use a 256-bit encryption key, and with a random salt (IV).
We can instsall the Bouncy Castle library with:
PS C:\python3\dotnet\ecdsa> dotnet add package BouncyCastle.Cryptography Determining projects to restore... Writing C:\Users\Administrator\AppData\Local\Temp\2\tmp14asbf.tmp info : X.509 certificate chain validation will use the default trust store selected by .NET for code signing. ... info : Package 'BouncyCastle.Cryptography' is compatible with all the specified frameworks in project 'C:\python3\dotnet\ecdsa\ecdsa.csproj'. info : PackageReference for package 'BouncyCastle.Cryptography' version '2.2.1' added to file 'C:\python3\dotnet\ecdsa\ecdsa.csproj'. info : Writing assets file to disk. Path: C:\python3\dotnet\ecdsa\obj\project.assets.json log : Restored C:\python3\dotnet\ecdsa\ecdsa.csproj (in 3.44 sec).
namespace AES { using System.Security.Cryptography; class Program { static void Main(string[] args) { var mode="CFB"; var message="Hello World!"; byte[] encrypted; string plaintext; if (args.Length >0) mode=args[0]; if (args.Length >1) message=args[1]; try { var aes = new AesCryptoServiceProvider(); aes.Mode = CipherMode.CBC; if (mode=="CBC") aes.Mode = CipherMode.CBC; else if (mode == "CFB") aes.Mode = CipherMode.CFB; else if(mode == "CTS") aes.Mode = CipherMode.CTS; else if (mode == "ECB") aes.Mode = CipherMode.ECB; else if (mode == "OFB") aes.Mode = CipherMode.OFB; aes.GenerateIV(); aes.GenerateKey(); ICryptoTransform encryptor = aes.CreateEncryptor(); ICryptoTransform decryptor = aes.CreateDecryptor(); using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { swEncrypt.Write(message); } encrypted = msEncrypt.ToArray(); } } using (MemoryStream msDecrypt = new MemoryStream(encrypted)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { plaintext = srDecrypt.ReadToEnd(); } } } string str1 = "Message: " + message; str1 = str1 + "\nIV " + BitConverter.ToString(aes.IV).Replace("-", string.Empty); str1 = str1 + "\nKey " + BitConverter.ToString(aes.Key).Replace("-", string.Empty); str1 = str1 + "\nMode: " + mode; str1 =str1+"\n\nCipher: "+ BitConverter.ToString(encrypted).Replace("-", string.Empty); str1 = str1 + "\nCipher: " + Convert.ToBase64String(encrypted); str1 = str1 + "\nDecrypted: " + plaintext; Console.WriteLine("{0}",str1); } catch (Exception e) { Console.WriteLine("Error: {0}",e.Message); } } } }
A sample run shows:
Message: Hello IV D1E3CCD8CAAA8DA829B164D43F3B226C Key 340E35D6943CFDF2DF735A7FA6D74AC676AE5CEC36F6A9A7C9D9159E8DDAC730 Mode: CBC Cipher: A3B8DC46AFAF2109174FDDA6BB44E0BE Cipher: o7jcRq+vIQkXT92mu0Tgvg== Decrypted: Hello
Presentation
Here is an outline: