需要下载bcprov-jdk15-135.jar包
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 "";
}
}
}
分享到:
- 2009-06-09 10:38
- 浏览 3219
- 评论(1)
- 论坛回复 / 浏览 (1 / 3278)
- 查看更多
相关推荐
主要介绍了Java实现的RSA加密解密算法,结合实例形式分析了java RAS加密解密算法的相关实现技巧,需要的朋友可以参考下
此文档内含有RSA加解密算法java源程序,可以直接运行。
RSA加密算法实现以及C#与java互通加解密,解决RSA算法在java与C#相互通用
用JAVA实现RSA的解密和加密的算法。~亲测可以运行
Java版MD5和RSA加解密算法完整实现,欢迎分享!
非对称加密算法RSA算法的JAVA实现,可视化界面,支持多种形式,供参考
分别用C#、Java实现的RSA和DES加解密算法,包含demo源码,经实际项目验证。
RSA加密解密算法 java版 源码 可运行 很实用
* RSA加密解密:私钥解密,公钥加密。 * RSA数字签名-俗称加签验签:私钥加签,公钥验签。 * RSA加密解密:私钥解密,公钥加密。 * RSA数字签名-俗称加签验签:私钥加签,公钥验签。 * RSA加密解密:私钥...
rsa加密解密算法;Base64Utils.java;RSAUtils.java;3.测试类
RSA算法:用java实现的带界面的RSA加密、解密算法
RSA加解密算法,JAVA源代码,网络安全技术及应用,窗口实现,英文字母a-z
C#与java平台RSA加密解密签名验签互通案例,解决RSA在C#与java加密算法不同导致验签不过情况
Eclipse为平台的Java rsa加解密算法实现有界面。没有MAIN函数自己加上就行了
JAVA RSA 加密算法 RSA.java RSA公钥加解密,RSA私钥加解密,MD5withRSA 签名 MD5withRSA签名较验
JAVA 实现RSA的加解密算法,通过BASE64编码,密钥长度1024
根据证书私钥加签---》RSA公钥加密--》RSA私钥解密---》证书公钥验签
可以直接运行成功的RSA加密解密示例 JAVA端采用公钥加密,服务端C#采用私钥解密。
RSA算法加解密java工具, 包括: 创建公钥、私钥、加密、解密、分段加密 创建公钥、私钥、加密、解密、分段加密 创建公钥、私钥、加密、解密、分段加密
Android RSA加密解密,解决java和android之前实现参数加密,和ios平台也可以实现加解密