首页>代码>jsp网上数字签名管理系统>/rsa/src/com/util/RSA.java
package com.util;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.interfaces.RSAPublicKey;
import java.util.logging.Level;
import java.util.logging.Logger;


public class RSA {
/**
* 创建密钥对生成器,指定加密和解密算法为RSA
* @param keylen
* @return
*/
public String[] createKey(int keylen) {// 输入密钥长度
String[] output = new String[5]; // 用来存储密钥的e n d p q
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(keylen); // 指定密钥的长度,初始化密钥对生成器
KeyPair kp = kpg.generateKeyPair(); // 生成密钥对
RSAPublicKey puk = (RSAPublicKey) kp.getPublic();
RSAPrivateCrtKey prk = (RSAPrivateCrtKey) kp.getPrivate();
BigInteger e = puk.getPublicExponent();
BigInteger n = puk.getModulus();
BigInteger d = prk.getPrivateExponent();
BigInteger p = prk.getPrimeP();
BigInteger q = prk.getPrimeQ();
output[0] = e.toString();
output[1] = n.toString();
output[2] = d.toString();
output[3] = p.toString();
output[4] = q.toString();
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(RSAChinese.class.getName()).log(Level.SEVERE, null, ex);
}
return output;
}


/**
* 加密在RSA公钥中包含有两个整数信息:e和n。对于明文数字m,计算密文的公式是m的e次方再与n求模。
* @param clearText 明文
* @param eStr 公钥
* @param nStr
* @return
*/
public String encrypt(String clearText, String eStr, String nStr) {
String secretText = new String();

try {
clearText = URLEncoder.encode(clearText,"GBK"); 
byte text[]=clearText.getBytes("GBK");//将字符串转换成byte类型数组,实质是各个字符的二进制形式
BigInteger mm=new BigInteger(text);//二进制串转换为一个大整数
clearText = mm.toString();

BigInteger e = new BigInteger(eStr);
BigInteger n = new BigInteger(nStr);
byte[] ptext = clearText.getBytes("UTF8"); // 获取明文的大整数
BigInteger m = new BigInteger(ptext);
BigInteger c = m.modPow(e, n);
secretText = c.toString();
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(RSAChinese.class.getName()).log(Level.SEVERE, null, ex);
}
return secretText;
}


/**
* 解密
* @param secretText 密文
* @param dStr 私钥
* @param nStr
* @return
*/
public String decrypt(String secretText, String dStr, String nStr) {
StringBuffer clearTextBuffer = new StringBuffer();
BigInteger d = new BigInteger(dStr);// 获取私钥的参数d,n
BigInteger n = new BigInteger(nStr);
BigInteger c = new BigInteger(secretText);
BigInteger m = c.modPow(d, n);// 解密明文
byte[] mt = m.toByteArray();// 计算明文对应的字符串并输出
for (int i = 0; i < mt.length; i++) {
clearTextBuffer.append((char) mt[i]);
}
String temp = clearTextBuffer.toString();//temp为明文的字符串形式
System.out.println("dectemp="+temp);
BigInteger b = new BigInteger(temp);//b为明文的BigInteger类型
byte[] mt1=b.toByteArray();

try {
String clearText = (new String(mt1,"GBK"));
clearText=URLDecoder.decode(clearText,"GBK"); 
return clearText;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}


public static void main(String[] args) {
RSA rsa = new RSA();
String[] str = rsa.createKey(1024);
String clearText = "123abc测试";
String secretText = rsa.encrypt(clearText, str[0], str[1]);
System.out.println("明文是:" + clearText);
System.out.println("密文是:" + secretText);
System.out.println("解密后的明文是:" + rsa.decrypt(secretText, str[2], str[1]));
}
}
最近下载更多
except I  LV2 2023年11月5日
2036495585  LV9 2023年9月25日
yymmdm  LV6 2022年8月30日
novice2  LV1 2022年8月28日
bai620123  LV16 2022年8月9日
liu2022  LV14 2022年7月31日
dasdascccf  LV10 2022年6月16日
小丶无奈  LV10 2022年6月14日
wanglinddad  LV54 2022年3月13日
lijun1314  LV6 2021年9月13日
最近浏览更多
吞吞吐吐她  LV5 3月28日
17693282606  LV11 2023年12月20日
1222222222222222222  LV2 2023年12月17日
2749263182 2023年11月12日
暂无贡献等级
except I  LV2 2023年11月5日
pangzhihui  LV12 2023年10月23日
微信网友_6680567232876544  LV8 2023年10月10日
2036495585  LV9 2023年9月25日
1WQAQW1  LV2 2023年6月8日
jsp10086 2023年6月2日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友