RSA加密算法

```import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Test{

/**
* 需要加密的字符串
*/
//	public final static String STRING = "Hello world!";
public final static String STRING = "Hello world! It is a beautiful day.";
//	public final static String STRING = "Hello world! It is a beautiful day. Hello world! It is a beautiful day. Hello world! It is a beautiful day.";

public static void main(String[] args){

System.out.println("============1.寻找质数=========");

System.out.println("============2.生成密钥=========");
System.out.println(key);

System.out.println("============3.加密============");
ArrayList<Long> list = encryToList(STRING, key);
String s = encryToString(STRING, key);
System.out.println("加密后的文本是：");
System.out.println(s);

System.out.println("============4.解密============");
System.out.println("解密之后的文本是：");
System.out.println(decry(list, key));

}

/**
* 4.解密加密后的值的list
* @param list
* @param key
* @return
*/
static String decry(ArrayList<Long> list, KeyPair key){

StringBuilder sb = new StringBuilder();
List<Long> delist = new ArrayList<Long>();
for (long ch : list) {
BigInteger a = BigInteger.valueOf(ch).pow(key.getD());
BigInteger b = a.mod(BigInteger.valueOf(key.getN()));

sb.append((char)b.longValue());

//			System.out.println((char)ch+" "+ch+"\t"+b+" "+(char)b.longValue());
}

return new String(sb);
}

/**
* 3.加密成String形式
* @param string
* @param key
* @return
*/
static String encryToString(String string, KeyPair key){

StringBuilder sb = new StringBuilder();

byte[] str = string.getBytes();

for (long ch : str) {

BigInteger a = BigInteger.valueOf(ch).pow(key.getE());
BigInteger b = a.mod(BigInteger.valueOf(key.getN()));

sb.append((char)b.longValue());

//			System.out.println((char)ch+" "+ch+"\t"+b+" "+(char)b.longValue());
}

return new String(sb);
}
/**
* 3.加密成list形式
* @param string
* @param key
* @return
*/
static ArrayList<Long> encryToList(String string, KeyPair key){

byte[] str = string.getBytes();

ArrayList<Long> list = new ArrayList<Long>();

for (long ch : str) {

BigInteger a = BigInteger.valueOf(ch).pow(key.getE());
BigInteger b = a.mod(BigInteger.valueOf(key.getN()));

//			System.out.println((char)ch+" "+ch+"\t"+b+" "+(char)b.longValue());
}

return list;

}

/**
* 2.生成密钥
* @param p
* @param q
* @param e
* @return
*/
static KeyPair getKeyPair(long p, long q, long e){
KeyPair key = new KeyPair();

long n = p*q;
long euler = (p-1)*(q-1); // e必须与euler互质，1<e<euler
// 计算满足下式的数 d
long d = 0L;
for(long i=1; ; i++){
//			System.out.println("-- 正在寻找公钥 "+i+" --");
if(e*i%euler == 1){
d = i;
break;
}
}

key.setE((int)e);
key.setD((int)d);
key.setN((int)n);

return key;
}

/**
* 1.寻找三个质数
* @return
*/
static int[] getPQE(){
List<Integer> numList = new ArrayList<Integer>();
for(int i=101; i<500; i++){
if(isPrime((long)i)){
//				System.out.println("----- "+i+" -----");
}
}
Random random = new Random();
int p = 0;
int q = 0;
int e = 0;

p = numList.get(random.nextInt(numList.size()));
do {
q = numList.get(random.nextInt(numList.size()));
} while (p==q);
do {
e = numList.get(random.nextInt(numList.size()));
} while (e==p || e==q);

return new int[]{p,q,e};
}

/**
* 0.判断一个数是否质数
* @param num
* @return
*/
static boolean isPrime(long num){
if(num<=0) return false;
for(int i=2; i<num; i++){
if(num%i==0){
return false;
}
}
return true;
}

}

// 密钥
class KeyPair {

private int e;
private int d;
private int n;

public void setE(int e) {
this.e = e;
}

public int getE() {
return e;
}

public void setD(int d) {
this.d = d;
}

public int getD() {
return d;
}

public void setN(int n) {
this.n = n;
}

public int getN() {
return n;
}

public int[] getPublicKey(){
return new int[]{e,n};
}
public String getPublicKeyStr(){
return "公钥：("+e+","+n+")";
}

public int[] getPrivateKey(){
return new int[]{d,n};
}
public String getPrivateKeyStr(){
return "私钥：("+d+","+n+")";
}

public String toString() {
return ("KeyPair["+getPublicKeyStr()+","+getPrivateKeyStr()+"]");
}
}```

65 篇文章37 人订阅

0 条评论

相关文章

46820

23840

用OC和Swift一起说说二叉树

一：在计算机科学中，二叉树是每个节点最多有两个子树的树结构。通常子树被称作“左子树”（left subtree）和“右子树”（right subtree）...

10850

29770

16820

25150

HOJ 2148&POJ 2680（DP递推，加大数运算）

Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total S...

303120

Hashtable源码解析（JDK1.8）

1 package java.util; 2 3 import java.io.*; 4 import java.util.concu...

31240

37490