5

I encrypt plain text from JS RSA Library(http://www-cs-students.stanford.edu/~tjw/jsbn/) and decrypt from python, Crypto.PublicKey.

But, when I decrypt cipher text from JS with python, It has dummy text. I don't know why they are.

So, I want clear text which does not have dummy.(ex. test)

Step

  1. Make key pairs in Python.

    key = RSA.generate(1024) #(publicKey, privateKey)
    
  2. Save modulus, exponent in JS and Encrypt "test"

    var rsa = new RSAKey();
    rsa.setPublic(modulus, exponent); //modulus and exponent hex string
    rsa.encrypt("test");
    

    result(hex string): d0ab7e22f92adcca7182e3c622b513382d163033df5ca0f3c0327e8a1774258800ae57dfc98522f5ed40a4bed2f4b54f46ea800ff1ef522b104b0f874a598f6bbcf5453506f8bf2f8aa3b04b0c73f0018564707304b3a059326d51945d3ff0282d63c2c4c1ea6ba5a2172af83ef8bdc1d104a8d67ba95ee97ab89b36cd5c34d4

  3. Decrypt using PrivateKey in Python

    key.decrypt(above_result.decode('hex'))
    

    result: \x02\xf0\xae\xafK\xd3\x17\xfc\xf4\xd6\xd9=\xee7\x04\x94\xea\x9c\xd8\xf7--\x19\x05$!'#\xad\x82'\xfcKG\xadK\xb6_\xabMZ:\x9dU\xa4\xc0[\x8c\xa6hC\x93\xf7\xbc\xf1:\x9f\x107D\xe8\xfe\x07R\x8c\xd7\xb3\xe6\xc6\xcf^\x92\xa2\xe2X\xe4\xaf|\x8aS\xfe\xd3\x84)\xc3\x82\xdc\xd1\x7f\xc9\x12\xd0\x94\xd2jS\xee\x83\xfda\xc6\xc7d\xdd\x0b2\xe6\x1d\x84\x0c\x93\x8aK\xc2\x10U\xc0Y~\xbf\x15\xfa\x00test

2
  • what are you expecting to get? Mar 1, 2015 at 11:32
  • @ArtjomB. exactly. I want clear decrypted text which does not have dummy
    – yumere
    Mar 1, 2015 at 12:31

2 Answers 2

4

The pycrypto rsa decrypt() method is a low-level method, hence you are getting the expected output, a PKCS#1, version 1.5, block type 2 array of bytes. As you seem to find that inconvenient, you should follow the advice in the API docs and instead use the PKCS1_v1_5 module.

0
0

I had been using JSEncrypt JavaScript file to achieve conversation between JavaScript and Python using pycrypto however during encryption in JavaScript it uses a random pad in the function pkcs1pad2 which needed to be removed and it started working. It had been a hack but it worked out. Following are the lines are commented out from function pkcs1pad2(s,n)

 while(n > 2) { // random non-zero pad
    x[0] = 0;
    while(x[0] == 0) rng.nextBytes(x);
    ba[--n] = x[0];
  }

  ba[--n] = 2;
  ba[--n] = 0;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.