使用Python解码消息3
问题描述:
我试图解码用Java编码的消息。下面是代码从我们的Java开发人员的代码片段:使用Python解码消息3
$encrypted = base64_decode(urldecode($value));
$decrypted = "";
openssl_private_decrypt($encrypted, $decrypted, $key);
我试图解码使用Python与私有密钥字符串:
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
import base64
from urllib.parse import unquote
private_key="Private_key"
cipher = PKCS1_OAEP.new(private_key)
mesg='some mesg'
# For URL encoder
a=unquote(mesg)
encrypted=base64.b64decode(a.encode("utf-8"))
# before decrypt convert the hex string to byte_array
message = cipher.decrypt(bytearray.fromhex(encrypted))
print(message)
我收到以下错误,而我使用Python 3
TypeError: fromhex() argument must be str, not bytes
答
在Python 2.7上使用pyCrypto,这是您如何解密RSA公钥加密。在python 3中可能会略有不同,但我很确定它应该接近或相同。
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
key = RSA.importKey('PrivateKeyString', passphrase=None)
def decrypt(self, message):
return PKCS1_OAEP.new(key).decrypt(message)
print(decrypt('EncryptedMessage'))
欲了解更多信息,你应该阅读Documentation。
对于比我更讨厌的人,这里有更多关于用于this example的类/对象。
我不知道,但你只是把你的私钥粘贴到stackoverflow? –
是什么让你觉得这是Java? – user2357112
请确保使该私钥失效并将其替换为新私钥。 – poke