2

Right I've a small problem. I'm using a Javascript library (jsencrypt) to encrypt a message in a browser. This message is then sent to the backend where it is decrypted using a Java library (bouncycastle). My problem is although I can encrypt and decrypt messages using both libraries they don't seem to want to work together. So when I encrypt my message in a browser and send it to the backend I end up getting garbled gibberish. Does anyone have any idea what's going on here?

JSENCRYPT

var text = "This is another msg!";
var pubkey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwyTZf5gRWJdEevtK7sJSz14lhs1Jw7+aFhGtr4cbDGxdiXH8J+BwuYmBc6QFMhRw7AeYcgkx9zPb3SICzr+oK17RMA6T66dH+GPXp75LFUmfONfk2JdSeO80mMODGctSuefWDvoQ24Cq0Bz+ysrhP7hRqvJso5a0GMNPwt8ErtWfz4HZjSsaaZ7gXga2h5dq1OTcGNfevkDN9CJtFW/0Wwb/F6cnXngVHE41rsN4POUB3IWcX2CrCGxSraa+xsT/P7AJ8HRJ4wcjl9G2K/rlHJ8ZXZKlIuWwEzx0/F0IjE+S93tLpDgt6YJxjWqYqjL2uuJAGmEU323+PWA3jFTC+QIDAQAB";        

var encrypt = new JSEncrypt();
encrypt.setPublicKey(pubkey);
var ciphertext = encrypt.encrypt(text);
console.log("ciphertext  : " + ciphertext);

var decrypt = new JSEncrypt();
decrypt.setPrivateKey($("#privkey").val());
var plaintext = decrypt.decrypt(ciphertext);
console.log("plaintext  : " + plaintext);

BOUNCYCASTLE

String cipherText = "jQ/I+oyyIfG5ARIHZsa6MfxwHciCt+3p6l+bLh4NPinq2s8eDjbO9O8abhVt2xuBQQcPAIaqbiP3Y3vRFYLOD2O+inKWiL1SpSBxvUb0XlWMgLmOqWUL6w6sL2iEla3i5EbdlrkK0uLA7QOUc6/fGVyLVe8VL7Vv4BGlo/cxR2FN74HK4MtLFRNaLKejwD6WbCNQoz4sIMA/Ez8GRSVEMyeYVZoWELShvyIRCqVADboAeuEP5l+oFlzgQfW6HFdpPnX+9TnHrbezdWhXiuJiD1Mq4VTicsya50MNcXJuPDV7NINYZs72UCS8NTYvfVkFc2lO7EUlDvvJ7Ns4wWuuWQ==";

PemReader pemReader = new PemReader(new InputStreamReader(new FileInputStream("priv.pem")));
PemObject pemObject = null;

try 
{
    pemObject = pemReader.readPemObject();

} finally {

    pemReader.close();
}

PrivateKey privateKey = EncryptionUtil.generatePrivateKey(pemObject.getContent());
byte[] plainText = EncryptionUtil.asymDecrypt(privateKey, cipherText.getBytes());
System.out.println(new String(plainText));
  • Have you tried encrypting on both front & back, and seeing if the ciphertext matches? – Caleb O'Leary Apr 18 '16 at 14:31
  • What is the expected result, should the java example decrypt `cypherText` into `This is another msg!`? – stjepano Apr 18 '16 at 14:33
  • @stjepano yes, that is what I am hoping to do. –  Apr 18 '16 at 14:45
  • 1
    What is the key size of your RSA key-pair? What problem are you trying to solve? Are you planning on encrypting text with RSA? Because the length of a text to encrypt with RSA is very limited. This is why you should use a hybrid solution between asymmetric and symmetric encryption to encrypt larger texts. If you are trying to re-invent TLS, I would not recommend this. – ST2OD Apr 18 '16 at 14:53
  • @st2erw2od key size is 2048, all keys are generated using bouncycastle. I'm trying to solve the problem of interoperability between jsencrypt and bouncycastle. I'm encrypting the text above as a proof of concept before moving to a hybrid solution. Any ideas on a solution? –  Apr 18 '16 at 15:02
  • 2
    You are properly getting different default padding modes. Keywords like RSA-OAEP padding & PKCS#1 padding comes to mind .. – Ebbe M. Pedersen Apr 18 '16 at 15:51
  • @EbbeM.Pedersen Genuis! That was it! Thanks a million! –  Apr 18 '16 at 16:38

1 Answers1

1

@EbbeM.Pedersen

You are properly getting different default padding modes. Keywords like RSA-OAEP padding & PKCS#1 padding comes to mind.

This was indeed the issue. I changed the default padding in bouncycastle to PKCS#1 and it all works now.

Thanks a million.