在python中需要PyCrypto的帮助。
我试图解密我从node.js app获得的文本。
这就是我在nodejs中解密密钥的方法
key.decrypt(<encrypted_text`, ‘base64’, ‘utf8’, ursa.RSA_PKCS1_PADDING)
但是,当我尝试使用python PyCrypto
模块解密相同的文本时,我收到错误。
以下是更多细节:我正在尝试使用以下代码在python中解密它:
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA
from Crypto.Hash import SHA
from Crypto import Random
from base64 import b64decode
key = RSA.importKey(open('cert.key').read())
sentinel = Random.new().read(15+dsize) # Let's assume that average data length is 15
text = b64decode(<encrypted_text>)
cipher = PKCS1_v1_5.new(key)
message = cipher.decrypt(text, sentinel)
print 'message =====>', message`
我收到此错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-180-a1b00798e108> in <module>()
19
20 cipher = PKCS1_v1_5.new(key)
---> 21 message = cipher.decrypt(text, sentinel)
22 print 'message =====>', message, '\n'
23 digest = SHA.new(message[:-dsize]).digest()
/Users/skapil/.virtualenvs/dave/lib/python2.7/site-packages/Crypto/Cipher/PKCS1_v1_5.pyc in decrypt(self, ct, sentinel)
202 # Step 1
203 if len(ct) != k:
--> 204 raise ValueError("Ciphertext with incorrect length.")
205 # Step 2a (O2SIP), 2b (RSADP), and part of 2c (I2OSP)
206 m = self._key.decrypt(ct)
ValueError: Ciphertext with incorrect length.