Encrypting streams
From: John Young (polomint77ATAThotmail.com)
Date: 07/05/04
- Next message: Alberto: "See data in a DataSet"
- Previous message: Rob Lamb: "'System.Web' (are you missing an assembly reference?)"
- Next in thread: John Young: "Re: Encrypting streams [UPDATE]"
- Reply: John Young: "Re: Encrypting streams [UPDATE]"
- Reply: Beeeeeeeeeeeeves: "RE: Encrypting streams"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 5 Jul 2004 15:44:23 +0100
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
- Next message: Alberto: "See data in a DataSet"
- Previous message: Rob Lamb: "'System.Web' (are you missing an assembly reference?)"
- Next in thread: John Young: "Re: Encrypting streams [UPDATE]"
- Reply: John Young: "Re: Encrypting streams [UPDATE]"
- Reply: Beeeeeeeeeeeeves: "RE: Encrypting streams"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|