1

I have generated RSA public and private keys in my python app. I want to encrypt some string in python, send it to java app and decrypt it. How to do that? I've tried few methods described in here but I can't make it work:

This is my python code:

from Crypto.PublicKey import RSA

new_key = RSA.generate(2048, e=65537)
public_key = new_key.publickey().exportKey("PEM")
private_key = new_key.exportKey("PEM")

def encrypt(string, public_key):
  encrypted = public_key.encrypt(bytes(string, "latin1"), 255)
  date = encrypted[0]
  date = base64.b64encode(date)
  return date

Decryption in python works:

def decrypt(string, private_key):
  raw_cipher_data = base64.b64decode(string)
  string  = private_key.decrypt(raw_cipher_data)
  string = string.decode("latin1")
  return string

where I create private_key object like this:

private_key_str = "-----BEGIN RSA PRIVATE KEY-----\nMIIE+IB [.....] bcy/VVp63YA==\n-----END RSA PRIVATE KEY-----"
private_key = RSA.importKey(private_key_str)
2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.