简体   繁体   中英

Persisting text as byte array to flat file

Update

This is to hide my public key on the client side to make it slightly more difficult for prying eyes to get to it. Yes I know the intent of the public key is to stay public but I am trying to mitigate key substitution attacks and in addition to obfuscation, assembly merging, assembly signing and some other measures this is a part of the overall strategy. So in my code that gets shipped to the client side I want to be able to do something like this

string publicKey = @"random characters" //don't want this in the code
byte[] keyBytes = [............] //this should be in the code

I am not quite sure how do I take the text of my public key, convert it to a byte array and then use that in the client code to convert back into public key.

I reckon there is an easy way to do this but I am going round in circles trying to figure it out.

Essentially I have a series of text data which I want to be able to save as bytes to a flat file. I can read it as a byte array but when I use a BinaryWriter to write that byte array to a file I end up with the original text.

Can someone please help?

Just as every file, a text file is a binary file.

It just happens to be that in this case every binary number corresponds with a character, so when you open the file in a text editor, you see readable text.

Obligatory The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) .

You could create a little converter for this purpose.

string publicKey = "AsdfsSDhysffsdfsdfZ09";
Console.Write("byte[] keyBytes = { ");
Console.Write(String.Join(", ", Array.ConvertAll(Encoding.ASCII.GetBytes(publicKey), b => String.Format("0x{0:X2}", b))));
Console.WriteLine("};");

Run it. Then just copy the last line of the output to your source code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM