vb.net - how do you save text data into binary file to be unreadable -
well cannot find simple example on net me achieve aim without having learn whole new chapter on topic in vbnet. want save crucial "settings" such administrative password can used user computer access remote server computer.
in example have program works ok long data not text such "this password" not reveal numbers 1232.78 or boolean values
plain text i.e strings can read file using notepad.
is there way avoid showing of text data being visible in binary files?
imports system imports system.io public class form2 private sub button1_click(byval sender system.object, byval e system.eventargs) handles button1.click const filename string = "appsettings.txt" using writer binarywriter = new binarywriter(file.open(filename, filemode.create)) writer.write("this secret password") writer.write(1.25f) writer.write("c:\temp") writer.write(10) writer.write(true) end using end sub end class when open file appsettings.txt notepad shows
secret password ?c:\temp with other characters cannot show here. when read text file data in text file reads correctly.
so how program user not see human readable information of binary text files?
text doesnt obfuscated when written out binary. technically, numerics see odd characters not unreadable either - if know datatype work out values.
to make non readable, encrypt each string 1 one (as in proposed dupe link), or "wrap" stream in cryptostream. since hoping binarywriter pseudo encryption you, skip entirely. alternative store info in class , serialize using cryptostream write out.
the code below uses class hold these various settings , binaryformatter serialize class. serializer prevent having write out data bit bit, , if ever extend or change data saved, modify class, not crypto code. server info class:
<serializable> public class svrsettings public property server string public property username string public property password string public property someint int32 public property somethingelse datetime ' add else encrypted end class ' elsewhere put data save/encrypt in instance: svrdata = new svrsettings svrdata.username = "admin" svrdata.password = "some password" svrdata.someint = 42 svrdata.somethingelse = datetime.now svrdata.server = "addressof(mysvr)" save encrypted binary file. need new imports:
imports system.security.cryptography imports system.runtime.serialization.formatters.binary '... dim password string = "aweakpassword" dim key byte() = encoding.utf8.getbytes(password) dim iv(15) byte using rng new rngcryptoserviceprovider rng.getnonzerobytes(iv) end using using rijalg = rijndael.create() rijalg.padding = paddingmode.iso10126 ' using encryptor , filestream using encryptor icryptotransform = rijalg.createencryptor(key, iv), fs new filestream("c:\temp\crypto.bin", filemode.openorcreate or filemode.truncate) ' save iv "naked" filestream fs.write(iv, 0, iv.length) using cs new cryptostream(fs, encryptor, cryptostreammode.write) dim bf new binaryformatter bf.serialize(cs, svrdata) ' may not needed - doesnt hurt cs.flushfinalblock() end using end using end using serializing means data in class saved @ once. cryptostream wraps filestream , actual encryption.
you need iv read data back, rather having store somewhere, writes filestream before wrapped in cryptostream. read back:
' read new instance dim newsvrdata svrsettings using rijalg = rijndael.create() rijalg.padding = paddingmode.iso10126 using fs new filestream("c:\temp\crypto.bin", filemode.open) ' read iv first fs.read(iv, 0, iv.length) ' using encryptor , cryptostream using encryptor icryptotransform = rijalg.createdecryptor(key, iv), cs new cryptostream(fs, encryptor, cryptostreammode.read) dim bf new binaryformatter newsvrdata = ctype(bf.deserialize(cs), svrsettings) end using end using end using ' test if data made round trip: console.writeline(newsvrdata.username) console.writeline(newsvrdata.password) console.writeline(newsvrdata.server) console.writeline(newsvrdata.somethingelse) test results:
admin
password
addressof(mysvr)
2/6/2016 13:56:44 pm
you similar binary writer wrapping in cryptostream. value of serializing object is 1 line of code save values.
Comments
Post a Comment