package RSA_Pak;
import java.math.BigInteger;//如果为了便于手工验证,可以把s改为1,bit改为8,’标记处’改小
import java.util.Random;
import java.util.Scanner;
/**
* RSA算法简单实现
* @author windy
*/
public class RSA {
/**生成素数的长度 按照bit */
public static int bit = 16; //最好是30以下
/**几个char为一组*/
public static int s = 3;
/**将加密完的数据存到code里*/
public String code="";
//大数参数
BigInteger p;
BigInteger q;
BigInteger n;
BigInteger xn;
BigInteger e;
BigInteger d;
/**
* 初始化所有参数
*/
public RSA() {
Random r = new Random();
p = BigInteger.probablePrime(bit, r);
q = BigInteger.probablePrime(bit, r);
n = p.multiply(q);
xn = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
e = this.getE();
d = this.getMod();
}
/**获取E参数*/
public BigInteger getE(){
BigInteger bi = new BigInteger("123");
int k = new Random().nextInt(655) + 200;
int count = 1;
for (long i = 3; i < xn.longValue(); i++) {
k = Math.abs(k);
bi = new BigInteger(""+i);
if(bi.gcd(xn).equals(BigInteger.ONE)){
if (count > k){
return bi;
}else{
count++;
}
}
}
return bi;
}
public void print(char[] c){
System.out.println();
for (int i = 0; i < c.length; i++) {
System.out.print(c[i]+", ");
}
}
public void print(byte[] c){
System.out.println();
for (int i = 0; i < c.length; i++) {
System.out.print(c[i]+", ");
}
}
/**
* 获取加密数据
* @param meg
* @return String
*/
public String getEncryptMeg(String meg){
BigInteger[] bi = this.diliverMeg(meg);
BigInteger[] re = new BigInteger[bi.length];
for (int i = 0; i < bi.length; i++) {
re[i] = this.getWordsEncrypt(bi[i]);
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < re.length; i++) {
sb.append(re[i]);
}
return sb.toString();
}
/**
* 传入连接分组后的BI数组,进行加密和解密
* (演示用..)
* @param bi
* @return String
*/
public String getBigIntegerArrayUnencrypt(BigInteger[] bi){
BigInteger[] re = new BigInteger[bi.length];
BigInteger[] ree = new BigInteger[bi.length];
for (int i = 0; i < bi.length; i++) {
re[i] = this.getWordsEncrypt(bi[i]);
System.out.println("原始:"+bi[i]);
System.out.println("加密:"+re[i]);
code+=re[i].toString()+" ";//将分组加密结果存入code,每组结果用空格格开
System.out.println("code:"+code);
}
for (int i = 0; i < re.length; i++) {//解密,不过只在RSA单类演示下用
ree[i] = this.getWordsUnencrypt(re[i]);
//System.out.println("解密:"+ree[i]);
}
//StringBuilder sb = new StringBuilder();
String s = null;//this.combainateMeg(ree);
return s;
}
/**
* 将字符串按照ASCII码分组连接处理 返回一个biginteger数组
* @param meg
* @return {@link BigInteger}[]
*/
public BigInteger[] diliverMeg(String meg){
int len = meg.length()/s;
if (meg.length()%s != 0){
for (int i = meg.length()%s; i < s; i++) {
meg += ' ';
}
len++;
}
BigInteger[] bi = new BigInteger[len];
System.out.println("原始数据的ASCII码为:");
for (int i = 0; i < bi.length; i++) {
String c = "";
for (int j = 0; j < s; j++) {
char a = meg.charAt(i*s+j);
System.out.print((int)a+",");
int b = Math.abs((int)a);
if (b < 100){c += "0"+String.valueOf(b);} //不足百的前补零
else{c += String.valueOf(b);} //产生了一个字符串
}
bi[i] = new BigInteger(c);
}
System.out.println();
return bi;
}
/**
* 把解密完的大数数组按照ASCII码还原为字符内容
* @param bi
* @return String
*/
public String combainateMeg(BigInteger[] bi){
char[] re = new char[bi.length*s]; //解密字符串数组的容器
int index = 0;
System.out.println("则解密得到的总数据ASCII码为:");
for (int i = 0; i < bi.length; i++) {
String a = bi[i].toString();
if (a.length()%3 == 1){
a = "00"+a;
}else if(a.length()%3 == 2){
a = "0"+a;
}
for (int j = 0; j < s; j++) {
String b = a.substring(j*3, (j+1)*3);
int c = Integer.parseInt(b);
System.out.print(c+",");
re[index] = (char)c;
index++;
}
}
System.out.println();
return String.copyValueOf(re);
}
public BigInteger getWordsEncrypt(BigInteger bi){
return bi.modPow(e, n);
}
public BigInteger getWordsUnencrypt(BigInteger bi){
return bi.modPow(d, n);
}
/**
* 计算D的 需要一个好点的算法
* @return {@link BigInteger}
*/
public BigInteger getMod(){
BigInteger re;
BigInteger temp;
for (long i = 1; i < 20000; i++) { //基本适合32bit的素数了,‘标记处’
re = xn.multiply(BigInteger.valueOf(i));
re = re.add(BigInteger.ONE);
temp = re.mod(this.e);
if(temp.equals(BigInteger.ZERO)){
return re.divide(e);
}
}
return null; //时间太长 退出
}
/**
* 产生一个bit长度的素数
* @return prime
*/
public BigInteger getPrime(){
Random random = new Random();
BigInteger bi = BigInteger.probablePrime(RSA.bit, random);
System.out.println(bi);
return bi;
}
private static String readString(String prompt) {//从控制台读取数据
Scanner scanner = new Scanner(System.in);
System.out.println(prompt);
return scanner.nextLine();
}
public static void main(String[] args) {
RSA rsa = new RSA();
System.out.println("q = "+rsa.q);
System.out.println("p = "+rsa.p);
System.out.println("n = "+rsa.n);
System.out.println("xn ="+rsa.xn);
System.out.println("e = "+rsa.e);
System.out.println("d = "+rsa.d);
String meg = readString("请输入您要加密的消息:");
System.out.println("原始信息: "+meg);
BigInteger[] re = rsa.diliverMeg(meg);
String s = rsa.getBigIntegerArrayUnencrypt(re);
System.out.println("还原的信息: "+s);
System.out.println(rsa.getEncryptMeg(meg));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
RSA_Code.rar (28个子文件)
RSA_Code
.project 384B
bin
RSA_Pak
Result_jButton2_actionAdapter.class 657B
Welcome_jButtonExit_actionAdapter.class 672B
Inputkey_jButtonOK_actionAdapter.class 672B
ProduceKey_jButton2_actionAdapter.class 681B
ProduceKey.class 5KB
InputMessage.class 3KB
Welcome.class 3KB
ProduceKey_jButton7_actionAdapter.class 681B
Welcome_jButtonOK_actionAdapter.class 666B
ProduceKey_jButton5_actionAdapter.class 681B
ProduceKey_jButton1_actionAdapter.class 681B
Inputkey_jButtonCancel_actionAdapter.class 684B
InputMessage_jButton1_actionAdapter.class 693B
Result.class 5KB
RSA.class 6KB
InputMessage_jButton2_actionAdapter.class 693B
Result_jButton1_actionAdapter.class 657B
Inputkey.class 3KB
ProduceKey_jButton4_actionAdapter.class 681B
ProduceKey_jButton3_actionAdapter.class 681B
src
RSA_Pak
Inputkey.java 4KB
Welcome.java 4KB
ProduceKey.java 7KB
Result.java 6KB
RSA.java 7KB
InputMessage.java 3KB
.classpath 232B
共 28 条
- 1
资源评论
- cmm7531982462014-07-13不错 初学者 学习一下
ymwugui
- 粉丝: 5
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功