Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
J Marshall Presnell
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20959
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic