Reputation: 25
public string Encrypt(string text, string pass)
{
byte[] hash = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(pass));
byte[] iv = new byte[16];
return this.EncryptString(text, hash, iv);
}
public string EncryptString(string plainText, byte[] key, byte[] iv)
{
Aes aes = Aes.Create();
aes.Mode = CipherMode.CBC;
byte[] numArray = new byte[32];
Array.Copy(key, 0, numArray, 0, 32);
aes.Key = numArray;
aes.IV = iv;
MemoryStream memoryStream = new MemoryStream();
ICryptoTransform encryptor = aes.CreateEncryptor();
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
byte[] bytes = Encoding.UTF8.GetBytes(plainText);
cryptoStream.Write(bytes, 0, bytes.Length);
cryptoStream.FlushFinalBlock();
byte[] array = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
return Convert.ToBase64String(array, 0, array.Length);
}
public string DecryptString(string cipherText, byte[] key, byte[] iv)
{
Aes aes = Aes.Create();
aes.Mode = CipherMode.CBC;
byte[] numArray = new byte[32];
Array.Copy(key, 0, numArray, 0, 32);
aes.Key = numArray;
aes.IV = iv;
MemoryStream memoryStream = new MemoryStream();
ICryptoTransform decryptor = aes.CreateDecryptor();
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Write);
string empty = string.Empty;
try
{
byte[] buffer = Convert.FromBase64String(cipherText);
cryptoStream.Write(buffer, 0, buffer.Length);
cryptoStream.FlushFinalBlock();
byte[] array = memoryStream.ToArray();
return Encoding.UTF8.GetString(array, 0, array.Length);
}
finally
{
memoryStream.Close();
cryptoStream.Close();
}
}
These are the function which I have created to encrypt and decrypt on c# But how can I decrypt the encrypted data using PHP? I have gone through:
https://www.geeksforgeeks.org/how-to-encrypt-and-decrypt-a-php-string/
https://www.php.net/manual/en/function.openssl-encrypt.php
private void btnEncrypt_Click(object sender, EventArgs e)
{
string text = richTextBoxEncrypt.Text;
string pass = "DecyptionPass";
richTextBoxEncrypt.Text= Encrypt(text, pass);
}
private void btnDecrypt_Click(object sender, EventArgs e)
{
byte[] hash = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes("DecyptionPass"));
byte[] iv = new byte[16];
richTextBoxDecrypt.Text = DecryptString(richTextBoxDecrypt.Text, hash, iv);
}
Here is the example of the Plain text, encrypted text and the IV used
plaintext = "This is what i encrypted."
Encrypted Text = "COz8V8eqkCS2U9TrH6FDA4ZD648m1WSB7QUoDoGyr7k="
Key = "ivdecrypt"
Update: This is the PHP Code that im trying but i dont know the actual value for IV
<?php
// Store a string into the variable which
// need to be Encrypted
$simple_string = "This is what i encrypted.";
// Display the original string
echo "Original String: " . $simple_string;
// Store the cipher method
$ciphering = "AES-128-CBC";
// Use OpenSSl Encryption method
$iv_length = openssl_cipher_iv_length($ciphering);
$options = 0;
// Non-NULL Initialization Vector for encryption
$encryption_iv = 'I dont know';
// Store the encryption key
$encryption_key = "ivdecrypt";
// Use openssl_encrypt() function to encrypt the data
$encryption = openssl_encrypt($simple_string, $ciphering,
$encryption_key, $options, $encryption_iv);
// Display the encrypted string
echo "Encrypted String: " . $encryption . "\n";
// Non-NULL Initialization Vector for decryption
$decryption_iv = 'I dont know';
// Store the decryption key
$decryption_key = "ivdecrypt";
// Use openssl_decrypt() function to decrypt the data
$decryption=openssl_decrypt ($encryption, $ciphering,
$decryption_key, $options, $decryption_iv);
// Display the decrypted string
echo "Decrypted String: " . $decryption;
?>
Upvotes: 0
Views: 1774
Reputation: 6414
I wrote a sample program to show how encryption and decryption in PHP took place with similar result (encrypted string) to C#.
In your PHP source you are using AES 128 CBC but C# is using AES 256 CBC. You are using a string "I dont know" as source for the initialization vector (IV) but in C# it's a 16 length byte array filled with 16 "x00"s.
Here is the result:
pass:ivdecrypt
password:7b39fbd3f7a82a38dc565a10c236267a977e19fb0ba063f1e882cb6faaadf16f
plaintext=This is what i encrypted.
cipher=aes-256-cbc
encrypted to: COz8V8eqkCS2U9TrH6FDA4ZD648m1WSB7QUoDoGyr7k=
enc expected: COz8V8eqkCS2U9TrH6FDA4ZD648m1WSB7QUoDoGyr7k=
decrypted to: This is what i encrypted.
and the code. Beware that there is no proper error handling. Crypto warning: The usage of a static IV is UNSECURE.
<?php
// https://stackoverflow.com/questions/63033173/how-to-decrypt-byte-array-using-php-which-i-has-encrypted-on-c-sharp-as-show-o
// How to decrypt byte array using PHP which i has encrypted on c# ? As show on below code
$plaintext = 'This is what i encrypted.';
$pass = 'ivdecrypt';
$method = 'aes-256-cbc';
// Must be exact 32 chars (256 bit)
$password = substr(hash('sha256', $pass, true), 0, 32);
echo "pass:" . $pass . "\n";
echo "password:" . bin2hex($password) . "\n";
// IV must be exact 16 chars (128 bit)
$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
// encryption
$encrypted = base64_encode(openssl_encrypt($plaintext, $method, $password, OPENSSL_RAW_DATA, $iv));
// decryption
$decrypted = openssl_decrypt(base64_decode($encrypted), $method, $password, OPENSSL_RAW_DATA, $iv);
// output
echo 'plaintext: ' . $plaintext . "\n";
echo 'cipher: ' . $method . "\n";
echo 'encrypted to: ' . $encrypted . "\n";
echo 'enc expected: ' . "COz8V8eqkCS2U9TrH6FDA4ZD648m1WSB7QUoDoGyr7k=" . "\n";
echo 'decrypted to: ' . $decrypted . "\n\n";
?>
Upvotes: 2