Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Python中创建的字符串中读取Java中的RSA私钥?_Java_Python_Encryption_Rsa - Fatal编程技术网

如何从Python中创建的字符串中读取Java中的RSA私钥?

如何从Python中创建的字符串中读取Java中的RSA私钥?,java,python,encryption,rsa,Java,Python,Encryption,Rsa,我在python应用程序中生成了RSA公钥和私钥。我想用python加密一些字符串,将其发送到java应用程序并解密。怎么做?我尝试了这里介绍的几种方法,但都无法奏效: 这是我的python代码: from Crypto.PublicKey import RSA new_key = RSA.generate(2048, e=65537) public_key = new_key.publickey().exportKey("PEM") private_key = new_key.ex

我在python应用程序中生成了RSA公钥和私钥。我想用python加密一些字符串,将其发送到java应用程序并解密。怎么做?我尝试了这里介绍的几种方法,但都无法奏效:

这是我的python代码:

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
python中的解密工作:

def decrypt(string, private_key):
  raw_cipher_data = base64.b64decode(string)
  string  = private_key.decrypt(raw_cipher_data)
  string = string.decode("latin1")
  return string
我在其中创建私钥对象,如下所示:

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

“我尝试了本文中描述的几种方法,但无法使其发挥作用”并不是对问题的有意义的陈述。@JamesKPolk,例如,