0

I am try to pull out byte data from a source, encrypt it, and then store it in the file system.

For encryption, I am using jasypt and the BasicTextEncryptor class. And for storing on to the file system, I am using Apache's Commons IOUtils class.

When required, these files will be decrypted and then sent to the user's browser. This system works on my local machine where the default charset is MacRoman, but it fails on the server where the default charset is UTF-8.

When I explicitly set the encoding at each stage of the process to use MacRoman it works on the server as well, but I am skeptical about doing this as rest of my code uses UTF8.

Is there a way that I can work the code without conversion to MacRoman?

TRiG
  • 10,148
  • 7
  • 57
  • 107

2 Answers2

1

You should just use UTF8 everywhere.

As long as you use the same encoding at each end of an operation (and as long as the encoding can handle all of the characters you need), you'll be fine.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Hey SLaks, Thanks for the reply. The thing is that when I dont specify encoding or set it to UTF8 explicitly the file that is thrown to the server doesnt render at all. The page just gets stuck while loading. – Kunal Ghogale Jul 06 '12 at 18:40
  • I am reading it from the database in which it is stored in a gzipped format. I am just reading it using standard gzipped inputstream reader. – Kunal Ghogale Jul 06 '12 at 19:15
  • If you're reeading it into a string, you **are** using an encoding. The default is the system codepage, which is not UTF8. That is your problem. – SLaks Jul 06 '12 at 22:04
  • I am reading into a byte array as the column is a long blob using hibernate. This read is good as I am using the read byte array in another place where it works perfectly fine. – Kunal Ghogale Jul 06 '12 at 22:19
1

In your comments on another answer, you claim you're not using an encoding, but that's impossible. You're using the BasicTextEncryptor class, which according to this documentation only works on Strings and char arrays. That means that, at some point, you're converting from an encoding-agnostic byte array to an encoding-specific String or char array. That means that you're relying upon an encoding somewhere, whether you realize it or not. You need to track down where that conversion is happening and ensure it has the correct encoding.

Your question states, "When I explicitly set the encoding at each stage of the process", so you will need to know how it's encoded in the database. If that doesn't make sense, read on.

It's also possible that you are simply trying to encrypt a file that you're getting out of the database, and you don't care about the string representation; you want to treat it as plain bytes, not as text. In that case, BasicTextEncrypter ("Utility class for easily performing normal-strength encryption of texts.") is not a good fit for this task. It encrypts strings. The BasicBinaryEncryptor ("Utility class for easily performing normal-strength encryption of binaries (byte arrays).") is what you need.

Corrodias
  • 734
  • 6
  • 10