T
T
theonic2015-07-31 23:01:24
WPF
theonic, 2015-07-31 23:01:24

Decrypting a file into a string - how to implement it?

I need to encrypt and decrypt a file with a key, probably using AES.
But I need to decrypt it without writing to disk, i.e. the result of the decryption must be written to a string (or to a TextBox if in WPF).
How would you do it?
For example, there is this code:

public static void aesDecryptFile(string inputFile, string outputFile, string skey)
        {
            RijndaelManaged aes = new RijndaelManaged();
            try
            {
                PasswordDeriveBytes derivedPassword = new PasswordDeriveBytes(skey, Encoding.ASCII.GetBytes("o1q"), "SHA1", 1);
                byte[] keyBytes = derivedPassword.GetBytes(256 / 8);
                byte[] initialVectorBytes = Encoding.ASCII.GetBytes("OFRqsfcn*aze01xY");
                byte[] key = Encoding.ASCII.GetBytes(skey);
                using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open))
                {
                    using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
                    {
                        using (CryptoStream cs = new CryptoStream(fsCrypt, aes.CreateDecryptor(keyBytes, initialVectorBytes), CryptoStreamMode.Read))
                        {
                            int data;
                            while ((data = cs.ReadByte()) != -1)
                            {
                                fsOut.WriteByte((byte)data);
                            }
                            aes.Clear();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                aes.Clear();
            }
        }

This code writes the decrypted file to disk. And how to modify it so that he would write it to a string? Or in a TextBox? Without writing to disk, I mean.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasiliy, 2015-07-31
@Applez

Read the file into a string and decrypt it, there is nothing complicated about it.

S
Stanislav Makarov, 2015-08-01
@Nipheris

Replace FileStreams with MemoryStreams on top of buffers, and do whatever you want with buffers. That's why the flow abstraction exists, it's a sin not to use it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question