Re: Using encryption with special Unicode characters

Discussion in 'Java' started by Mayeul, Aug 29, 2011.

  1. Mayeul

    Mayeul Guest

    On 29/08/2011 08:11, Qu0ll wrote:
    > This is my first go at using Java encryption. I have a requirement to
    > encrypt and then later decrypt a series of strings that may contain
    > special Unicode characters such as "\u25bc". The code below correctly
    > encrypts and decrypts "normal" ASCII strings but turns characters like
    > "\u25bc" into '?' when it decrypts (or maybe even when it encrypts).
    >
    > It doesn't really matter which encryption algorithm I use as long as it
    > is reasonably secure (I chose AES) but the encryption/decryption process
    > needs to handle these special characters.
    >
    > The output from the following code is:
    >
    > Before char(0): 9660
    > After char(0): 63
    > Equal: false
    >
    > How can I get this to work? Here is the code:
    >
    > import javax.crypto.Cipher;
    > import javax.crypto.spec.SecretKeySpec;
    >
    > public class Encryption {
    >
    > private static final String ALGORITHM = "AES";
    >
    > private static final String KEY = "0123456789ABCDEF";
    >
    > private static final SecretKeySpec KEY_SPEC = new
    > SecretKeySpec(KEY.getBytes(), ALGORITHM);
    >
    > private static Cipher cipherEncrypt;
    >
    > private static Cipher cipherDecrypt;
    >
    > static {
    > try {
    > cipherEncrypt = Cipher.getInstance(ALGORITHM);
    > cipherEncrypt.init(Cipher.ENCRYPT_MODE, KEY_SPEC);
    > cipherDecrypt = Cipher.getInstance(ALGORITHM);
    > cipherDecrypt.init(Cipher.DECRYPT_MODE, KEY_SPEC);
    > } catch (final Exception e) {
    > e.printStackTrace();
    > }
    > }
    >
    > public static String decrypt(final byte[] raw) {
    > String result = null;
    > try {
    > result = new String(cipherDecrypt.doFinal(raw));
    > } catch (final Exception e) {
    > e.printStackTrace();
    > }
    >
    > return result;
    > }
    >
    > public static byte[] encrypt(final String raw) {
    > byte[] result = null;
    > try {
    > result = cipherEncrypt.doFinal(raw.getBytes());
    > } catch (final Exception e) {
    > e.printStackTrace();
    > }
    >
    > return result;
    > }
    >
    > public static void main(final String[] args) {
    > final String before = "\u25bc ABC";
    > System.out.println("Before char(0): " + (int)before.charAt(0));
    > final String after = decrypt(encrypt(before));
    > System.out.println("After char(0): " + (int)after.charAt(0));
    > System.out.println("Equal: " + before.equals(after));
    > }
    > }


    String.getBytes() and String(byte[]), converting String to byte array
    and backwise, is the job of a character encoding, which, in Java, are
    called 'charsets'. If you do not specify which charset you want to use,
    they will use your default charset, which depends on your environment.

    This charset is not guaranteed to support Unicode. In fact, in western
    environments it is rather likely to be iso-8859-1 or likewise, which
    does not support Unicode.

    Which is why you're better off forcing the use of a Unicode-compliant
    charset, like utf-8. utf-8 and the utf-16s are guaranteed to be
    supported by Java, which makes them safe choices.

    --
    Mayeul
     
    Mayeul, Aug 29, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Mueller
    Replies:
    3
    Views:
    33,140
    Stefan Mueller
    Jul 23, 2006
  2. Replies:
    2
    Views:
    1,114
    Ingo Menger
    May 31, 2007
  3. rvino
    Replies:
    0
    Views:
    4,680
    rvino
    Aug 14, 2007
  4. Grzegorz ¦liwiñski
    Replies:
    2
    Views:
    986
    Grzegorz ¦liwiñski
    Jan 19, 2011
  5. Roedy Green
    Replies:
    0
    Views:
    529
    Roedy Green
    Aug 29, 2011
Loading...

Share This Page