Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're not setting the <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm.iv.aspx" rel="nofollow"><code>IV</code></a> property of <code>crypt</code>, so it's starting off as a random value each time. You need to set it to the same value when decrypting as when encrypting - like a salt for hashing. EDIT: Given the way ECB works, it looks like the IV may be ignored, which is why your previous code worked without storing it.</p> <p>EDIT: While the IV part is certainly required for non-ECB, it's not enough. I'm not sure what the rest of the problem is, although:</p> <ul> <li>The ECB cipher mode isn't recommended - any reason for using it?</li> <li>You may well end up running into problems due to padding; I don't know if <code>BinaryFormatter</code> handles that for you automatically, but it's worth looking into.</li> </ul> <p>EDIT: Doh - I've worked out the bigger problem; you should indeed be using <code>inBytes</code>, as per Elian's comment. Currently you're completely ignoring the cipher-text - that's got <em>no</em> chance of working!</p> <p>Here's a complete program showing the whole thing hanging together:</p> <pre><code>using System; using System.IO; using System.Text; using System.Security.Cryptography; using System.Runtime.Serialization.Formatters.Binary; class Test { static void Main() { byte[] data = Serialize("Some arbitrary test data", "pass"); object x = Deserialize(data, "pass"); Console.WriteLine(x); } private static SymmetricAlgorithm CreateCryptoServiceProvider(string key) { byte[] passwordHash; using (MD5 md5 = MD5.Create()) { // It's not clear why you're taking the hash of the password... passwordHash = md5.ComputeHash(Encoding.UTF8.GetBytes(key)); } var crypt = new TripleDESCryptoServiceProvider(); crypt.Key = passwordHash; crypt.Mode = CipherMode.CBC; // This is the default anyway - can remove crypt.Padding = PaddingMode.PKCS7; // Ditto // Fix this to use a randomly generated one and store it for real code. crypt.IV = new byte[crypt.BlockSize / 8]; return crypt; } public static byte[] Serialize(object obj, string key) { var provider = CreateCryptoServiceProvider(key); using (MemoryStream memory = new MemoryStream()) { using (CryptoStream stream = new CryptoStream( memory, provider.CreateEncryptor(), CryptoStreamMode.Write)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, obj); } return memory.ToArray(); } } public static object Deserialize(byte[] inBytes, string key) { var provider = CreateCryptoServiceProvider(key); using (MemoryStream memory = new MemoryStream(inBytes)) { using (CryptoStream stream = new CryptoStream( memory, provider.CreateDecryptor(), CryptoStreamMode.Read)) { BinaryFormatter formatter = new BinaryFormatter(); return formatter.Deserialize(stream); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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