Question

I'm currently working on encryption/decryption of plain text files that are embedded in a .dll file. The plain text files are scripts, and are being parsed as and when they're needed.

At the minute, this is how the (plain text) script files are being loaded :

string RunLocation = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
Assembly _assembly = Assembly.LoadFrom(RunLocation + "\\<name of dll>.dll");
s = new StreamReader(_assembly.GetManifestResourceStream(File));
RunScript(s);

The RunScript method reads a single line of the script into a string, then parses it. It looks through each line in turn, and performs each of the actions we define in the script as they are found.

Since out dll files aren't encrypted in anyway, we've decided that we should encrypt each of the script files before they are embedded into the dll. We decided on the Rijndael algorithm because of the ease of which it can be implemented in C#/.NET.

Currently encryption runs like this:

FileStream fileStreamForEncryption = new FileStream (inputFileToBeEncrypted, FileMode.Create);
RijndaelManaged rijndaelCryptography = new RijndaelManaged();
CryptoStream encryptionStream = new CryptoStream(fileStreamForEncryption, rijndaelCryptography .CreateEncryptor(key, iv), CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
  cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fileStreamForEncryption .Close()

This means that decryption is a matter of running a few commands and getting the decrypted file... except that, because of the design of the rest of the code (encryption seems to have been a "once we've got the system running to a given standard"), the file needs to be returned as a StreamReader object. At the minute, this is how I'm decrypting a requested file:

string decodedFile = null;
FileStream fileToDecrypt= new FileStream(_assembly.GetManifestResourceStream(File).ToString(), FileMode.Open);
/* I know that the above line is overly complicated, it's because our files are read into StreamReader objects (as I've explained above) */
RijndaelManaged rijndaelCryptography = new RijndaelManaged();
try
{
  CryptoStream cs = new CryptoStream(fileToDecrypt, rijndaelCryptography .CreateDecryptor(key, iv), CryptoStreamMode.Read);
  using (StreamReader decryptReader = new StreamReader(cs))
  {
    decodedFile = decryptReader.ReadToEnd();
  }
catch (Exception e)
{
MessageBox.Show(e.Message);
}

Does anyone know how I can decrypt a file, using Rijndael, but have the output accessible as a / read into an instance of a StreamReader object?

I've had a long look around The Internet for ways to do this and the closest I came was copying the contents of decryptReader into another StreamReader object instead of decrypting out to a string, but it seems that you can't copy the contents of one StreamReader to another StreamReader - correct me if I'm wrong.

I'd love to have to change as little of the existing (that which I haven't shared here) code base as possible as that could cause bugs to arise. I'd also love to do this without writing the file back to disk (there's a reason we're encrypting them, obviously).

Thank you in advance, and sorry if I haven't been clear enough or a little vague. I can add more information if you need it to help me.

Jamie

Was it helpful?

Solution

You could simply convert your decoded file contents to a byte array and expose this as a stream:

var stream = new System.IO.MemoryStream(System.Text.Encoding.Unicode.GetBytes(decodedFile));
var streamReader = new StreamReader(stream);

Also, you should probably be disposing of your RijndaelManaged instance by placing it in a using block:

using (RijndaelManaged rijndaelCryptography = new RijndaelManaged())
{
    ...
}

Update

You can get the encrypted file like so, and the CryptoStream constructor will still be happy:

Stream fileToDecrypt= _assembly.GetManifestResourceStream(File);
...
CryptoStream cs = new CryptoStream(fileToDecrypt, rijndaelCryptography.CreateDecryptor(key, iv), CryptoStreamMode.Read);

OTHER TIPS

I've had a long look around The Internet for ways to do this and the closest I came was copying the contents of decryptReader into another StreamReader object instead of decrypting out to a string, but it seems that you can't copy the contents of one StreamReader to another StreamReader - correct me if I'm wrong.

You are only slightly correct. While you can't copy from one StreamReader to another, you can read from one StreamReader and write to a StreamWriter backed by, say, a MemoryStream. You can then wrap the StreamReader around the data in the MemoryStream that was written to by a StreamWriter.

So what you want to do is quite possible. In fact, the biggest problem I see for your setup is dealing with the decryption key.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top