locked
Encryption to Memory Stream RRS feed

  • Question

  • Hi,

    I would like to create function to encrypt dataset in ecrypted text file and in order to avoid using a temporary file, I would like to create an EncryptToMemory function.

    My base method is:

            public static void EncryptFile(String sInputFilename, String sOutputFilename, String sKey)
            {
                FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
                FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);
                try
                {
                    DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                    ICryptoTransform desencrypt = DES.CreateEncryptor();
                    CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);
    
                    byte[] bytearrayinput = new byte[fsInput.Length];
                    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
                    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
                    cryptostream.Close();
                }
                catch
                {
                    throw new System.ApplicationException("The encryption of the file failed");
                }
                finally
                {
                    if (fsInput != null)
                    {
                        fsInput.Close();
                        fsInput.Dispose();
                    }
                    if (fsEncrypted != null)
                    {
                        fsEncrypted.Close();
                        fsEncrypted.Dispose();
                    }
                }
    
            }
    

    So I build

            public static void EncryptFileFromMemory(String sOutputFilename, MemoryStream oStream, String sKey)
            {
                FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);
                try
                {
                    DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                    ICryptoTransform desencrypt = DES.CreateEncryptor();
                    CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);
    
                    byte[] bytearrayinput = new byte[oStream.Length];
                    oStream.Read(bytearrayinput, 0, bytearrayinput.Length);
                    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
                    cryptostream.Close();
                }
                catch
                {
                    throw new System.ApplicationException("The encryption of the file failed");
                }
                finally
                {
                    if (oStream != null)
                    {
                        oStream.Close();
                        oStream.Dispose();
                    }
                    if (fsEncrypted != null)
                    {
                        fsEncrypted.Close();
                        fsEncrypted.Dispose();
                    }
                }
    
            }
    

    but if I take one of my old encrypted file, decrypt it, encrypt it again with the memory encryption and decrypt it again, the file is then corrupted. What am I doing wrong? Why memory stream behave differently?

    Sunday, September 13, 2009 6:21 PM

Answers

  • Hi, I just found the bug, I was missing the oStream.Position =0 when decrypting. I just surprised by the size of the encrypted fille, I have a dataset encryption and when I was using temporary file, the size of the file was 12Kb and now that I'm using memorystream as temporary location, the file is much shorter.
    • Proposed as answer by Pawel Koselski Monday, September 14, 2009 7:39 AM
    • Marked as answer by eryang Thursday, September 17, 2009 6:03 AM
    Monday, September 14, 2009 7:12 AM

All replies

  • Hi,
    How exactly are you decrypting file and memory stream?

    Regards,
    Pawel
    Sunday, September 13, 2009 10:13 PM
  • Hi,

    I'm doing it like
            public static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey)
            {
                //Create a file stream to read the encrypted file back.
                FileStream fsread = new FileStream(sInputFilename,
                   FileMode.Open,
                   FileAccess.Read);
                StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
                try
                {
                    DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                    //A 64 bit key and IV is required for this provider.
                    //Set secret key For DES algorithm.
                    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                    //Set initialization vector.
                    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    
                    //Create a DES decryptor from the DES instance.
                    ICryptoTransform desdecrypt = DES.CreateDecryptor();
                    //Create crypto stream set to read and do a 
                    //DES decryption transform on incoming bytes.
                    CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read);
                    //Print the contents of the decrypted file.
                    fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
                    fsDecrypted.Flush();
                }
                catch
                {
                    fsDecrypted.Flush();
                    throw new System.ApplicationException("The decryption of the file failed");
                }
                finally
                {
                    if (fsDecrypted != null)
                    {
                        fsDecrypted.Close();
                        fsDecrypted.Dispose();
                    }
                    if (fsread != null)
                    {
                        fsread.Close();
                        fsread.Dispose();
                    }
                }
            }
    

    and like

            public static MemoryStream DecryptFileToMemory(string sInputFilename, string sKey)
            {
                //Create a file stream to read the encrypted file back.
                FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
                MemoryStream fsDecrypted = new MemoryStream();
                try
                {
                    DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                    //A 64 bit key and IV is required for this provider.
                    //Set secret key For DES algorithm.
                    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                    //Set initialization vector.
                    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    
                    //Create a DES decryptor from the DES instance.
                    ICryptoTransform desdecrypt = DES.CreateDecryptor();
                    //Create crypto stream set to read and do a 
                    //DES decryption transform on incoming bytes.
                    MemoryStream ms = new MemoryStream();
                    CryptoStream cryptostreamDecr = new CryptoStream(fsread,
                       desdecrypt,
                       CryptoStreamMode.Read);
                    //Print the contents of the decrypted file.
                    StreamReader oStream = new StreamReader(cryptostreamDecr);
                    Byte[] oByte = Encoding.ASCII.GetBytes(oStream.ReadToEnd());
                    fsDecrypted.Write(oByte,0,oByte.Length);
                    return fsDecrypted;
                }
                catch
                {
                    throw new System.ApplicationException("The decryption of the file failed");
                }
            }

    But if I loop by decrypting, encrypting, decrypting, I have an issue.
    Monday, September 14, 2009 6:27 AM
  • Hi, I just found the bug, I was missing the oStream.Position =0 when decrypting. I just surprised by the size of the encrypted fille, I have a dataset encryption and when I was using temporary file, the size of the file was 12Kb and now that I'm using memorystream as temporary location, the file is much shorter.
    • Proposed as answer by Pawel Koselski Monday, September 14, 2009 7:39 AM
    • Marked as answer by eryang Thursday, September 17, 2009 6:03 AM
    Monday, September 14, 2009 7:12 AM