We are dealing with a 3rd party who run a Java API. They deal in online banking/finance etc, and one requirement from their side is that sensitive data is encrypted in the browser prior to being sent to our server. (From our server, the data is sent on to theirs, untouched aside from very basic validation). We have no way to change this requirement.
Our client is an HTML5 website that needs to be able to encrypt certain sensitive submitted form data while leaving other parts as plain text (so no to JCryption)
I've read up many S.O. threads where people have ideas but no solutions.
As a starting point, we have the Java that will decrypt the incoming JS string, using the BouncyCastle jar:
/**
* Decrypt text using private key.
*
* @param toBeDecrypted encrypted text
* @param key the private key
* @return the unencrypted value
*/
public String decrypt(String toBeDecrypted, PrivateKey key) throws GeneralSecurityException {
// Local variables
byte[] dectyptedText;
final Cipher cipher;
dectyptedText = null;
// get an RSA cipher object and print the provider
cipher = Cipher.getInstance(ALGORITHM_CIPHER);
// decrypt the text using the private key
cipher.init(Cipher.DECRYPT_MODE, key);
dectyptedText = cipher.doFinal(Base64.decodeBase64(toBeDecrypted.getBytes()));
return new String(dectyptedText);
}
I've tried multiple JavaScript libraries with poor results (using/downloading their demo pages) swapping in my own public/private PEM files, all of the following libraries can encrypt and decrypt strings, but those encrypted strings are not decrypted by the above Java code.
I've tried:
- http://www.ohdave.com/rsa/
- http://travistidwell.com/jsencrypt/
- http://www-cs-students.stanford.edu/~tjw/jsbn/
- http://ats.oka.nu/titaniumcore/js/crypto/readme.txt
all without success.
Can anyone point me to a working example of JS encryption being decrypted by Java?
(as an aside, yes, our team knows about JS's unsuitablity for encryption. However this is one more step in a complex security setup, even if it does amount to only a small step)