0

I used the following code to decrypt an encrypted String

public String decrypt(String decryptMe) throws Exception {
    if (decryptMe == null) {
        return null;
    } else {
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(2, this.getKeySpec());
        return new String(cipher.doFinal((new BASE64Decoder()).decodeBuffer(decryptMe)));
    }
}

While I run the code locally and deployed it on my local tomcat server, it displays the correct French characters (i.e. ÉPERVIÈRES). But when I deploy the code on tc-server running in Linux box, it doesn't display the correct characters (??PERVI??RES). Is there any problem with my decrypt method?

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
Yoseph
  • 15
  • 1
  • 7

1 Answers1

3

The problem is probably here:

return new String(cipher.doFinal((new BASE64Decoder()).decodeBuffer(decryptMe)));

new String(byte[] bytes) uses the platform's default charset and should generally be avoided. Instead, use new String(byte[] bytes, Charset charset) and specify the encoding of your encrypted data. For example, if the data is UTF-8:

return new String(cipher.doFinal((new BASE64Decoder()).decodeBuffer(decryptMe)), StandardCharsets.UTF_8);

You can find out the default charset on a platform by calling Charset.defaultCharset(), so check it on your local server and use that charset in the code.

It could also be an issue of your terminal not displaying the Unicode output properly, but that depends on which terminal software you're using. Check the manual for Unicode display options.

Sean Van Gorder
  • 3,393
  • 26
  • 26
  • 1
    Indeed. Best use UTF-8 as that can handle all scripts. – Joop Eggen Mar 26 '18 at 20:15
  • @JoopEggen It needs to match the encoding of the bytes before they were encrypted, but yeah, if your code handles both sides then you should set both to use UTF-8, using [String.getBytes(Charset charset)](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#getBytes(java.nio.charset.Charset)) for the encryption. – Sean Van Gorder Mar 26 '18 at 20:25
  • @SeanVanGorder - I logged the default Charset value in my local and also tested it on my Linux server. The values are deferent. In my local it is >> windows-1252 but in the Linux box it is >> US-ASCII. – Yoseph Mar 26 '18 at 20:33
  • @Yoseph US-ASCII as default is kind of strange, it only includes the basic alphabet/numbers/symbols, not the accented characters you need. If the OS is set to US-ASCII somehow you might need to change it. – Sean Van Gorder Mar 26 '18 at 22:49
  • @SeanVanGorder I don't have that much experience on Unix/Linux. Can you please tell me how I can change the default value? – Yoseph Mar 27 '18 at 13:18
  • @Yoseph That depends on your system. Try https://stackoverflow.com/q/5306153/2643425 or https://serverfault.com/q/236391 . Set everything to UTF-8 if possible. – Sean Van Gorder Mar 27 '18 at 15:39
  • @SeanVanGorder Thank you for your help. Your solution worked for me. – Yoseph Mar 27 '18 at 19:14