0

I tried to encrypt data using this function in PHP and decrypt it with the other function in C#. But I don't get the same string.

//php function public function onCrypt($text) { $key=md5('DFDFDFDFDFDFDFDFDFDFDFDF',true); $crypttext = urldecode(trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$key, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND)))); $text_crp =base64_encode($crypttext); return $text_crp; } 

//c# function

//public static void DecryptFile Parameters : strKey : the key choosed in decryption . PathPlainTextFile : path of the crypted file PathPlainTextFile : the original file decrypted.

public static void DecryptFile(string strKey, string pathPlainTextFile, string pathCypheredTextFile) { //crypt key with md5 function System.Security.Cryptography.MD5 alg = System.Security.Cryptography.MD5.Create(); System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding(); byte[] md5val = alg.ComputeHash(enc.GetBytes(strKey)); StreamReader fsPlainTextFile = File.OpenText(pathPlainTextFile); FileInfo t = new FileInfo(pathCypheredTextFile); StreamWriter Tex =t.CreateText(); string input = null; while ((input = fsPlainTextFile.ReadLine()) != null) { byte[] cipheredData = Convert.FromBase64String(input); RijndaelManaged rijndaeld = new RijndaelManaged(); // define the used mode rijndaeld.Mode = CipherMode.ECB; // create the cipher AES - Rijndael ICryptoTransform decryptor = rijndaeld.CreateDecryptor(md5val,null); // Write the ciphered data in MemoryStream MemoryStream ms= new MemoryStream(cipheredData); CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read); // Insert the ciphered data in a byte array byte[] plainTextData = new byte[cipheredData.Length]; int decryptedByteCount = cs.Read(plainTextData, 0, plainTextData.Length); ms.Close(); cs.Close(); // Insert the ciphered data in string encoded on Base64 Tex.WriteLine (Encoding.UTF8.GetString(plainTextData, 0, decryptedByteCount)); } Tex.Close(); } 

2 Answers 2

1

ECB mode is not secure. You should use either CTR mode or CBC mode. It is also best to explicitly specify the padding you are going to use at both ends.

Sign up to request clarification or add additional context in comments.

2 Comments

but i not want save the vector IV in Data Base
CTR mode does not need an IV, just a nonce, which can be smaller.
0

At a quick glance, you're not supplying an IV to the C# decryptor:

ICryptoTransform decryptor = rijndaeld.CreateDecryptor(md5val, null); 

I'm not familiar with php, but it looks like you created an IV when you encrypted the content. You'll need to have that same IV to decrypt it in the C# code (you'd need the same IV to decrypt it even if you were doing the decryption through php).

4 Comments

rijndaeld.Padding=Padding.zero;
ECB mode does not use any IV.
This cannot be a valid answer, as ECB does not use any IV.
@AlexMaker - Hmmm....I'd delete it if I could but it won't let me delete the accepted answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.