PHP and C# AES256 encryption -> decryption

✔ Recommended Answer

php code:

define('AES_128_ECB', 'aes-128-ecb');$encryption_key = "MY_16_CHAR_KEY:)";$data = "MyOwnEncryptedSecretText";$encryptedData = openssl_encrypt($data, AES_128_ECB, $encryption_key, 0);

C# code:

public String Decrypt(String text, String key){    //decode cipher text from base64    byte[] cipher = Convert.FromBase64String(text);    //get key bytes    byte[] btkey = Encoding.ASCII.GetBytes(key);    //init AES 128    RijndaelManaged aes128 = new RijndaelManaged();    aes128.Mode = CipherMode.ECB;    aes128.Padding = PaddingMode.PKCS7;    //decrypt    ICryptoTransform decryptor = aes128.CreateDecryptor(btkey, null);    MemoryStream ms = new MemoryStream(cipher);    CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);    byte[] plain = new byte[cipher.Length];    int decryptcount = cs.Read(plain, 0, plain.Length);    ms.Close();    cs.Close();    //return plaintext in String    return Encoding.UTF8.GetString(plain, 0, decryptcount);}

and usage of it:

string DecryptedText = Decrypt("GENERATED_KEY", "MY_16_CHAR_KEY:)");

Now it works great :)Thanks.

Source: stackoverflow.com

Answered By: djxm2m

Method #2

AES256 encryption and decryption can be performed using PHP and C# with the help of appropriate cryptographic libraries in both languages.

In PHP, the OpenSSL extension provides support for AES encryption and decryption. Here's an example code snippet that demonstrates how to encrypt and decrypt a string using AES256 in PHP:

php
$key = 'mysecretkey12345'; $data = 'Hello, world!'; // Encrypt data $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc')); $encrypted = openssl_encrypt($data, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv); // Decrypt data $decrypted = openssl_decrypt($encrypted, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv); echo $decrypted; // Output: Hello, world!

In this example, the $key variable contains the encryption key, and the $data variable contains the data to be encrypted. The openssl_random_pseudo_bytes() function is used to generate a random initialization vector (IV), which is used to ensure that the same plaintext does not produce the same ciphertext. The openssl_encrypt() function is then used to encrypt the data using the AES256 cipher in CBC mode. The $encrypted variable contains the encrypted data and the $iv variable contains the IV. The openssl_decrypt() function is used to decrypt the encrypted data using the same key and IV.

In C#, the System.Security.Cryptography namespace provides support for AES encryption and decryption. Here's an example code snippet that demonstrates how to encrypt and decrypt a string using AES256 in C#:

csharp
string key = "mysecretkey12345"; string data = "Hello, world!"; // Encrypt data byte[] iv = new byte[16]; using (var rng = new RNGCryptoServiceProvider()) { rng.GetBytes(iv); } byte[] encrypted; using (var aes = Aes.Create()) { aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = iv; using (var encryptor = aes.CreateEncryptor()) { encrypted = encryptor.TransformFinalBlock(Encoding.UTF8.GetBytes(data), 0, data.Length); } } // Decrypt data string decrypted; using (var aes = Aes.Create()) { aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = iv; using (var decryptor = aes.CreateDecryptor()) { decrypted = Encoding.UTF8.GetString(decryptor.TransformFinalBlock(encrypted, 0, encrypted.Length)); } } Console.WriteLine(decrypted); // Output: Hello, world!

In this example, the key variable contains the encryption key, and the data variable contains the data to be encrypted. The RNGCryptoServiceProvider class is used to generate a random IV, which is stored in the iv variable. The Aes.Create() method is used to create a new AES object, which is configured with the key and IV. The CreateEncryptor() method is used to create an encryptor object, which is used to encrypt the data. The CreateDecryptor() method is used to create a decryptor object, which is used to decrypt the encrypted data.

Note that in both PHP and C#, it's important to use a secure and unique key and IV for each encryption operation. The key and IV should be kept secret and should not be shared between different encryption operations.

Comments

Most Popular

PhpStorm, return value is expected to be 'A', 'object' returned

Remove Unicode Zero Width Space PHP

Laravel file upload returns forbidden 403, file permission is 700 not 755