Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSA Encrypt and Decrypt between JS and Python(pycrypto)

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

like image 663
yumere Avatar asked Sep 28 '22 15:09

yumere


1 Answers

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.

like image 98
President James K. Polk Avatar answered Oct 03 '22 10:10

President James K. Polk