PortBlog.API/AesEncryption/Program.cs
Bangara Raju Kottedi 21e02ab3ca 1. AES Encryption/Decryption Tool
2. Encrypted Connection string
3. Added API Security using ApiKey
2024-04-28 13:22:57 +05:30

139 lines
5.1 KiB
C#

// See https://aka.ms/new-console-template for more information
using System.Security.Cryptography;
string keyBase64;
string cipherText;
string vectorBase64;
string plainText;
Console.WriteLine("Welcome to the Aes Encryption/Decryption tool");
Select:
Console.WriteLine("Please select the action you want to perform: /n Press 1 to Generate Key. /n Press 2 to Encrypt using the key. /n Press 3 to Decrpt using the key.");
string? action = Console.ReadLine();
if (action == null || !int.TryParse(action, out int actionId) || !new List<int>{ 1, 2, 3 }.Contains(actionId))
{
Console.WriteLine("Invalid option. /n");
goto Select;
}
switch (actionId)
{
case 1:
Console.WriteLine("Creating Aes Encryption 256 bit key");
using (Aes aesAlgorithm = Aes.Create())
{
aesAlgorithm.KeySize = 256;
aesAlgorithm.GenerateKey();
keyBase64 = Convert.ToBase64String(aesAlgorithm.Key);
Console.WriteLine($"Aes Key Size : {aesAlgorithm.KeySize}");
Console.WriteLine("Here is the Aes key in Base64:");
Console.WriteLine(keyBase64);
}
break;
case 2:
Console.WriteLine("Encrypt Text");
Console.WriteLine("Provide the Aes Key in base64 format :");
keyBase64 = Console.ReadLine();
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("Please enter the text that you want to encrypt:");
plainText = Console.ReadLine();
Console.WriteLine("--------------------------------------------------------------");
cipherText = EncryptDataWithAes(plainText, keyBase64, out vectorBase64);
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("Here is the cipher text with vector:");
Console.WriteLine(cipherText + ":" + vectorBase64);
break;
case 3:
Console.WriteLine("Please enter the text that you want to decrypt:");
cipherText = Console.ReadLine();
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("Provide the Aes Key:");
keyBase64 = Console.ReadLine();
Console.WriteLine("--------------------------------------------------------------");
vectorBase64 = cipherText.Split(":")[1];
cipherText = cipherText.Split(":")[0];
plainText = DecryptDataWithAes(cipherText, keyBase64, vectorBase64);
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("Here is the decrypted data:");
Console.WriteLine(plainText);
break;
}
goto Select;
static string EncryptDataWithAes(string plainText, string keyBase64, out string vectorBase64)
{
using (Aes aesAlgorithm = Aes.Create())
{
aesAlgorithm.Key = Convert.FromBase64String(keyBase64);
aesAlgorithm.GenerateIV();
Console.WriteLine($"Aes Cipher Mode : {aesAlgorithm.Mode}");
Console.WriteLine($"Aes Padding Mode: {aesAlgorithm.Padding}");
Console.WriteLine($"Aes Key Size : {aesAlgorithm.KeySize}");
//set the parameters with out keyword
vectorBase64 = Convert.ToBase64String(aesAlgorithm.IV);
// Create encryptor object
ICryptoTransform encryptor = aesAlgorithm.CreateEncryptor();
byte[] encryptedData;
//Encryption will be done in a memory stream through a CryptoStream object
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(plainText);
}
encryptedData = ms.ToArray();
}
}
return Convert.ToBase64String(encryptedData);
}
}
static string DecryptDataWithAes(string cipherText, string keyBase64, string vectorBase64)
{
using (Aes aesAlgorithm = Aes.Create())
{
aesAlgorithm.Key = Convert.FromBase64String(keyBase64);
aesAlgorithm.IV = Convert.FromBase64String(vectorBase64);
Console.WriteLine($"Aes Cipher Mode : {aesAlgorithm.Mode}");
Console.WriteLine($"Aes Padding Mode: {aesAlgorithm.Padding}");
Console.WriteLine($"Aes Key Size : {aesAlgorithm.KeySize}");
Console.WriteLine($"Aes Block Size : {aesAlgorithm.BlockSize}");
// Create decryptor object
ICryptoTransform decryptor = aesAlgorithm.CreateDecryptor();
byte[] cipher = Convert.FromBase64String(cipherText);
//Decryption will be done in a memory stream through a CryptoStream object
using (MemoryStream ms = new MemoryStream(cipher))
{
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
using (StreamReader sr = new StreamReader(cs))
{
return sr.ReadToEnd();
}
}
}
}
}