Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have come out with <a href="http://itscommonsensestupid.blogspot.com/2009/06/how-to-encrypt-string-in-net-and.html" rel="nofollow noreferrer">a solution in my blog</a>. </p> <p>Here is an excerpt :</p> <pre><code> private static string CreateEncryptedString(string myString, string hexiv, string key) { RijndaelManaged alg = new RijndaelManaged(); alg.Padding = PaddingMode.Zeros; alg.Mode = CipherMode.CBC; alg.BlockSize = 16 * 8; alg.Key = ASCIIEncoding.UTF8.GetBytes(key); alg.IV = StringToByteArray(hexiv); ICryptoTransform encryptor = alg.CreateEncryptor(alg.Key, alg.IV); MemoryStream msStream = new MemoryStream(); CryptoStream mCSWriter = new CryptoStream(msStream, encryptor, CryptoStreamMode.Write); StreamWriter mSWriter = new StreamWriter(mCSWriter); mSWriter.Write(myString); mSWriter.Flush(); mCSWriter.FlushFinalBlock(); var EncryptedByte = new byte[msStream.Length]; msStream.Position = 0; msStream.Read(EncryptedByte, 0, (int)msStream.Length); return ByteArrayToHexString(EncryptedByte); } public static byte[] StringToByteArray(String hex) { int NumberChars = hex.Length; byte[] bytes = new byte[NumberChars / 2]; for (int i = 0; i &lt; NumberChars; i += 2) bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16); return bytes; } public static string ByteArrayToHexString(byte[] ba) { StringBuilder hex = new StringBuilder(ba.Length * 2); foreach (byte b in ba) hex.AppendFormat("{0:x2}", b); return hex.ToString(); } </code></pre> <p>There is one thing to note. The key size must be 16. Which means that key parameter must be a string with 16 characters. Or else the encryption won't work and will throw a CryptographicException.</p> <p>On the PHP end, here's how you do the decoding:</p> <pre><code>$cipher_alg = MCRYPT_RIJNDAEL_128; $decrypted_string = mcrypt_decrypt($cipher_alg, $key, $encrypted_string , MCRYPT_MODE_CBC, trim(hex2bin(trim($hexiv)))); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload