1 year ago
#63789
Peter
JSEncrypt into Python
I'm reverse engineering a website. In website's code,
C = n("720d")
w = new C["JSEncrypt"];
w.setPublicKey(k.privateKey);
k = {
privateKey: "30820122300d06092a864886f70d01010105000382010f003082010a0282010100f357429c22add0d547ee3e4e876f921a0114d1aaa2e6eeac6177a6a2e2565ce9593b78ea0ec1d8335a9f12356f08e99ea0c3455d849774d85f954ee68d63fc8d6526918210f28dc51aa333b0c4cdc6bf9b029d1c50b5aef5e626c9c8c9c16231c41eef530be91143627205bbbf99c2c261791d2df71e69fbc83cdc7e37c1b3df4ae71244a691c6d2a73eab7617c713e9c193484459f45adc6dd0cba1d54f1abef5b2c34dee43fc0c067ce1c140bc4f81b935c94b116cce404c5b438a0395906ff0133f5b1c6e3b2bb423c6c350376eb4939f44461164195acc51ef44a34d4100f6a837e3473e3ce2e16cedbe67ca48da301f64fc4240b878c9cc6b3d30c316b50203010001",
encrypt: function() {
var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : "";
return w.encrypt(e)
}
They defined w
and k
as above, and encrypt string with it like below.
k.encrypt("text goes here")
And encrypted output is below.
zrvsLtC+A+Ix8Dq/BoRqwRLaI72zOK0TgpJ7KdXEF6spBMYyv6boOzN0yhxA1cKCAImWsaFT7/JRTbBNj40b+qREkE7aNo9gymM4kHH/+u4oFm+2+vLZXBAWGApt9g585tpVLbwOa6ANE2CBLOt8MeZl4smteJq5rKMddRtDosodfdZWTTt8aTbJrgCgkFPzNyYmN+koxwsbZ1VW2MmLtCnIlEIZfDkOkU0WWzcOylLlpG6/Gs0LkRzsY8HoG4ttY0prb8JDlFcuSi+tdLptoSMABJlXZ0mZOAmcElye47QWlq3RlaxF8saJAjthJpsZABiwIRaVUt/X7i6Uunen9A==
I'm pretty sure that w
is just JSEncrypt
. Seems like when a string is given, It just encrypt string with JSEncrypt
which has k.privateKey
as public key.
I want to achieve the same thing with Python 3.
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5
from base64 import b64decode,b64encode
pubkey = "30820122300d06092a864886f70d01010105000382010f003082010a0282010100f357429c22add0d547ee3e4e876f921a0114d1aaa2e6eeac6177a6a2e2565ce9593b78ea0ec1d8335a9f12356f08e99ea0c3455d849774d85f954ee68d63fc8d6526918210f28dc51aa333b0c4cdc6bf9b029d1c50b5aef5e626c9c8c9c16231c41eef530be91143627205bbbf99c2c261791d2df71e69fbc83cdc7e37c1b3df4ae71244a691c6d2a73eab7617c713e9c193484459f45adc6dd0cba1d54f1abef5b2c34dee43fc0c067ce1c140bc4f81b935c94b116cce404c5b438a0395906ff0133f5b1c6e3b2bb423c6c350376eb4939f44461164195acc51ef44a34d4100f6a837e3473e3ce2e16cedbe67ca48da301f64fc4240b878c9cc6b3d30c316b50203010001"
msg = "text goes here"
keyDER = b64decode(pubkey)
keyPub = RSA.importKey(keyDER)
cipher = Cipher_PKCS1_v1_5.new(keyPub)
cipher_text = cipher.encrypt(msg.encode())
emsg = b64encode(cipher_text)
print(emsg)
I'm trying with PyCrypto library, but since the public key looks different from others, error occurs.
Traceback (most recent call last):
File "/Users/pi/Desktop/test/app.py", line 13, in <module>
keyPub = RSA.importKey(keyDER)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/Crypto/PublicKey/RSA.py", line 682, in importKey
raise ValueError("RSA key format is not supported")
ValueError: RSA key format is not supported
How can I achieve the same thing what JSEncrypt does with Python?
javascript
python
cryptography
rsa
jsencrypt
0 Answers
Your Answer