前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RSA加密算法

RSA加密算法

作者头像
yawn
发布2018-03-14 11:12:13
2K0
发布2018-03-14 11:12:13
举报
代码语言:javascript
复制
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()+"]");
	}
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档