pythonencryptionrsajsencrypt

python RSA encrypt differs from JSEncrypt?


I am writing a crawler using python. The website uses JSEncrypt to encrypt password. The JS code is as follows:

var pubkey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDB3Xn/+WP5OVbA8hCj3zuTlqAJ+CVt5UPHi1TYTbsBzEZ0W0+tgn9YElA9hNIi5ElFTicpGCYW4w+B2zmniSLKy3sqItT1wNCt6zGJ7lkUCkhLSIY4mp9Tqs8hn01/3HNnqDRBPhFcmA99Vy+SOoTUvCOUMiGp4ENruyxkvEp5vwIDAQAB"
var encrypt = new JSEncrypt();
encrypt.setKey(pubkey)
encrypt.encrypt("123")

And my python codes are:

from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
import base64
pubkey = '''-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDB3Xn/+WP5OVbA8hCj3zuTlqAJ
+CVt5UPHi1TYTbsBzEZ0W0+tgn9YElA9hNIi5ElFTicpGCYW4w+B2zmniSLKy3sq
ItT1wNCt6zGJ7lkUCkhLSIY4mp9Tqs8hn01/3HNnqDRBPhFcmA99Vy+SOoTUvCOU
MiGp4ENruyxkvEp5vwIDAQAB
-----END PUBLIC KEY-----'''
rsa_key = RSA.importKey(pubkey)
cipher = PKCS1_v1_5.new(rsa_key)
print base64.b64encode(cipher.encrypt("123"))

But their results are different. Also, I found that these codes produce different encryption output each time called. With the same key and plaintext, shouldn't rsa algorithm return the same output?


Solution

  • Try running JSEncrypt twice, are the outputs the same?

    In theory, the outputs should be different every time.

    https://stackoverflow.com/a/16329374