Borg Lin
Borg Lin

Reputation: 133

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?

Upvotes: 1

Views: 2195

Answers (2)

Rain
Rain

Reputation: 601

Don't focus on comparing results, It will not same, it's different every time you run encrypt. Modify like this, it's should work event if the result not the same,

print base64.b64encode(cipher.encrypt(b'123')).decode()

Upvotes: 0

Ronie Martinez
Ronie Martinez

Reputation: 1275

Try running JSEncrypt twice, are the outputs the same?

In theory, the outputs should be different every time.

https://stackoverflow.com/a/16329374

Upvotes: 1

Related Questions