# How To Encrypt And Decrypt In C# Using Simple AES Keys

In this article, I'm going to tell you how to encrypt and decrypt a string in Visual Studio.

Whenever the word encryption comes to our mind, we will move to the topic AES (Advanced Encryption Standard). But today I came up with an ideology of using Public Key Cryptography. One can perform encryption and decryption by the source code provided below but to better understand the concept, please read the theory.

## Cryptology

Cryptology is a science of using mathematics to encrypt and decrypt data.

Generally, it is classified into two categories,

Cryptanalysis

It refers to the study of ciphertext in cryptosystems.

Cryptography

In terms of Cryptography, the phrase ‘crypto’ means secret and ‘graphy’ means writing, So cryptography means ‘secret writing’.

It is a method of modifying original data in a particular form so that only those for whom it is intended can read and process it.

Cryptography is most often associated with scrambling plain text into ciphertext and then back again.

Cryptography is the science of keeping information secure. It involves encryption and decryption of messages.

Basic Image of Encryption,
There are two types of Cryptography., They are,

Secret Key Cryptography

The encryption key and decryption key are the same

Let ‘K’ be the key.

The message TEJA can be represented as WHMD

Here., the key-value K=3 and one can easily identify the key

## Public key Cryptography

The different key for encryption and decryption
Process flow example of two-way communication based on my understanding

Let's assume Santhosh and Teja are two persons who agree to have public key = 5.

Santhosh picks message = 3, while Teja picks message = 2. Santhosh computes encryption = 3 x 5 (15), and Teja computes encryption = 2 x 5 (10) .

Santhosh and Teja exchange encrypted messages.

Santhosh computes k = 3*10 = 30, while Teja computer k = 2*15 = 30. They now use k = 30.

Thus the messages can be easily encrypted and decrypted.

Ok, now coming to the C# coding part, Source code of encryption and decryption for one way communication is given below,

Encryption in C#

Here I have taken a string with value as "Water" to encrypt

I had taken "santhosh" as the public key and "engineer" as a secret key and here I had got the encrypted value as "VtbM/yjSA2Q="
1. public string Encrypt() {
2.  try {
3.   string textToEncrypt = "Water";
4.   string ToReturn = "";
5.   string publickey = "santhosh";
6.   string secretkey = "engineer";
7.   byte[] secretkeyByte = {};
8.   secretkeyByte = System.Text.Encoding.UTF8.GetBytes(secretkey);
9.   byte[] publickeybyte = {};
10.   publickeybyte = System.Text.Encoding.UTF8.GetBytes(publickey);
11.   MemoryStream ms = null;
12.   CryptoStream cs = null;
13.   byte[] inputbyteArray = System.Text.Encoding.UTF8.GetBytes(textToEncrypt);
14.   using(DESCryptoServiceProvider des = new DESCryptoServiceProvider()) {
15.    ms = new MemoryStream();
16.    cs = new CryptoStream(ms, des.CreateEncryptor(publickeybyte, secretkeyByte), CryptoStreamMode.Write);
17.    cs.Write(inputbyteArray, 0, inputbyteArray.Length);
18.    cs.FlushFinalBlock();
19.    ToReturn = Convert.ToBase64String(ms.ToArray());
20.   }
21.   return ToReturn;
22.  } catch (Exception ex) {
23.   throw new Exception(ex.Message, ex.InnerException);
24.  }
25. }
Decryption in C#

After encrypting the value "Water", I had got the encrypted value "VtbM/yjSA2Q=" which now will be decrypted back to "Water"
1. public string Decrypt() {
2.  try {
3.   string textToDecrypt = "VtbM/yjSA2Q=";
4.   string ToReturn = "";
5.   string publickey = "santhosh";
6.   string privatekey = "engineer";
7.   byte[] privatekeyByte = {};
8.   privatekeyByte = System.Text.Encoding.UTF8.GetBytes(privatekey);
9.   byte[] publickeybyte = {};
10.   publickeybyte = System.Text.Encoding.UTF8.GetBytes(publickey);
11.   MemoryStream ms = null;
12.   CryptoStream cs = null;
13.   byte[] inputbyteArray = new byte[textToDecrypt.Replace(" ""+").Length];
14.   inputbyteArray = Convert.FromBase64String(textToDecrypt.Replace(" ""+"));
15.   using(DESCryptoServiceProvider des = new DESCryptoServiceProvider()) {
16.    ms = new MemoryStream();
17.    cs = new CryptoStream(ms, des.CreateDecryptor(publickeybyte, privatekeyByte), CryptoStreamMode.Write);
18.    cs.Write(inputbyteArray, 0, inputbyteArray.Length);
19.    cs.FlushFinalBlock();
20.    Encoding encoding = Encoding.UTF8;
21.    ToReturn = encoding.GetString(ms.ToArray());
22.   }
23.   return ToReturn;
24.  } catch (Exception ae) {
25.   throw new Exception(ae.Message, ae.InnerException);
26.  }
27. }
By using these two methods we can encrypt and decrypt the string in C#. One should note that the key size of the public key and private key should should be equal and should not exceed less than 8 characters as I had encoded using UTF8.