开发者

python RSA module how to use java decrypt

开发者 https://www.devze.com 2023-01-19 01:01 出处:网络
I use python rsa module(http://stuvel.eu/rsa) get private_key and public_key. How can I use these private_key and public_ke开发者_JAVA百科y to encrypt or decrypt in java?Thank you all. I think I have

I use python rsa module(http://stuvel.eu/rsa) get private_key and public_key.

How can I use these private_key and public_ke开发者_JAVA百科y to encrypt or decrypt in java?


Thank you all. I think I have got the method. The python's Rsa module can generate (n,p,q,e,d).I can use follow method in Java

KeyFactory s=KeyFactory.getInstance("RSA");

Key pri_k=s.generatePrivate(new RSAPrivateKeySpec(new BigInteger(n=p*q),new BigInteger(e));

Key pub_k=s.generatePublic(new RSAPublicKeySpec(new BigInteger(n=p*q),new BigInteger(d));

The second method is I run a .py in command(not jython). such as: Runtime.getRuntime().exec("python C:\test.py")


I'm assuming you want to use the methods and classes in the Java standard library to actually encrypt and decrypt messages with the keys you created in Python. The best solution to this problem is Jython, which is an implementation of Python that runs on the Java Virtual Machine.

Jython is good for you in this case because it will give you access to all the stuff contained in the Java standard library. Here's a simple example of how you can use Jython to use packages in the Java standard library:

from java.lang.Math import *

# use some methods from java.lang.math
hypotenuse = sqrt(a**2 + b**2)

You can do the same thing for all the java.security methods and classes you'll want to use.

0

精彩评论

暂无评论...
验证码 换一张
取 消