2017-07-27 7 views
-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 
+3

私は気にしませんでしたが、あなたの秘密鍵をstackoverflowに貼り付けましたか? –

+2

あなたはそれがJavaだと思いますか? – user2357112

+0

秘密鍵を無効にして新しい鍵で置き換えてください。 – poke

答えて

0

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に使用されるクラス/オブジェクトについてもう少し詳しく説明します。

関連する問題