How to Encrypt Data with Plain C Using AES

Listen to this Post

In this article, we’ll explore how to implement symmetric encryption in C using the Advanced Encryption Standard (AES) algorithm. This is particularly useful for securing sensitive data like API keys, passwords, or confidential information.

Why AES?

AES is a widely adopted encryption standard known for its security and efficiency. It supports key sizes of 128, 192, and 256 bits, making it suitable for various security requirements.

Step-by-Step Implementation

1. Generate a Key and Initialization Vector (IV)

AES requires a secret key and an IV for encryption and decryption.

using System.Security.Cryptography;

byte[] GenerateRandomKey(int size) 
{
using (var rng = RandomNumberGenerator.Create()) 
{
byte[] key = new byte[bash]; 
rng.GetBytes(key); 
return key; 
}
}

byte[] key = GenerateRandomKey(32); // 256-bit key 
byte[] iv = GenerateRandomKey(16); // 128-bit IV 

2. Encrypt Data with AES

string Encrypt(string plainText, byte[] key, byte[] iv) 
{
using (Aes aes = Aes.Create()) 
{
aes.Key = key; 
aes.IV = iv;

ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

using (MemoryStream ms = new MemoryStream()) 
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) 
using (StreamWriter sw = new StreamWriter(cs)) 
{
sw.Write(plainText); 
sw.Close(); 
return Convert.ToBase64String(ms.ToArray()); 
}
}
}

string encrypted = Encrypt("YourOpenAI_API_Key", key, iv); 
Console.WriteLine($"Encrypted: {encrypted}"); 

3. Decrypt Data

string Decrypt(string cipherText, byte[] key, byte[] iv) 
{
using (Aes aes = Aes.Create()) 
{
aes.Key = key; 
aes.IV = iv;

ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(cipherText))) 
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) 
using (StreamReader sr = new StreamReader(cs)) 
{
return sr.ReadToEnd(); 
}
}
}

string decrypted = Decrypt(encrypted, key, iv); 
Console.WriteLine($"Decrypted: {decrypted}"); 

You Should Know:

  • Never hardcode keys or IVs – Store them securely (e.g., Azure Key Vault, environment variables).
  • Use authenticated encryption (like AES-GCM) for better security.
  • Always validate inputs to prevent injection attacks.

Linux & Windows Security Commands

  • Linux (OpenSSL AES Encryption):
    openssl enc -aes-256-cbc -salt -in plaintext.txt -out encrypted.enc -k "YourPassword"
    
  • Windows (PowerShell Encryption):
    $secureString = ConvertTo-SecureString "SecretData" -AsPlainText -Force 
    $encrypted = ConvertFrom-SecureString $secureString -Key (1..32) 
    

What Undercode Say:

AES in C provides a strong, efficient way to secure sensitive data. However, always follow best practices:
– Rotate keys periodically.
– Use secure key storage solutions.
– Prefer AES-GCM for authenticated encryption.

For further reading, check the original post: Encrypt Data with C and AES.

Expected Output:

Encrypted: U2FsdGVkX19zZWNyZXRfZGF0YQ== 
Decrypted: YourOpenAI_API_Key 

References:

Reported By: Milan Jovanovic – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image