Adrian
Adrian

Reputation: 23

String that was RSA encrypted using Java contains extra encrypted characters when decrypted using Python

I have been provided with data that was RSA encrypted by a Java application and the RSA public key in DER format. I wrote a simple Python script, using pycrypto , to decrypt the data however the output contains what seems to be encrypted text as well as what I am looking for. This is quite untidy and not suitable for further processing.

Original String, and my expected output is something like:

elem:1234567890:0987654321

The owners of the application that encrypted the data told me to

  1. Decode the string from Base64
  2. Decrypt the string using the private key they supplied me with (DER format)

My Python script looks something like this:

from Crypto.PublicKey import RSA
import base64
import sys
import ast
def decode_rsa(encryptedString, key_path):
    key = RSA.importKey(open(key_path).read())
    deocdedString = base64.b64decode(encryptedString)
    decrpytedString = key.decrypt(deocdedString)
    return decrpytedString

print decode_rsa(sys.argv[1], sys.argv[2])

But when I run the code I get:

python decryptRSA.py Hh6+rJdFA0SPWvbLU8gxbrZXTnYXv3M/XlSU2IHgfGvIMXckrJk/3w7OSjadhNeyIHqzfXNXRexn721lmCh7QZbGXB/cKzuEDr9pAZU6kbrc1BWDLkTuOC5e+vAcV21sebuYQUyWjGGkuMrTtXw9nlT0+h9/GAzFS7wVTFE859w= private_key.der

uB▒#▒▒▒{4elem:1234567890:0987654321&i▒_+▒▒,▒I%▒▒▒▒▒▒(d>

As you can see, what I need is there but there are other weird characters coming back too.

Upvotes: 2

Views: 478

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 93978

You are raw decrypting the RSA encrypted text. RSA requires padding to be secure. You need to use a padding mode - probably OAEP - as indicated in the documentation of PyCrypto.

Upvotes: 1

Related Questions