python - Decode text encoded with RSA -


i have rsa public/private key pair , passphrase. trying decode text encrypted using using above key(s). encoded text 512 chars long alpha-num string.

i have tried using code provided @ sof question decrypt using rsa public key pycrypto

first used private key encoded aes-256-cbc pem file. start of privkey.pem made me think aes-256 encrypted

-----begin rsa private key----- proc-type: 4,encrypted dek-info: aes-256-cbc <rest of data> -----end rsa private key----- 

but received following error message.

valueerror: pem encryption format not supported. 

so asked source private key without aes encryption gave me. using key decrypted works , decrypted text looks below (i showing of text)

b'\x93\n(\x92\x02\x9af*?\x18"\x19\x12gn\xc2\<rest of text>' 

this not plain text. doing wrong? can me decode text.

edit 1:

based on maarten's answer below, have tried following code still getting errors.

here code decryption

from crypto.cipher import pkcs1_oaep crypto.publickey import rsa import ast  encrypted_text = "39085fc25e<hidden>2fcce845760391ff"  key = rsa.importkey(open("\\path_to_key\\private.der", encoding="utf8").read()) cipher = pkcs1_oaep.new(key)  message = cipher.decrypt(ast.literal_eval(str(uid))) 

and error:

unicodedecodeerror: 'utf-8' codec can't decode byte 0x82 in position 1: invalid start byte 

note had convert private key pem der using code below becasue using pem file getting syntaxerror: unexpected eof while parsing

openssl rsa -in private_key.pem -out private_key.der -outform der 

becasue

here solution have found.

first of using pycryptodome librray instead of pycrypto.

below encode , decode functions.

from crypto.cipher import pkcs1_oaep crypto.publickey import rsa  def encode_rsa(message, key_path):         key = rsa.importkey(open(key_path).read())     cipher = pkcs1_oaep.new(key)     ciphertext = cipher.encrypt(message)     return ciphertext  def decode_rsa(ciphertext, key_path):     key = rsa.importkey(open(key_path).read())     cipher = pkcs1_oaep.new(key)     # before decrypt convert hex string byte_array      message = cipher.decrypt(bytearray.fromhex(ciphertext))     return message 

using above 2 functions able encode/deode data correctly.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -