Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 28288
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic