Java后端提供的公钥加密格式
miwen = "r6nxgVxAX7QmG1P1JINSGopEUc7J40dU-bEG9fnprEK3rBgrKVBeVPAfFWS57mhy2ljhh5flBwzbcAs5WCKd_Q=="
python执行解密报错
Traceback (most recent call last):
File "E:\galactica\app\micgenERP\operation\login\data\RSA_PKCS1_PADDING.py", line 153, in <module>
rsa_decrypt = documentRsa.rsa_decrypt(miwen)
File "E:\galactica\app\micgenERP\operation\login\data\RSA_PKCS1_PADDING.py", line 101, in rsa_decrypt
return self._rsa_decrypt(self.get_private_key(), msg)
File "E:\galactica\app\micgenERP\operation\login\data\RSA_PKCS1_PADDING.py", line 51, in _rsa_decrypt
return b''.join(priobj.decrypt(msg, b' '))
File "E:\galactica\venv\lib\site-packages\Crypto\Cipher\PKCS1_v1_5.py", line 174, in decrypt
raise ValueError("Ciphertext with incorrect length (not %d bytes)" % k)
ValueError: Ciphertext with incorrect length (not 64 bytes)
原因分析:
1、Java公钥加密生成的密文带有'-'和'_',需要替换成'+'和'/'
2、传入的参数的长度不是2的对象,在参数最后加上等于号"="(一个或者两个)
miwen = "r6nxgVxAX7QmG1P1JINSGopEUc7J40dU+bEG9fnprEK3rBgrKVBeVPAfFWS57mhy2ljhh5flBwzbcAs5WCKd/Q=="
python解密成功!
附上代码
import json
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.Pub