0

My goal is to convert a string to a ByteArray, write this ByteArray AS a ByteArray to a string so it's unreadable but still readable again upon "ByteArray to String" conversion in C#.

This is how my code is right now:

string json = "{\"database\":{\"tables\":{\"Users\":[\"column\":{\"id\":\"1\", \"name\":\"Test\"}]}}}"; var bytes = Encoding.ASCII.GetBytes(json); File.WriteAllBytes("database.dat", bytes); 

This works in theory, however the final output file has the same content of the string, and not the converted ByteArray. This is what the file contains:

database.dat

{"database":{"tables":{"Users":["column":{"id":"1", "name":"Test"}]}}}

But I expected something like

l4@ˆC}nC(YXX>AI0ve‚22úL«*“ÑÃYgPæaiäi ’Ê¢±·Ä¿|^Û×RÉ!×¹ÝYPZŠO•QÚÉèT“g‘Ѳ¬¡\g²Ô

What am I doing wrong? Is this not a ByteArray? Is there another way to convert data to an unreadable file, and then be able to convert it back into a string in my program?

6
  • 2
    Nothing wrong, you encode your string as ASCII byte array, then save it in file. Then Notepad decodes it as ASCII string. Commented Jun 18, 2016 at 13:10
  • But that's exactly what I don't want? I want the file to be a bytearray and not readable text? That's not exactly "secure" :) Commented Jun 18, 2016 at 13:12
  • Your file is bytearray exactly, but Notepad just can decode it as ASCII string ;) Commented Jun 18, 2016 at 13:14
  • 2
    Well, being not human-readable doesn't mean it's secure at all! It's just obfuscated. "security by obscurity" can be quite dangerous! Commented Jun 18, 2016 at 13:15
  • You might use BinaryReader and BinaryWriter instead. Commented Jun 18, 2016 at 13:21

1 Answer 1

2

What am I doing wrong? Is this not a ByteArray? Is there another way to convert data to an unreadable file, and then be able to convert it back into a string in my program?

Depends how much unreadable you want it to be? In the most extreme case you might need to use encryption.

In your case, you are storing ASCII representation of the string into a file, so of course a text editor can read it back to you.

One way could be try converting the byte array which you obtained to base64 encoded string - and store that string in file. That way it will not be easily readable, however, someone else can still decode it if he/she tries. So the security guarantees provided aren't that much. But again, depends on your needs.

Sign up to request clarification or add additional context in comments.

3 Comments

That would ofcourse do it. If I may ask, do you know how other files do it? Like Databases etc?
@KevinJensenPetersen Depends how much security you want? If you want it to be secure, use encryption. Google symmetric encryption for example. But then you may need to take care of key management, etc.
You can also use a DeflateStream. As an added bonus you'll be saving a few bytes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.