problem description
when using Python
to access the server written by Java
, the resources returned by the server are encrypted by RSA
, and I have the RSA
private key string generated in Java
. But in the decryption, there is a problem.
later learned (looking at the Java code) that Java
uses the private key generated by PKCS-sharp8
, while Python
has only PKCS-sharp1
, which makes it impossible to decrypt.
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCF7hfCivkkln8OC2H3x2Imn/ynFj04ospoiwHAGD4
the environmental background of the problems and what methods you have tried
later, by querying on the Internet, I convert PKCS-sharp8
of Java
into PKCS-sharp1
, but it still doesn"t work in Python
(rsa library). The code is shown below
related codes
import rsa
import base64
response = {
"encryptkey": "LwHgWbrcpRxDmutLqV+eKjUFV4DBXZZbD8hSQxCZ7ZG33lhs/s7otKOu3ohREYzBTIV6jNMIfA6S/M2+ZwvwxgOU591bmJQywGlfueGbtEUW1KSe3C2l0X8H7d2KXL5s4fESWy8LBbkBUcbtIm1sRLFiHqFGRe2mmBtnI51sBHU="
}
-sharp Java
pck8_str = """
-----BEGIN PRIVATE KEY-----
MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIpB2tBWTUFVJbsb
eeuL2lt+S5zht5fsZRtxGVU15LwF9uhrZVZYtKkYaRtuN4XPPn7d0r03Z4w4cGE0
qtaV/vVIwWh7cmf5JRls6useBZpxaKoGLfT2rzf1k2A2qcT+T5qM39KfPPeYfJY/
unMpv2vKAQK6LldmGXiS9TCRa2e/AgMBAAECgYAoC2b7TOPXdngVN6IajUlTksMH
z4DDqh6m0h1KRu/XXcrkCqcxWUQKbh2Talr+kMZDLLCrSW7eeEJkhUKuIIfVyW+0
H14nahTtLeVe5hwQ9mqpG828rKso7rCEhErtTFw9tnmEy6v8X3a6w3JfmHWQ2SBh
-----END PRIVATE KEY-----
"""
-sharp
pck1_str = """
-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCKQdrQVk1BVSW7G3nri9pbfkuc4beX7GUbcRlVNeS8Bfboa2VW
WLSpGGkbbjeFzz5+3dK9N2eMOHBhNKrWlf71SMFoe3Jn+SUZbOrrHgWacWiqBi30
9q839ZNgNqnE/k+ajN/Snzz3mHyWP7pzKb9rygECui5XZhl4kvUwkWtnvwIDAQAB
AoGAKAtm+0zj13Z4FTeiGo1JU5LDB8+Aw6oeptIdSkbv113K5AqnMVlECm4dk2pa
/pDGQyywq0lu3nhCZIVCriCH1clvtB9eJ2oU7S3lXuYcEPZqqRvNvKyrKO6whIRK
7UxcPbZ5hMur/F92usNyX5h1kNkgYRSxGctFDOZkui+wpQECQQDaBHOvV2cJdTTW
-----END RSA PRIVATE KEY-----
"""
encryptkey = response.get("encryptkey").encode("utf-8")
private_key = rsa.PrivateKey.load_pkcs1(pck1_str.encode())
data = rsa.decrypt(encryptkey, private_key)
error message actually seen
Traceback (most recent call last):
File "F:/response.py", line 68, in <module>
data = rsa.decrypt(encryptkey, private_key)
File "D:\Program Files\Python3\lib\site-packages\rsa\pkcs1.py", line 238, in decrypt
raise DecryptionError("Decryption failed")
rsa.pkcs1.DecryptionError: Decryption failed