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)
Community
  • 1
  • 1
user3626048
  • 706
  • 4
  • 19
  • 52
  • "I've tried few methods described in here but I can't make it work" is *not* a meaningful statement of the problem. – President James K. Polk Nov 03 '15 at 01:20
  • @JamesKPolk, e.g. http://stackoverflow.com/a/19387517/3626048, http://stackoverflow.com/questions/7216969/getting-rsa-private-key-from-pem-base64-encoded-private-key-file, http://stackoverflow.com/questions/22900570/key-from-string-in-java-rsa – user3626048 Nov 03 '15 at 21:19

0 Answers0