c# - Accessing a decrypted Rijndael encrypted file with a StreamReader without writing to disk -
c# - Accessing a decrypted Rijndael encrypted file with a StreamReader without writing to disk -
i'm working on encryption/decryption of plain text files embedded in .dll file. plain text files scripts, , beingness parsed , when they're needed.
at minute, how (plain text) script files beingness 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 single line of script string, parses it. looks through each line in turn, , performs each of actions define in script found.
since out dll files aren't encrypted in anyway, we've decided should encrypt each of script files before embedded dll. decided on rijndael algorithm because of ease of can implemented in c#/.net.
currently encryption runs 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 decryption matter of running few commands , getting decrypted file... except that, because of design of rest of code (encryption seems have been "once we've got scheme running given standard"), file needs returned streamreader object. @ minute, how i'm decrypting requested file:
string decodedfile = null; filestream filetodecrypt= new filestream(_assembly.getmanifestresourcestream(file).tostring(), filemode.open); /* know above line overly complicated, it's because our files read streamreader objects (as i've explained above) */ rijndaelmanaged rijndaelcryptography = new rijndaelmanaged(); seek { cryptostream cs = new cryptostream(filetodecrypt, rijndaelcryptography .createdecryptor(key, iv), cryptostreammode.read); using (streamreader decryptreader = new streamreader(cs)) { decodedfile = decryptreader.readtoend(); } grab (exception e) { messagebox.show(e.message); }
does know how can decrypt file, using rijndael, have output accessible / read instance of streamreader object?
i've had long around net ways , closest came copying contents of decryptreader streamreader object instead of decrypting out string, seems can't re-create contents of 1 streamreader streamreader - right me if i'm wrong.
i'd love have alter little of existing (that haven't shared here) code base of operations possible cause bugs arise. i'd love without writing file disk (there's reason we're encrypting them, obviously).
thank in advance, , sorry if haven't been clear plenty or little vague. can add together more info if need help me.
jamie
you convert decoded file contents byte array , expose stream:
var stream = new system.io.memorystream(system.text.encoding.unicode.getbytes(decodedfile)); var streamreader = new streamreader(stream);
also, should disposing of rijndaelmanaged
instance placing in using block:
using (rijndaelmanaged rijndaelcryptography = new rijndaelmanaged()) { ... }
update
you can encrypted file so, , cryptostream constructor still happy:
stream filetodecrypt= _assembly.getmanifestresourcestream(file); ... cryptostream cs = new cryptostream(filetodecrypt, rijndaelcryptography.createdecryptor(key, iv), cryptostreammode.read);
c# .net dll embedded-resource rijndaelmanaged
Comments
Post a Comment