Re: error writing encrypted byte string to a file
From: Rob Teixeira (RobTeixeira_at_@msn.com)
Date: 11/03/04
- Previous message: Chuck Hartman: "Can't run Word Template Project"
- In reply to: billr: "error writing encrypted byte string to a file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Wed, 3 Nov 2004 17:59:35 -0500
Seems to be several problems with the code below.
First of all since you aren't setting the padding mode, you should be using
PKCS#7 padding by default. This requires that the last block be written in a
special manner. Failing to do this is the number 1 reason you get the "Bad
Data" error (as far as I can tell). In this case, after you finishing
writing data to your stream, you MUST call FlushFinalBlock on the
CryptoStream.
There are a few other minor glitches, but the code should work more or less.
For example, you are using a memory stream, and setting the position to 0
then reading it into a byte array. You can skip all that and simply call
objMemoryStream.ToArray(). Again, this isn't really critical.
However, one thing that will come back to haunt you is that you aren't
setting the Key and IV properties. In this case, it works because you are
reusing the DESCryptoServiceProvider object (which passes the same key to
the Encryptor and Decryptor object), but if you write code to files, you
won't be able to reuse the object (presumably, you will be decrypting days
or even years after the initial encryption), so you must deal with key
management. You can only successfully decrypt if you use the same key that
was used during the encryption processes.
I'm in the middle of writing a huge article about symmetric key block
ciphers that could give you a lot of pointers for this sort of thing, which
I will happily post here, but I'm afraid it won't be done until sometime
this weekend.
-Rob Teixeira
"billr" <billr@discussions.microsoft.com> wrote in message
news:3E6CC013-F5E3-4931-91DC-69B449584ED8@microsoft.com...
> I want to encrypt a string, and save it to file, so that later on I can
read
> in the string and decrypt it ... this SHOULD be pretty straight forward,
> unfortunately it is not!
>
> Here is a method that should perform both operations, however the data
that
> gets written to the file is nonsense, so when we try reading it in again,
the
> result is "Bad Data".
>
> I would appreciate any help on offer, thank you
>
> using System.Security.Cryptography;
> using System.IO;
>
> public static string WriteAndRead()
> {
> byte[] btOutBuffer = null;
> string strDataString = @"This is the data string that I want to encrypt";
>
> byte[] btInputBuffer = (new UnicodeEncoding()).GetBytes(strDataString);
>
> Console.WriteLine("data string : " + strDataString);
>
> DESCryptoServiceProvider objDes = new DESCryptoServiceProvider();
> ICryptoTransform objEncryptor = objDes.CreateEncryptor();
> Stream objStream = new MemoryStream();
> CryptoStream objCryptoStream = new CryptoStream(objStream, objEncryptor,
> CryptoStreamMode.Write);
>
> objCryptoStream.Write(btInputBuffer, 0, btInputBuffer.Length);
> objStream.Position = 0;
>
> btOutBuffer = new byte[objStream.Length];
> objStream.Read(btOutBuffer, 0, btOutBuffer.Length);
> objCryptoStream.Close();
>
> // Note we do not simply write to a FilesStream because we will pass the
> // data to another object which will insert into an XML document.
> // This code works 100% when written directly to a FileStream
> objStream = new FileStream("file.txt", FileMode.OpenOrCreate,
> FileAccess.Write);
> objStream.Write(btOutBuffer, 0, btOutBuffer.Length);
> objStream.Close();
>
> objStream = new FileStream("file.txt", FileMode.Open, FileAccess.Read);
> ICryptoTransform objDecryptor = objDes.CreateDecryptor();
> // whoops! cannot read the file
> objCryptoStream = new CryptoStream(objStream, objDecryptor,
> CryptoStreamMode.Read);
> Console.WriteLine("DECRYPTED string : " + (new
> StreamReader(objCryptoStream, new UnicodeEncoding())).ReadToEnd() );
>
> }
>
- Previous message: Chuck Hartman: "Can't run Word Template Project"
- In reply to: billr: "error writing encrypted byte string to a file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|