xml - Build & keep file into memory and encrypt (C#, WPF, LINQ) -
during creating xml file i'm doing encrypt fields. it's working ok. below code:
create xml class:
private void btnsave_click(object sender, routedeventargs e) { xmltextwriter xwriter = new xmltextwriter("test.xml", encoding.utf8); xwriter.formatting = formatting.indented; xwriter.writestartelement("root"); testviewclassdatacontext dc = new testviewclassdatacontext(); list<test_view> tvq = (from tt in dc.test_views select tt).tolist(); var propertiestestview = typeof(test_view).getproperties(); var testviewvalues = new list<string>(); looppropxml(tvq, propertiestestview, testviewvalues, xwriter); xwriter.writeendelement(); xwriter.close(); } public void looppropxml<t>(ienumerable<t> queryresult, propertyinfo[] properites, list<string> addedvalues, xmltextwriter xwriter) { foreach (var qrl in queryresult) { var values = new list<object>(); xwriter.writestartelement("tv"); foreach (var property in properites) { object value = property.getvalue(qrl, null); xwriter.writestartelement(property.name.tostring()); desencrypt testencrypt = new desencrypt(); string pass = "qwertyuiop"; string enctext = testencrypt.encryptstring((value == null ? "" : value.tostring()), pass); xwriter.writestring(enctext); xwriter.writeendelement(); values.add(value); } xwriter.writeendelement(); } }
here encrypt code took "microsoft academy c#":
static tripledes createdes(string key) { md5 md5 = new md5cryptoserviceprovider(); tripledes des = new tripledescryptoserviceprovider(); des.key = md5.computehash(encoding.unicode.getbytes(key)); des.iv = new byte[des.blocksize / 8]; return des; } public string encryptstring(string plaintext, string password) { byte[] plaintextbytes = encoding.unicode.getbytes(plaintext); memorystream mystream = new memorystream(); tripledes des = createdes(password); cryptostream cryptstream = new cryptostream(mystream, des.createencryptor(), cryptostreammode.write); cryptstream.write(plaintextbytes, 0, plaintextbytes.length); cryptstream.flushfinalblock(); return convert.tobase64string(mystream.toarray()); }
how change to:
1 - first build file , keep memory
2 - encrypt it
3 - last save it.
write unencrypted values file:
public void looppropxml<t>(ienumerable<t> queryresult, propertyinfo[] properites, list<string> addedvalues, xmltextwriter xwriter) { foreach (var qrl in queryresult) { var values = new list<object>(); xwriter.writestartelement("tv"); foreach (var property in properites) { object value = property.getvalue(qrl, null); xwriter.writestartelement(property.name.tostring()); xwriter.writestring(value.tostring()); xwriter.writeendelement(); values.add(value); } xwriter.writeendelement(); } }
writing file using xmltextwriter
creates , saves if want encrypt file after have done need read contents of file memory again , encrypt it:
private void btnsave_click(object sender, routedeventargs e) { const string filename = "test.xml"; xmltextwriter xwriter = new xmltextwriter(filename, encoding.utf8); xwriter.formatting = system.xml.formatting.indented; xwriter.writestartelement("root"); testviewclassdatacontext dc = new testviewclassdatacontext(); list<test_view> tvq = (from tt in dc.test_views select tt).tolist(); var propertiestestview = typeof(test_view).getproperties(); var testviewvalues = new list<string>(); looppropxml(tvq, propertiestestview, testviewvalues, xwriter); xwriter.writeendelement(); xwriter.close(); string unencrypted = file.readalltext(filename); desencrypt testencrypt = new desencrypt(); string pass = "qwertyuiop"; string enctext = testencrypt.encryptstring(unencrypted, pass); file.writealltext(filename, enctext); }
Comments
Post a Comment