File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Java Products and Servers and the fly likes String encrypt in Java, decrypt in Javascript with pre-shared key (AES256) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Other Java Products and Servers
Bookmark "String encrypt in Java, decrypt in Javascript with pre-shared key (AES256)" Watch "String encrypt in Java, decrypt in Javascript with pre-shared key (AES256)" New topic
Author

String encrypt in Java, decrypt in Javascript with pre-shared key (AES256)

J Marshall Presnell
Greenhorn

Joined: Jun 01, 2011
Posts: 2
I have a application requirement to encrypt a string in Java on a portable device (such as Android), then have that output ciphertext decryptable on a client-side browser using Javascript. The encryption key is pre-shared.

The primary reason for this is to preserve some purely private data - even from the servers that house it on the web within a database. Think of it as a privacy preserving data transport and storage mechanism.

I've found many implementations of AES256 in Java and Javascript, but they seem to not interoperate well. I've found JS Encrypt / Java Decrypt pairs, but that's backwards from what I am looking for. My thought on this is simply that I am borking some initialization/config in the Java implementation, but I've yet for find the magic combination.

The JS reference implementation I am using is at http://www.movable-type.co.uk/scripts/aes.html - I have yet to get any Java implementation to work against it successfully.

Additional note: The byte array is base64 encoded before transmission and decoded in the browser before the attempt to decrypt. I've manually verified that the bytes are unchanged during transmission.

Any help on this would be appreciated. Even vague generalities would be helpful to reduce the problem scope

Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Here's a shot in the dark: Encrypting a string produces an array of bytes, which you subsequently decrypt into (hopefully) the original string.

So you have to make sure that you transmit that array of bytes as an array of bytes. Don't allow it to be converted into a String at any time, and particularly not by Java. Converting bytes to a String in Java only works successfully if those bytes represented text in the charset you used in the conversion. It doesn't work on arbitrary binary data.
J Marshall Presnell
Greenhorn

Joined: Jun 01, 2011
Posts: 2
Nice shot in the dark there

But I do base64 encode before transmission and decode before decryption. Sorry, I should have mentioned that in the original post
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

If you look at that AES Javascript Aes.Ctr.encrypt() method you will see that it uses an algorithm for converting the password into a valid AES key. You will need to emulate this to stand any chance of getting the equivalent Java functionality. The comments in the code define the appropriate reference material.




Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String encrypt in Java, decrypt in Javascript with pre-shared key (AES256)
 
Similar Threads
DSA Encryption In Javascript & Java
File Transfer and Security
Data encription
AES256, cryptoPerms and Unlimited Cryptography
Encryption of long strings