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.寻找质数=========");
int[] primeNum = getPQE();
System.out.println("p="+primeNum[0]+", q="+primeNum[1]+", e="+primeNum[2]);
System.out.println("============2.生成密钥=========");
KeyPair key = getKeyPair(primeNum[0], primeNum[1], primeNum[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()));
delist.add(b.longValue());
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()));
list.add(b.longValue());
// 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)){
numList.add(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()+"]");
}
}