views:

40

answers:

1

Well guys I am in a bit of a pickle here...

I am doing some exercises on encrypting data. One of them are binary files. I am currently using triple DES to encrypt and decrypt the files both in VB.NET and C#...

Now the thing is, once it is decrypted in VB.NET and saved, i can execute it again...

But for some reason, my C# file is bigger! 20,4K where VB.NET one is 19,0. The C# file also is rendered unexecutable...

Upon a closer look. The files appear almost exactly the same, but C# seems to add in a few extra bytes here and there in (seemingly) random places...

I am currently using File.ReadAllText(String filepath, Encoding encoding); with UTF-8 encoding

thanks!

+3  A: 

You say you're using File.ReadAllText... but also that these are binary files. That makes me suggest that you're treating opaque binary data (e.g. the result of encryption) as if it were text (e.g. calling Encoding.GetString on it).

Don't do that.

Basically, encryption generally works on binary data - binary in, binary out. If you need to encrypt text to text, you'll usually apply a "normal" encoding to convert the text to binary data (e.g. Encoding.UTF8.GetBytes(text)) and then use Base64 to convert the opaque binary data to text in a lossless way - e.g. with Convert.ToBase64String(encrypted).

Decrypting is just the reverse: use Convert.FromBase64String(encryptedText) to get the encrypted binary data, decrypt it, and then use Encoding.UTF8.GetString(decrypted) to get back to the text.

Jon Skeet