Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.westnet.com.au!news.westnet.com.au.POSTED!not-for-mail NNTP-Posting-Date: Mon, 29 Aug 2011 01:12:44 -0500 From: "Qu0ll" Newsgroups: comp.lang.java.programmer Subject: Using encryption with special Unicode characters Date: Mon, 29 Aug 2011 16:11:13 +1000 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal Importance: Normal X-Newsreader: Microsoft Windows Live Mail 15.4.3538.513 X-MimeOLE: Produced By Microsoft MimeOLE V15.4.3538.513 Message-ID: Lines: 84 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 124.149.48.246 X-Trace: sv3-Wkv5qt24OpWHHdPM67b+TG4epqoz7cw4EXm/wAacvZQyq0T0h8KBnTecxgbBgYP9tAV0NQCKmLV98P9!FxaT78XKqzbbWTnntTxPng19yCqinS4Y1APkBId90vp4Ln2/tMF0BxdUPgB5WL50/NdfD96UFjM3!sUYpf1xvlkY1aTOCWbShPw== X-Complaints-To: abuse@westnet.com.au X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3802 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7454 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)); } } -- And loving it, -Qu0ll (Rare, not extinct) _________________________________________________ Qu0llSixFour@gmail.com [Replace the "SixFour" with numbers to email me]