1

I tried to do RSA encryption in javascript and decryption in java. I referred this as example (#2 post)

http://www.wenda.io/questions/5025740/encrypt-a-small-string-with-rsa-in-javascript-then-decrypt-in-java-on-server.html

KeyPairGenerator kpg;

try {
    kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(2048);
    KeyPair kp = kpg.genKeyPair();
    yourVariablePublic = kp.getPublic();
    yourVariablePublic = kp.getPrivate();
} catch(NoSuchAlgorithmException e) {

}

Now let`s move to java code of our current page:

// receiving public key from where you store it
Key publicKey = YourCarrierClass.getYourVariablePublic();
KeyFactory fact;
// initializing public key variable
RSAPublicKeySpec pub = new RSAPublicKeySpec(BigInteger.ZERO, BigInteger.ZERO);
try {
    fact = KeyFactory.getInstance("RSA");
    pub = fact.getKeySpec(publicKey,    RSAPublicKeySpec.class);
} catch(NoSuchAlgorithmException e1) {
} catch(InvalidKeySpecException e) {
}

// now you should pass Modulus string onto your html(jsp) in such way
String htmlUsedModulus = pub.getModulus().toString(16);
// send somehow this String to page, so javascript can use it

And to decrypt it in java code:

 Key privateKey = YourCarrierClass.getYourVariablePrivate();
 Cipher cipher;
 BigInteger passwordInt = new BigInteger(ajaxSentPassword, 16);
 byte[] dectyptedText = new byte[1];
 try {
   cipher = javax.crypto.Cipher.getInstance("RSA/ECB/PKCS1Padding");
   byte[] passwordBytes = passwordInt.toByteArray();
   cipher.init(Cipher.DECRYPT_MODE, privateKey);
   dectyptedText = cipher.doFinal(passwordBytes);
   } catch(NoSuchAlgorithmException e) {
   } catch(NoSuchPaddingException e) { 
   } catch(InvalidKeyException e) {
   } catch(IllegalBlockSizeException e) {
   } catch(BadPaddingException e) {
   }
   String passwordNew = new String(dectyptedText);
   System.out.println("Password new " + passwordNew);

Like in example, I used the following code in javascript

function sendPassword() {
    var password = $('#passwordField').val();
    var rsa = new RSAKey();
    rsa.setPublic($('#keyModulus').text(), '10001');
    var res = rsa.encrypt(password);
    $('#ajaxSentPassword').val(res);
}

I've altered the keypair generation part with Get method of servlet and stored the value to be passed to jsp in session. And changed the decryption part to POST method of servlet. I do get those keys for decryption by retrieving from session. This is just for my learning and I do realise that it'll be vulnerable if implemented in real time. This is to learn from the basics for me.

The problem is, in javascript code, it doesn't recognize the RSAkey(), I got as "Uncaught reference error: RSAKey() is not defined". Does anyone know what's the .js file used for that example. I tried jsencrypt.js, which shows as "Uncaught reference error: RSAKey() is not defined", and if I use rsa.js file - I got Invalid RSA Public key error. It's not stated which .js file is used by him.

It can also be found here (2nd answer)

Encrypt a small string with RSA in javascript then decrypt in java on server

Community
  • 1
  • 1
The Coder
  • 2,562
  • 5
  • 33
  • 62
  • But you already found an alternative in the jsencrypt library so follow its documentation and write proper relevant code rather than relying on a vague stackoverflow thread where no answer is actually accepted. – Gimby Jan 27 '15 at 10:15
  • 1) Why PKCS#1v1.5 padding? It's weak. 2) Why bother at all? TLS already encrypts the password in transit. And without TLS an attacker can simply inject additional javascript that leaks the password in plain. – CodesInChaos Jan 27 '15 at 10:56
  • @CodesInChaos jsbn and jsencrypt don't seem to implement OAEP. – Artjom B. Jan 27 '15 at 10:58

1 Answers1

0

If you use jsencrypt, then you need to use its API:

 var encrypt = new JSEncrypt();
 encrypt.setPublicKey($('#pubkey').val());
 var encrypted = encrypt.encrypt($('#input').val());

Although it uses the jsbn library, I don't think it is exposed. So, RSAKey will not be there. If you want to use jsbn directly then you will have a reference to RSAKey.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222