Encrypting streams

J

John Young

Hi, I have been trying to encrypt and decrypt a filestream. I have a
problem where if I encrypt a file a set key and iv (eg. key is 'polomint'
and the IV is '12345678'), and then decrypt it with the same key but an IV
of 29384567, it still shows the file mostly unencrypted!....
Is it just me, or does the IV not have too much of a difference on
decrypting?

Here's my encryption code,,,,,


public string EncryptFile( string rawFile, string encFile, string key,
string iv )

{

///

/// Encrypts (rawFile) using (key) and (iv) and places result into (encFile)

///

if ( ! File.Exists( rawFile ) )

{

// the file to encrypt is not available

return "ERROR: The file to encrypt (rawFile) does not exist or is not
available";

}

string tmp = CheckKeyAndIV( key, iv );

if ( tmp != "OK" ) return tmp;

try

{

//Create variables to help with read and write.

byte[] bin = new byte[1024];

long rdlen = 0;

int len;

FileStream fsRaw = new FileStream( rawFile, FileMode.Open,
FileAccess.Read );

long totlen = fsRaw.Length;

FileStream fsEnc = new FileStream( encFile, FileMode.Create,
FileAccess.Write );

//fsEnc.SetLength( 0 );

byte[] bKey = Encoding.ASCII.GetBytes( key );

byte[] bIV = Encoding.ASCII.GetBytes( iv );

DESCryptoServiceProvider dcp = new DESCryptoServiceProvider();

CryptoStream cs = new CryptoStream( fsEnc, dcp.CreateEncryptor( bKey, bIV ),
CryptoStreamMode.Write );

// write out encrypted content into MemoryStream

while ( rdlen < totlen )

{

len = fsRaw.Read( bin, 0, bin.Length );

cs.Write( bin, 0, len );

rdlen = rdlen + len;

}

cs.FlushFinalBlock();

fsEnc.Close();

fsRaw.Close();

}

catch ( Exception x )

{

// oops

return "ERROR: " + x.Message;

}

return "OK";

}



I hope someone can help, coz I'm probably making a silly mistake somewhere.

TIA

John
 
J

John Young

I've just tried it on a text file and when I encrypt it, then decrypt with a
different IV, the first 8 characters are corrupt (what I expected), but the
rest of the text is decrypted (which it shouldn't be!).

Any ideas?

John
 
G

Guest

Hi, I have been trying to encrypt and decrypt a filestream. I have a
problem where if I encrypt a file a set key and iv (eg. key is 'polomint'
and the IV is '12345678'),

The IV should never be that. You don't normally choose what the IV is.
See inline.

and then decrypt it with the same key but an IV
of 29384567, it still shows the file mostly unencrypted!....
Is it just me, or does the IV not have too much of a difference on
decrypting?

Here's my encryption code,,,,,


public string EncryptFile( string rawFile, string encFile, string key,
string iv )

{

///

/// Encrypts (rawFile) using (key) and (iv) and places result into (encFile)

///

if ( ! File.Exists( rawFile ) )

{

// the file to encrypt is not available

return "ERROR: The file to encrypt (rawFile) does not exist or is not
available";

Use an enum, not strings, for error/success codes.
}

string tmp = CheckKeyAndIV( key, iv );

if ( tmp != "OK" ) return tmp;

Again, use an enum.
try

{

//Create variables to help with read and write.

byte[] bin = new byte[1024];

Don't hardcode how big it is. Glean this from the parameters passed in.

Oh this is too hard too explain. All I know is I had a devil's own job figuring out encryption, but I'll email you a proper explanation of it when I've got more time if you like, email me at benjtaylor at hotpop dot com if you want.


long rdlen = 0;

int len;

FileStream fsRaw = new FileStream( rawFile, FileMode.Open,
FileAccess.Read );

long totlen = fsRaw.Length;

FileStream fsEnc = new FileStream( encFile, FileMode.Create,
FileAccess.Write );

//fsEnc.SetLength( 0 );

byte[] bKey = Encoding.ASCII.GetBytes( key );

byte[] bIV = Encoding.ASCII.GetBytes( iv );

DESCryptoServiceProvider dcp = new DESCryptoServiceProvider();

CryptoStream cs = new CryptoStream( fsEnc, dcp.CreateEncryptor( bKey, bIV ),
CryptoStreamMode.Write );

// write out encrypted content into MemoryStream

while ( rdlen < totlen )

{

len = fsRaw.Read( bin, 0, bin.Length );

cs.Write( bin, 0, len );

rdlen = rdlen + len;

}

cs.FlushFinalBlock();

fsEnc.Close();

fsRaw.Close();

}

catch ( Exception x )

{

// oops

return "ERROR: " + x.Message;

}

return "OK";

}



I hope someone can help, coz I'm probably making a silly mistake somewhere.

TIA

John
 
J

Jon Skeet [C# MVP]

I've just tried it on a text file and when I encrypt it, then decrypt with a
different IV, the first 8 characters are corrupt (what I expected), but the
rest of the text is decrypted (which it shouldn't be!).

Any ideas?

No, that's exactly what you should expect. The IV is basically the
initial state, which can affect early data but not all data.

Don't think of the IV as being a proper key - it's not.
 
J

John Young

Aha, I think the penny has just dropped. I think I have it figured out
now.. Thanks for making that point Jon.

John Young
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top