c# - problem with hexadecimal values in xml document -
i have simple c# function takes 1 string encode , return it:
public static string encodestring(string input) { byte[] bchipertext = null; rijndaelmanaged rp = new rijndaelmanaged(); rp.key = utf8encoding.utf8.getbytes("!lb!&*w_4xc54_0w"); rp.iv = utf8encoding.utf8.getbytes("6&^fi6s5saks_ax6"); icryptotransform re = rp.createencryptor(); byte[] bcleartext = utf8encoding.utf8.getbytes(input); memorystream mstm = new memorystream(); cryptostream cstm = new cryptostream(mstm, re, cryptostreammode.write); cstm.write(bcleartext, 0, bcleartext.length); cstm.flushfinalblock(); bchipertext = mstm.toarray(); cstm.close(); mstm.close(); return system.text.asciiencoding.ascii.getstring(bchipertext); }
after call function parameter "hello" xml file this:
<?xml version="1.0" encoding="utf-8"?> <users> <user name="user1" password="?v?py????%???9?"/> </users>
everithing fine when open xml file in visual studio 2010 receive warning this:
error 1 character ' ', hexadecimal value 0x13 illegal in xml documents.
can tell have done wrong?can ignore warnings?
thanks
this problem:
return system.text.asciiencoding.ascii.getstring(bchipertext);
you're converting arbitrary binary data text treating if ascii. it's not. don't that.
the safest approach use base64:
return convert.tobase64string(bchipertext);
of course, client need same in revert, e.g. using convert.frombase64string
.
Comments
Post a Comment