Hi. 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? EDIT: This was SOLVED. It turned out to simply be a server filesystem problem. It was fixed by repairing the virtual disk file.
Edit: Here's some sample 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;
}
Here's the full program Program.cs
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();
}
}
}
cryptDriver.cs
abstract class gridCrypto
{
/// <summary>
/// Decrypts the input stream to the output stream
/// </summary>
/// <param name="source">I</param>
/// <param name="dest">O</param>
/// <param name="cryptInfo">U</param>
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;
}
/// <summary>
/// Encrypts the input stream and securely deletes the input file with the specified number of passes. The source stream MUST have length
/// </summary>
/// <param name="source">The source stream (to be deleted)</param>
/// <param name="dest">The destination stream</param>
/// <param name="delcount">The number of passes to erase the file</param>
/// <param name="cryptInfo">Crypto stuff</param>
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 < delcount)
//{
// source.Position = 0;
// while (source.Position < source.Length)
// {
// if (source.Length - source.Position > 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;
}
}
The code to delete the file is commented out and is not used in the program.