MrSplosion
MrSplosion

Reputation: 193

Encoding a Char in Java

So I'm trying to simply encode a character into a file but for some reason it's not working. Here where I've found the example code and here is my code:

public class derp : MonoBehaviour{
    public Char[] chars = new Char[] {'a', 'b', 'c', 'r', 't', 'h'};

    void Start(){
        UTF8Encoding utf8 = new UTF8Encoding();
        Byte[] encodedBytes = utf8.GetBytes(chars);

        FileStream stream = new FileStream(Application.dataPath+"/testing.wld", FileMode.Create);
        for(int i = 0; i < chars.Length; i++){
            stream.WriteByte(encodedBytes[i]);
        }
        stream.Close();
    }
}

I ran the script, opened it and this is what the file looks like:

I don't want any old person to be able to open the file and edit the data that's in it. However the picture clearly shows it's possible. My code isn't any different than the code in the example but for some reason the file doesn't have encoded data. So why isn't this working? Why isn't the data encoded?

Upvotes: 0

Views: 203

Answers (3)

compilererror
compilererror

Reputation: 889

Well, the data is encoded, but it sounds like you wanted it to be encrypted. UTF-8 isn't for cryptography; it's just a character set. UTF-8 encoding looks exactly like ASCII, which is why Notepad can read the file.

If you want to encrypt the file, one option is RSA encryption. Check out the RSACryptoServiceProvider class:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx

Upvotes: 3

Kerrek SB
Kerrek SB

Reputation: 477040

The data is being encoded, and everything is working as documented, and as expected by everyone who knows UTF8. Perhaps your own expectations don't match reality? What did you think "UTF8" was and what "encoding" meant?

Upvotes: 0

Rich O&#39;Kelly
Rich O&#39;Kelly

Reputation: 41757

It will be encoded - in UTF8...

Upvotes: 0

Related Questions