I have to pass some strings from a java servlet to a php script. What options are there for encrypting the strings? I’d need a method that is implemented for both java and php (like..a caesar cipher…). Is there any standard encryption method I should be able to get a library for both java and php?
I want to encrypt the strings on the java side, pass to the php script, then let the php script decrypt them.
I can’t use https due to the limitations of the provider I’m using.
Thanks
Hopefully this can get you started. Error handling is missing and the secret key is hard coded. Both of those would need to be addressed for production quality code. From the Java side you can use the Java Cryptography Architecture (JCA):
From the PHP side, you are going to need Mcrypt.
The function
pkcs5_unpadwas copied from here since it seems that PHP Mcrypt doesn’t include support for PKCS5 padding. The Java code prefixes the data with the initialization vector used to encrypt it. Subsequently the PHP code splits it into two, the initialization vector and the encrypted data.This code uses 128 bit AES (Rijndael) in CBC mode which should be secure enough for most uses. In addition to the simple encryption, I recommend using an HMAC as described here to ensure the data isn’t tampered with. To perform the HMAC in Java, use the Mac class. For PHP, see Mhash.