Cyber Security with C# and AES
In this article, we will discuss Cyber Security in C# and create a project demonstrating the usage of encryption and decryption.
Cybersecurity is crucial in today’s world with the increasing use of technology in all areas of life. The risk of cyberattacks is at an all-time high, making it essential for individuals, businesses, and governments to prioritize protection against them. To be successful, cybersecurity must continually adapt to new threats. This requires programmers to continuously learn and innovate to create the most effective solutions.
To improve cybersecurity, we need more dedicated developers to join the growing team of security experts. As educators, we are also responsible for introducing this field to the next generation, so they can develop solutions for future challenges.
So it is with this article (And Chapter 9 of our Tiny C# Projects book check it out here!) that we want to take our first step into teaching this very next generation of experts in our internet security.
Prefer a VIDEO format? Check out our VIDEO TO THIS ARTICLE RIGHT HERE!
What is Cyber Security?
Before we begin, for those that do not know at all what encryption and decryption is, let´s see a quick explanation.
Encryption converts plaintext (readable data) into an unreadable format, called ciphertext, using a mathematical algorithm called a cipher. This process is used to protect sensitive information from unauthorized access and ensure the confidentiality of the data. The process of converting ciphertext back into plaintext is called decryption. This process uses the same mathematical algorithm as encryption but with a different key, allowing only authorized parties to access the original data. Encryption and decryption are commonly used in communication systems, computer networks, and storage devices to protect data from unauthorized access and ensure data integrity.
So, for our project, we will use the Aes encryption algorithm, a symmetric encryption algorithm. This means that the same key is used for both encryption and decryption.
Our Encryption Method
Now, let’s take a look at the Encrypt method. This method uses three parameters: the plain text password, the key, and the initialization vector. The plain text password is the password that we want to encrypt, the key is used to encrypt and decrypt the password, and the initialization vector is used to add an extra layer of security to the encryption process.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
static byte[] Encrypt(string simpletext, byte[] key, byte[] iv) { byte[] cipheredtext; using (Aes aes = Aes.Create()) { ICryptoTransform encryptor = aes.CreateEncryptor(key, iv); using (MemoryStream memoryStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { using (StreamWriter streamWriter = new StreamWriter(cryptoStream)) { streamWriter.Write(simpletext); } cipheredtext = memoryStream.ToArray(); } } } return cipheredtext; } |
As you can see, we are using the Aes class to create an encryptor object. This encryptor object is used to encrypt the plain text password using the key and initialization vector. We then create a MemoryStream object and a CryptoStream object to write the encrypted password to. Finally, we convert the encrypted password to a byte array and return it.
Our Decryption Method
Now, let’s move on to the decryption method. The decryption method is similar to the encryption method but is the opposite process. We first create an instance of the Aes class. Then we create a decryptor using the key and iv that we generated earlier. We then use a memory stream to read the ciphered text, and we use the decryptor to decrypt the text. We then use a stream reader to read the decrypted text and return it as a string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
static string Decrypt(byte[] cipheredtext, byte[] key, byte[] iv) { string simpletext = String.Empty; using (Aes aes = Aes.Create()) { ICryptoTransform decryptor = aes.CreateDecryptor(key, iv); using (MemoryStream memoryStream = new MemoryStream(cipheredtext)) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) { using (StreamReader streamReader = new StreamReader(cryptoStream)) { simpletext = streamReader.ReadToEnd(); } } } } return simpletext; } |
Now that we have both the encryption and decryption methods, let’s move on to the Main method. This is where we will be using the encryption and decryption methods. In the Main method, we first ask the user to enter a username and password. We then generate the key and iv, as we did before. We then use the encryption method to encrypt the password and convert it to a string. We then use the decryption method to decrypt the password and print it out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
static void Main(string[] args) { Console.WriteLine("Please enter a username:"); string username = Console.ReadLine(); Console.WriteLine("Please enter a password:"); string password = Console.ReadLine(); //Generate the key and IV byte[] key = new byte[16]; byte[] iv = new byte[16]; using(RandomNumberGenerator rng = RandomNumberGenerator.Create()) { rng.GetBytes(key); rng.GetBytes(iv); } //Encrypt the password byte[] encryptedPassword = Encrypt(password, key, iv); string encryptedPasswordString = Convert.ToBase64String(encryptedPassword); Console.WriteLine("Encrypted password: " + encryptedPasswordString); //Decrypt the password string decryptedPassword = Decrypt(encryptedPassword, key, iv); Console.WriteLine("Decrypted password: " + decryptedPassword); Console.ReadLine(); } |
Our Result
After explaining the entire project, go ahead and run it to see it in action. You should be able to enter a username and password, and the program will encrypt and decrypt the password and print it out.
Your output should look something like this:

Cyber Security with C# and AES
Conclusion: Cyber Security with C# and AES
That’s it for this article on Cyber Security in C#. We hope you found it helpful and informative. As always, feel free to reach out to us if you have any questions or comments. Thanks for reading!