Java RSA 加解密算法实现

需要下载bcprov-jdk15-135.jar包
[code]
import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPublicKeySpec;

import javax.crypto.Cipher;

public class RSAEncrypt {

private String nprime;
private String eprime;
private String dprime;
private String pprime;
private String qprime;
private String dmp;
private String dmq;
private String qmp;

public void setNprime(String nvalue){
this.nprime=nvalue;
}

public void setEprime(String evalue){
this.eprime=evalue;
}
public void setDprime(String dvalue){
this.dprime=dvalue;
}
public void setPprime(String pvalue){
this.pprime=pvalue;
}
public void setQprime(String qvalue){
this.qprime=qvalue;
}
public void setDmp(String dmpvalue){
this.dmp=dmpvalue;
}
public void setDmq(String dmqvalue){
this.dmq=dmqvalue;
}
public void setQmp(String qmpvalue){
this.qmp=qmpvalue;
}


public String BytesToHex(byte[] in){
String hs="";
String stmp="";
for (int n=0;n<in.length;n++) {
stmp=(Integer.toHexString(in[n] & 0XFF));
if (stmp.length()==1) hs=hs+"0"+stmp;
else hs=hs+stmp;
}
return hs.toLowerCase();
}

public byte[] hexToBytes(String src)
{
int m = 0, n = 0;
int l = src.length() / 2;
String str;
byte[] ret = new byte[l];

for (int i = 0; i < l; i++)
{
m = i * 2 + 1;
n = m + 1;
str = src.substring(i * 2, i * 2 + 2);
ret[i] = (byte) (0xff & Integer.parseInt(str, 16));
}
return ret;
}

public String Encrypt(String str){
try{

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(nprime,16),new BigInteger(eprime,16));

Cipher cipher = Cipher.getInstance("RSA");

SecureRandom random = new SecureRandom();

KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");

generator.initialize(512,random);

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

Key publicKey =(Key) fact.generatePublic(pubKeySpec);

cipher.init(Cipher.ENCRYPT_MODE, publicKey,random);

byte[] cipherText = cipher.doFinal(str.getBytes());

return BytesToHex(cipherText);

}
catch(Exception ex){
return "";
}

}

public String de_Encrypt(String encryptText){

try{

Cipher cipher = Cipher.getInstance("RSA");

RSAPrivateCrtKeySpec privKeySpec = new RSAPrivateCrtKeySpec(
new BigInteger(nprime,16),
new BigInteger(eprime,16),
new BigInteger(dprime,16),
new BigInteger(pprime,16),
new BigInteger(qprime,16),
new BigInteger(dmp,16),
new BigInteger(dmq,16),
new BigInteger(qmp,16));

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

Key privKey = (Key) fact.generatePrivate(privKeySpec);

cipher.init(Cipher.DECRYPT_MODE, privKey);

byte[] forumcookie = hexToBytes(encryptText);

byte[] plainText = cipher.doFinal(forumcookie);

return new String(plainText);

}
catch(Exception ex){
System.out.println(ex.toString());
return "";
}
}

}
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值