John Davidson

Rijndael Encryption/Decryption C# to PHP conversion


I have an encryption decryption class written in C# and i need help writing same algorithm in PHP to obtain same output result, any help finding a reliable solution is highly appreciated, Thank you.

C# Code: (Input: admin, Output: nRy/sK5Z7ENvGsSwfcmLzw==)

static readonly string PasswordHash = "P@@Sw0rd";
static readonly string SaltKey = "S@LT&KEY";
static readonly string VIKey = "@1B2c3D4e5F6g7H8";

//for encrypt

public string Encrypt(string plainText)
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash,Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
byte[] cipherTextBytes;

using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cipherTextBytes = memoryStream.ToArray();
return Convert.ToBase64String(cipherTextBytes);

//to decrypt

public string Decrypt(string encryptedText)
byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };

var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
var memoryStream = new MemoryStream(cipherTextBytes);
var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];

int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());

PHP Code: (Input: admin, Output: LCvAt7BMJcG81a3foyMh8Q==)

$ciphertext_b64 = "";
$plaintext = "admin";
$password = "P@@Sw0rd";
$salt = "S@LT&KEY";
$iv = "@1B2c3D4e5F6g7H8";
$iterations = 1000;
$keyLength = 32;

$prepared_key = openssl_pbkdf2($password, $salt, $keyLength, $iterations, "sha256");

$ciphertext_b64 = base64_encode(openssl_encrypt($plaintext,"AES-256-CBC",$prepared_key,OPENSSL_RAW_DATA, $iv));
echo $ciphertext_b64 . "<br/>";

$plaintext = openssl_decrypt(base64_decode($ciphertext_b64),"AES-256-CBC", $prepared_key,OPENSSL_RAW_DATA, $iv);
echo $plaintext . "<br/>";

My Answer:
Rijndael is a symmetric key encryption algorithm that was selected as the Advanced Encryption Standard (AES) by the National Institute of Standards and Technology (NIST) in 2001. It supports key sizes of 128, 192, and 256 bits and block sizes of 128 bits.

Here is an example of how to perform Rijndael encryption and decryption in C:


void encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *ciphertext) {
AES_KEY aes_key;
AES_set_encrypt_key(key, 128, &aes_key);
AES_encrypt(plaintext, ciphertext, &aes_key);

void decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *plaintext) {
AES_KEY aes_key;
AES_set_decrypt_key(key, 128, &aes_key);
AES_decrypt(ciphertext, plaintext, &aes_key);

int main() {
unsigned char key[16] = "0123456789abcdef";
unsigned char plaintext[] = "Hello, world!";
unsigned char ciphertext[16];
unsigned char decryptedtext[16];

encrypt(plaintext, strlen(plaintext), key, ciphertext);
decrypt(ciphertext, 16, key, decryptedtext);

printf("Plaintext: %s\n", plaintext);
printf("Ciphertext: ");
for (int i = 0; i < 16; i++) {
printf("%02x", ciphertext[i]);
printf("Decrypted text: %s\n", decryptedtext);

return 0;

In this example, we first define functions `encrypt` and `decrypt` to perform encryption and decryption using the AES algorithm. We then generate a 128-bit key and a plaintext message, encrypt the plaintext, and decrypt the ciphertext back to the original plaintext. Finally, we print out the original plaintext, ciphertext, and decrypted text.

Please note that this example uses the OpenSSL library for AES encryption and decryption. Make sure to link the OpenSSL library when compiling the code.

Rate this post

4 of 5 based on 9090 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us