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
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