String Encoding Help.

I wrote a method that takes a string and encrypts it using crypto RSA to a text file and then decrypts the text file to a textarea. I used Encodings.WindowsANSI for both textinputstream and textoutputstream. The problem is anytime in the string there is a ’ character [chr(39)] I get weird symbols back in the text area from the decrypt result. What encoding do I need to use to prevent these unwanted characters? Thanks!

Example:

There’s

Use UTF-8. Encrypt the string, then encode it as hex before saving it to the text file. When you read it back, decode the hex, decrypt it, and then define the encoding as UTF-8.

Thank you sir.

Kem I’ve tried and tried to get this working but for some reason I just can’t quite get it working. If I encrypt the string and then encode it as hex, write it to the text file, then read it back, decode it, and then decrypt it, it does nothing for the There’s issue. It looks exactly the same without encoding it to hex.

If I do it in reverse, meaning, if I encode the string to hex, then encrypt it, write it to the text file, then read it back, decrypt it, and decode the hex it only decrypts the first 128 bytes, leaving the rest encrypted. However, it does fix the There’s issue and return it as There’s instead. If I set EncodeHex(string, true) instead of false, it works! BUT, then other characters such as an o in the word or aren’t there or are encoded strangely as other ascii characters.

I’m pretty lost now. Any ideas? Thanks!

I could PM you my code if you feel like taking a look at it for me.

What happens if you encrypt the string and then immediately decrypt it, without writing it to the file? If that works, then it really is a matter of not having the correct encoding after you read it from the file. And, what are the byte values before and after?

That’s a good idea to test Tim. I’ll do some testing without writing to the file and see what kind of results I get as well as taking a closer look at the byte values before and after. I have noticed that they change quite a bit once encoded to Hex. Thanks for the ideas!

To read it has to be the reverse of writing

So for writing to disk

  • encrypt
  • hex
  • write bytes to disk

To read from disk

  • read bytes from disk
  • un hex
  • decrypt
  • DEFINE ENCODING as UTF-8

the last step on the READ from disk is VERY important as in the writing to disk you start with UTF-8 data and this last thing when reading basically tells Xojo that this is to be handled as UTF-8 data

Without it you will get weird looking results

Norman, I swear that’s exactly what I did and it looks the same as if I didn’t encode it to hex at all with the There’s stuff going on once un hexed and decrypted and defined as UTF8. Thanks for your input.

Will try again…

Ok so I was doing it right from the start, just like Kem and Norman said to do it. I took Tim’s advice and just worked with the string data and didn’t write it to a file. It turned out to be the string I had copied and pasted in to use as an example string. When I added this code to the string before I did any encoding or encrypting it fixed it.

s = ConvertEncoding(s, Encodings.WindowsLatin1)

Thanks for all the help!