import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public
class
RSAUtil {
public
static
String encrypt(String message) {
byte[] result = null;
try
{
result = encrypt(message, getPublicKey());
}
catch
(Exception e) {
e.printStackTrace();
}
return
toHexString(result);
}
public
static
String decrypt(String message) {
byte[] result = null;
try
{
result = decrypt(message, getPublicKey());
}
catch
(Exception e) {
e.printStackTrace();
}
return
new
String(result);
}
private
static
byte[] encrypt(String message, Key key) throws Exception {
Cipher cipher = Cipher.getInstance(
"RSA"
,
new
BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, key);
return
cipher.doFinal(message.getBytes(
"gb2312"
));
}
private
static
byte[] decrypt(String message, Key key) throws Exception {
Cipher cipher = Cipher.getInstance(
"RSA"
,
new
BouncyCastleProvider());
cipher.init(Cipher.DECRYPT_MODE, key);
return
cipher.doFinal(toBytes(message));
}
public
static
PublicKey getPublicKey() {
PublicKey publicKey = null;
String modulus =
"140865665237544398577638791993321201143991791099370252934699963963887058026979531275917645451893685346013654333931757603593193739776986525943697469996693704995753266331593233395038088698299308180612215713544577462613426793519824197226393059683065343801412208205295045502348474411031999124137863144916358656019"
;
String publicExponent =
"65537"
;
BigInteger m =
new
BigInteger(modulus);
BigInteger e =
new
BigInteger(publicExponent);
RSAPublicKeySpec keySpec =
new
RSAPublicKeySpec(m, e);
try
{
KeyFactory keyFactory = KeyFactory.getInstance(
"RSA"
,
new
BouncyCastleProvider());
publicKey = keyFactory.generatePublic(keySpec);
}
catch
(Exception e1) {
e1.printStackTrace();
}
return
publicKey;
}
private
static
final
byte[] toBytes(String s) {
byte[] bytes;
bytes =
new
byte[s.length() / 2];
for
(int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);
}
return
bytes;
}
public
static
String toHexString(byte[] b) {
StringBuilder sb =
new
StringBuilder(b.length * 2);
for
(int i = 0; i < b.length; i++) {
sb.append(HEXCHAR[(b[i] & 0xf0) >>> 4]);
sb.append(HEXCHAR[b[i] & 0x0f]);
}
return
sb.toString();
}
private
static
char[] HEXCHAR = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
}