Encrypting streams

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: John Young (polomint77ATAThotmail.com)
Date: 07/05/04


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



Relevant Pages

  • RE: Using Win32 CryptDecrypt to Decrypt RijndaelManaged
    ... I figured out how to use RijndaelManaged with AES in the C++ app. ... C++ crypto WILL successfully decrypt the .NET generated ... I am trying to write a Win32 app that can decrypt that string using the ... I can get both to encrypt and decrypt successfully in their own projects, ...
    (microsoft.public.platformsdk.security)
  • Re: Encrypt and Decrypt a file using .NET 2.0?
    ... public static string GenerateKey() ... DES des = DES.Create; ... // Distribute this key to the user who will decrypt this file. ... // Get the Key for the file to Encrypt. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: .NET Crypto Classes Interoperability with Win32 Crypto APIs
    ... >the hash of the string you are feeding in. ... when i encrypt a string using .NET classes and try ... >> decrypt it using Win32 APIs, ...
    (microsoft.public.dotnet.security)
  • Re: Encrypt and Decrypt a file using .NET 2.0?
    ... public static string GenerateKey() ... using (CryptoStream cryptoStream = new CryptoStream(outFile, ... // Distribute this key to the user who will decrypt this file. ... // Get the Key for the file to Encrypt. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Decryptionfailed to bring original text back....
    ... cryptography, but now using them on web.config, any idea, I like to ... There isn't really much reason to encrypt a .NET string with ... what happen when you decrypt the encrypted ...
    (microsoft.public.dotnet.security)