0

I have encrypted a file with c# and gave it the extention .crypted. The file extention .crypted is associated with my program. So if i double click the file i get a password request dialog from my program. After entering the correct password my program decrypts the file and writes it unencrypted to the disk without the .crypted extention, so that i can run it. After using the file i just delete the unencrypted file and if the file was altered i use my program to reencrypt the file first. But u can easily use a file recovery program to get unauthorized access to the unencrypted file. Thats why i dont want to write unencrypted files to the disk after my program decrypts them. Is it possible to open an encrypted file without ever writing it to the disk? Like saving the unencrypted file to the ram/memory and opening it from there? I dont want to write a whole file system for that. The encrypted files should stay on NTFS or ext4. I think i need to change the code at this point:

  using(CryptoStream cryptoStream = new CryptoStream(fileStream, rijndael.CreateDecryptor(), CryptoStreamMode.Read))
  {
    // write unencrypted file to memory and execute or something else?
  }
PhazBlue
  • 79
  • 8
  • 1
    The first parameter to `CryptoStream` is a stream, yes? If so, pass in a `MemoryStream`. – mjwills May 16 '18 at 22:12
  • 1
    See: [MemoryMappedFile](https://msdn.microsoft.com/en-us/library/system.io.memorymappedfiles.memorymappedfile(v=vs.110).aspx). – Jimi May 16 '18 at 22:17
  • a `CryptoStream` is just a stream, so you can pass it directly to anything that reads from a stream, like [`XmlSerializer.Deserialize()`](https://msdn.microsoft.com/en-us/library/dsh84875(v=vs.110).aspx) or [`JsonSerializer`](https://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm) or whatever. – dbc May 16 '18 at 22:21
  • @mjwills You can create if from an existing file or with [CreateNew](https://msdn.microsoft.com/en-us/library/system.io.memorymappedfiles.memorymappedfile.createnew(v=vs.110).aspx). No file needed in this case. – Jimi May 16 '18 at 22:22
  • @Jimi And what is the benefit of that vs `MemoryStream`? – mjwills May 16 '18 at 22:23
  • @mjwills Concurrency, multi-processing, multiple views with pre-defined tools already available. It's a good alternative choice, imo. – Jimi May 16 '18 at 22:26
  • 1
    I think the answer to this question depends largely on "what do you want to do with it?" From your question it seems the file is an executable you want to run? – Nyerguds May 16 '18 at 22:27
  • 1
    What **specifically** do you mean by "open an encrypted file"? If you mean "run a 3rd party program that reads the file" then no, in general what you're asking is not possible because for that 3rd party program to be able to open the file **it has to be a file**. Could you set up a "ram disk" and write the file to that? Or set up a "fake" file system where the unencrypted file can be read from? – Lasse V. Karlsen May 16 '18 at 22:30
  • May be relevant: (possibly even a duplicate candidate, depending on what OP is actually trying to do) [Q: Load an EXE file and run it from memory](https://stackoverflow.com/q/3553875/395685). Though the "if the file was altered" bit makes me doubt it's about running exe's... – Nyerguds May 16 '18 at 22:42
  • 1
    @mjwills That `CryptoStream` is his encrypted input. It's normal that that is a file on the disk. OP is asking how to handle what comes _out_ of it. – Nyerguds May 16 '18 at 22:45

0 Answers0