tags:

views:

50

answers:

2

I use python rsa module(http://stuvel.eu/rsa) get private_key and public_key.
How can I use these private_key and public_key to encrypt or decrypt in java?

A: 

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.

Rafe Kettler
The rsa module(http://stuvel.eu/rsa) is not the Jython' module. So I cann't import rsa .
You can use a lot of modules that are Python 2.5 compatible with Jython, especially if this module is pure-Python like it says. If not that, there's an incredibly good security and cryptography library in Java.
Rafe Kettler
+1  A: 

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")