Re: error writing encrypted byte string to a file

From: Rob Teixeira (RobTeixeira_at_@msn.com)
Date: 11/03/04

  • Next message: Angelos Karantzalis: "Re: Manually set custom permissions based on User"
    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() );
    >
    > }
    >


  • Next message: Angelos Karantzalis: "Re: Manually set custom permissions based on User"

    Relevant Pages

    • Re: Byte array to string and back - newbie question
      ... // Create a symmetric algorithm. ... This is done to make encryption more ... // Encrypt a string into a string using a password ... // Decrypt a byte array into a byte array using a key and an IV ...
      (microsoft.public.dotnet.framework.aspnet.security)
    • How do I Use DPAPI to Encrypt and Decrypt Data (C#/VB.NET)?
      ... Use DPAPI to Encrypt and Decrypt Data ... The code below demonstrates how to call Data Protection API (DPAPI) ... In addition to encryption and decryption, ... public static string Encrypt ...
      (microsoft.public.dotnet.framework.aspnet.security)
    • RE: Using Win32 CryptDecrypt to Decrypt RijndaelManaged
      ... Did you check the encryption output from BOTH CAPI and .Net using the ... 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, ... DWORD cbKeySize; ...
      (microsoft.public.platformsdk.security)
    • RE: Using Win32 CryptDecrypt to Decrypt RijndaelManaged
      ... Did you check the encryption output from BOTH CAPI and .Net using the ... 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, ... DWORD cbKeySize; ...
      (microsoft.public.platformsdk.security)
    • Re: CryptAPI(encryption/decryption)
      ... It seems like you're missing the Base64 decode step when trying to decrypt ... I misspelled the Private Key as Primary Key. ... Is there any variation in the encryption format in openssl compared to ... "Dylan DSilva " wrote: ...
      (microsoft.public.pocketpc.developer)