c# - char[] array to textfile, including non-printing control characters -


i got quite tricky question (at least me), coding small , simple encryption , decryption program works polyalphabetical substitution.

this encryption function:

static string encr(string plaintext, string key)     {         char[] chars = new char[plaintext.length];         int h = 0;         (int = 0; < plaintext.length; i++)         {             if (h == key.length)                 h = 0;             int j = plaintext[i] + key[h];             chars[i] = (char)j;             h++;         }         streamwriter sw = new streamwriter(file_name, false, encoding.unicode);         (int x = 0; x < plaintext.length; x++)         {             sw.write(chars[x]);         }         sw.close();         return new string(chars);     } 

it's working fine, problem outputfile created streamwriter contains additional unwanted 00's (due unicode encoding) , 2 totally wrong beginning-values due unicode encoding

http://abload.de/img/unbenannt-16ilx4.jpg (sorry can't post images directly cause < 10rep)

ff fe 8a 00 a6 00 cc 00 a4 00 b0 00

bold ones correct ones, ff fe in beginning useless encryption/decryption, , 00's unwanted (i know standard unicode encoding, question how achieve without encoding still able display corresponding unicode chars)

i hope clear want achieve, write out characters only. in special case hex-view of encrypted file this: 8a a6 cc a4 b0, encoded unicode utf-8 according http://unicode-table.com/, corresponding letters Š¦Ì¤°

i have failed far in of attempts solve this. solution easy through...

the characters @ start 2 bytes of encoding preamble identify encoding used. think better of converting bytes , writing bytes general binary file without encoding.

as in:

static string encr(string plaintext, string key) {     char[] chars = new char[plaintext.length];     int h = 0;     (int = 0; < plaintext.length; i++)     {         if (h == key.length)             h = 0;         int j = plaintext[i] + key[h];         chars[i] = (char)j;         h++;     }      file.writeallbytes(file_name, system.text.encoding.utf8.getbytes(chars));      return new string(chars, system.text.encoding.utf8); } 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -