Welcome to your friendly neighborhood coding guide! Today we’re diving into the world of AES (Advanced Encryption Standard) encryption and how it functions seamlessly across various programming languages.
What is AES Encryption?
AES is a symmetric encryption technique widely used to secure data. It ensures that only authorized users can access sensitive information, making it essential in today’s digital landscape. In this guide, we will explore how to implement AES encryption in Python, PHP, C#, Java, C++, F#, Ruby, Scala, and Node.js.
Project Goal
The goal of this project is to provide a simple and compatible code, ensuring that data encrypted in one language can be decrypted in another. We achieve this using the AES algorithm in CBC and CFB modes, alongside HMAC SHA256 for ciphertext authenticity. The format for the encrypted data is as follows:
- salt[16] + iv[16] + ciphertext[n] + mac[32].
While the methods used here are secure, always consider using well-established libraries for production applications.
How to Encrypt and Decrypt Data
Let’s explore how the AES encryption process works in various programming languages through practical examples.
Python Example
In Python, to encrypt data using AES-128-CBC, you can follow this example:
data = "my data"
password = "my super strong password"
aes = AesEncryption()
enc = aes.encrypt(data, password)
print(enc)
Here, think of the encryption process like sealing your data inside a vault (AES), ensuring only those with the key (password) can access (decrypt) it later.
PHP Example
For PHP, decrypting AES-128-CBC encrypted data looks like this:
$data = "jDY94lq4C84RXD4uPohrqUZvyZNJg3L+KBl7d9S6hPufBCBeUcrsYoialAR+M+nJt4rWwWvB41ScQQOrlc3OzKukLqlP0Zirz7yaiYQwB4=";
$password = "my super strong password";
$aes = new AesEncryption();
$dec = $aes->decrypt($data, $password);
echo $dec; // Output: my data
C# Example
In C#, encrypting data in CFB mode can be done as follows:
string data = "my data";
string password = "my super strong password";
AesEncryption aes = new AesEncryption(cfb);
byte[] enc = aes.Encrypt(data, password);
Console.WriteLine(Encoding.ASCII.GetString(enc));
Java Example
Java users can decrypt data in CFB mode with this code:
String data = "NDVqzcBopFejULtlhK0vy66kFI2UiI3mEiu6XrfW0D3Qjf66cQES9PBk28Jhyc0QWk6XpBD4Fsth9EJStxXw7UgIerZ4OyM=";
String password = "my super strong password";
AesEncryption aes = new AesEncryption(cfb);
byte[] dec = aes.decrypt(data, password);
System.out.println(new String(dec)); // Output: my data
C++ Example
In C++, we can encrypt data using AES-256-CBC like so:
std::string data = "my data";
std::string password = "my super strong password";
AesEncryption aes(cbc, 256);
CryptoPP::SecByteBlock enc = aes.encrypt(data, password);
std::cout << std::string(enc.begin(), enc.end()) << std::endl;
F# Example
In F#, decrypting AES-256-CBC data is achieved with:
let data = "xDl8P0fKwL2pgi6WQPvd5iLUjT9IuBiZKBrH2DXdPTwwKiQILnndaaCYvu7cNv9894ap3HzgmgaOcIzT1TOWwUISAmMGqqOosLPl5Qu6o="
let password = "my super strong password"
let aes = new AesEncryption(cbc, 256)
let dec = aes.Decrypt(data, password)
printfn %A (Encoding.UTF8.GetString dec) // Output: my data
Ruby Example
For Ruby, you can encrypt data with key-based AES-128-CBC:
aes = AesEncryption.new()
key = aes.random_key_gen()
enc = aes.encrypt("my data")
puts key // Output: key
puts enc // Output: encrypted data
Scala Example
To decrypt data key-based in Scala:
val data = "NXOjXelxtIDgb+LMnIseCSQB6MvLRfMP1bMiqtCGRGdt6uR0zSV8zDShmZhY4z4xFSXhxGwGhjQhvMA53qBnEyhquf3b7PEhdHvMKs="
val aes = new AesEncryption()
aes.setMasterKey("kC4y8+6dFS8uhPmIU0d+KjYT1nc7gGXiphT0p9Ax0as=")
val dec = aes.decrypt(data)
println(new String(dec)) // Output: my data
Node.js Example
For file encryption and decryption in Node.js:
const aes = new AesEncryption();
const key = aes.randomKeyGen();
var path = "path/to/file.txt";
var encPath = aes.encryptFile(path);
var decPath = aes.decryptFile(encPath);
Troubleshooting Tips
- If your encrypted data is not decrypting properly, ensure that the passwords match in both code sections.
- Errors related to supported key lengths may occur; verify that your implementation aligns with the standards (e.g., 128 bits).
- If you encounter issues related to libraries, double-check that all required dependencies are correctly installed.
- For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

