Note that there are some explanatory texts on larger screens.

plurals
  1. POEncrypting a file with 3 passwords
    primarykey
    data
    text
    <p>This is kind of an unusual problem. I am having difficulty encrypting a file using 3 passwords. I am attempting to wrap one CryptoStream around two other CryptoStreams, but when I write the file to the disk, it seems to become corrupted, and the padding cannot be completely removed. Why would this be happening?</p> <p>Edit: Here's some sample code</p> <pre><code> public static Stream Encrypt(Stream source, int delcount, params keyPair[] cryptInfo) { Stream prevStream = source; foreach (keyPair et in cryptInfo) { Rijndael mydale = Rijndael.Create(); mydale.BlockSize = 256; mydale.KeySize = 256; mydale.IV = et.IV; mydale.Key = et.key; CryptoStream mystream = new CryptoStream(prevStream, mydale.CreateEncryptor(), CryptoStreamMode.Write); prevStream = mystream; } return prevStream; </code></pre> <p>}</p> <p>Here's the full program Program.cs</p> <pre><code>class Program { static string opcode = "test"; static string IDCID = "an ID"; static string password = "A strong password"; static void Main(string[] args) { if (Console.ReadLine() == "encrypt") { Stream thestream = File.Open(Environment.CurrentDirectory + "\\sample.txt", FileMode.Create, FileAccess.ReadWrite); PasswordDeriveBytes mybytes = new PasswordDeriveBytes(Encoding.Unicode.GetBytes(opcode), Encoding.ASCII.GetBytes(opcode)); byte[] key = mybytes.GetBytes(32); mybytes = new PasswordDeriveBytes(Encoding.Unicode.GetBytes((IDCID.Length + password.Length + opcode.Length * 15).ToString()), Encoding.ASCII.GetBytes((IDCID.Length + password.Length + 5 + opcode.Length * 24).ToString())); byte[] IV = mybytes.GetBytes(32); keyPair mypair = new GlobalGridCore.keyPair(IV, key); mybytes = new PasswordDeriveBytes(Encoding.Unicode.GetBytes(password), Encoding.ASCII.GetBytes(password)); key = mybytes.GetBytes(32); mybytes = new PasswordDeriveBytes(Encoding.Unicode.GetBytes((IDCID.Length + password.Length + opcode.Length * 9).ToString()), Encoding.ASCII.GetBytes((IDCID.Length + password.Length + 7 + opcode.Length * 24).ToString())); IV = mybytes.GetBytes(32); keyPair secondpair = new keyPair(IV, key); mybytes = new PasswordDeriveBytes(Encoding.Unicode.GetBytes(IDCID), Encoding.ASCII.GetBytes(IDCID)); key = mybytes.GetBytes(32); mybytes = new PasswordDeriveBytes(Encoding.Unicode.GetBytes((IDCID.Length + password.Length + opcode.Length * 2).ToString()), Encoding.ASCII.GetBytes((IDCID.Length + password.Length + 14 + opcode.Length * 7).ToString())); IV = mybytes.GetBytes(32); keyPair thirdpair = new keyPair(IV, key); keyPair[] list = new keyPair[] { mypair, secondpair, thirdpair }; thestream = gridCrypto.Encrypt(thestream, 0, list); BinaryWriter mywriter = new BinaryWriter(thestream); mywriter.Write("ehlo"); mywriter.Write(new byte[512]); mywriter.Flush(); } else { Stream thestream = File.Open(Environment.CurrentDirectory + "\\sample.txt", FileMode.Open, FileAccess.ReadWrite); PasswordDeriveBytes mybytes = new PasswordDeriveBytes(Encoding.Unicode.GetBytes(opcode), Encoding.ASCII.GetBytes(opcode)); byte[] key = mybytes.GetBytes(32); mybytes = new PasswordDeriveBytes(Encoding.Unicode.GetBytes((IDCID.Length + password.Length + opcode.Length * 15).ToString()), Encoding.ASCII.GetBytes((IDCID.Length + password.Length + 5 + opcode.Length * 24).ToString())); byte[] IV = mybytes.GetBytes(32); keyPair mypair = new GlobalGridCore.keyPair(IV, key); mybytes = new PasswordDeriveBytes(Encoding.Unicode.GetBytes(password), Encoding.ASCII.GetBytes(password)); key = mybytes.GetBytes(32); mybytes = new PasswordDeriveBytes(Encoding.Unicode.GetBytes((IDCID.Length + password.Length + opcode.Length * 9).ToString()), Encoding.ASCII.GetBytes((IDCID.Length + password.Length + 7 + opcode.Length * 24).ToString())); IV = mybytes.GetBytes(32); keyPair secondpair = new keyPair(IV, key); mybytes = new PasswordDeriveBytes(Encoding.Unicode.GetBytes(IDCID), Encoding.ASCII.GetBytes(IDCID)); key = mybytes.GetBytes(32); mybytes = new PasswordDeriveBytes(Encoding.Unicode.GetBytes((IDCID.Length + password.Length + opcode.Length * 2).ToString()), Encoding.ASCII.GetBytes((IDCID.Length + password.Length + 14 + opcode.Length * 7).ToString())); IV = mybytes.GetBytes(32); keyPair thirdpair = new keyPair(IV, key); keyPair[] list = new keyPair[] { mypair, secondpair, thirdpair }; thestream = gridCrypto.Decrypt(thestream, list); BinaryReader myreader = new BinaryReader(thestream); Console.WriteLine(myreader.ReadString()); Console.ReadLine(); } } } </code></pre> <p>cryptDriver.cs</p> <pre><code>abstract class gridCrypto { /// &lt;summary&gt; /// Decrypts the input stream to the output stream /// &lt;/summary&gt; /// &lt;param name="source"&gt;I&lt;/param&gt; /// &lt;param name="dest"&gt;O&lt;/param&gt; /// &lt;param name="cryptInfo"&gt;U&lt;/param&gt; public static Stream Decrypt(Stream source, params keyPair[] cryptInfo) { Stream prevStream = source; foreach (keyPair et in cryptInfo) { Rijndael mydale = Rijndael.Create(); mydale.BlockSize = 256; mydale.KeySize = 256; mydale.IV = et.IV; mydale.Key = et.key; CryptoStream mystream = new CryptoStream(prevStream, mydale.CreateDecryptor(), CryptoStreamMode.Read); prevStream = mystream; } return prevStream; } /// &lt;summary&gt; /// Encrypts the input stream and securely deletes the input file with the specified number of passes. The source stream MUST have length /// &lt;/summary&gt; /// &lt;param name="source"&gt;The source stream (to be deleted)&lt;/param&gt; /// &lt;param name="dest"&gt;The destination stream&lt;/param&gt; /// &lt;param name="delcount"&gt;The number of passes to erase the file&lt;/param&gt; /// &lt;param name="cryptInfo"&gt;Crypto stuff&lt;/param&gt; public static Stream Encrypt(Stream source, int delcount, params keyPair[] cryptInfo) { Stream prevStream = source; foreach (keyPair et in cryptInfo) { Rijndael mydale = Rijndael.Create(); mydale.BlockSize = 256; mydale.KeySize = 256; mydale.IV = et.IV; mydale.Key = et.key; CryptoStream mystream = new CryptoStream(prevStream, mydale.CreateEncryptor(), CryptoStreamMode.Write); prevStream = mystream; } return prevStream; //int cpos = 0; //while (cpos &lt; delcount) //{ // source.Position = 0; // while (source.Position &lt; source.Length) // { // if (source.Length - source.Position &gt; 512) // { // Random mrand = new Random(); // byte[] thearray = new byte[512]; // mrand.NextBytes(thearray); // source.Write(thearray, 0, thearray.Length); // } // else // { // Random mrand = new Random(); // byte[] thearray = new byte[source.Length-source.Position]; // mrand.NextBytes(thearray); // source.Write(thearray, 0, thearray.Length); // source.Flush(); // } // } // cpos += 1; //} } } class keyPair { public byte[] IV; public byte[] key; public keyPair(byte[] InitializationVector, byte[] Key) { IV = InitializationVector; key = Key; } } </code></pre> <p>The code to delete the file is commented out and is not used in the program.</p>
    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.
 

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