C# side you have to pad the iv to length of 32.
In PHP encrypt data like this:
<?php
$iv= "01234567890123456789012345678901";
$key= "this is they key";
$text= "This text will be encrypted...";
$bsize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$pad= $bsize - (strlen($text) % $bsize);
$text .= str_repeat(chr($pad), $pad);
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv);
$enc_text = base64_encode($encrypted);
echo "
$enc_text";
/* the output
* MCfBJe4H7U+9Qb48RkHlXUt1/it10mx+TG7lrCUjO4w=
*/
?>
In C# decrypt data like this:
using System; using System.Text; using System.Security.Cryptography; using System.IO; namespace Encryption { class MainClass { public static void Main (string[] args) { string enc_text = @"MCfBJe4H7U+9Qb48RkHlXUt1/it10mx+TG7lrCUjO4w="; string iv = "01234567890123456789012345678901"; string key = "this is they key"; byte[] bytes = Base64_Decode(enc_text); Console.WriteLine(Decrypt(bytes, key, iv)); } public static byte[] Base64_Decode (string str) { return Convert.FromBase64String (str); } public static string Decrypt (byte[] cipher, string key, string iv) { string retVal = ""; UTF8Encoding encoding = new UTF8Encoding (); byte[] Key = encoding.GetBytes (key); byte[] IV = encoding.GetBytes (iv); using (RijndaelManaged rj = new RijndaelManaged ()) { try { rj.Padding = PaddingMode.PKCS7; rj.Mode = CipherMode.CBC; rj.KeySize = 256; rj.BlockSize = 256; rj.Key = Key; rj.IV = IV; using (MemoryStream ms = new MemoryStream (cipher)) { using (CryptoStream cs = new CryptoStream (ms, rj.CreateDecryptor (Key, IV), CryptoStreamMode.Read)) { using (StreamReader sr = new StreamReader (cs)) { retVal = sr.ReadLine (); } } } } finally { rj.Clear (); } } return retVal; } } }
//QubeX2
Inga kommentarer:
Skicka en kommentar