📄 rsaalgorithm.java
字号:
package org.encryption.rsa;
import java.math.BigInteger;
import java.util.Random;
public class RSAAlgorithm {
private BigInteger p;
private BigInteger q;
private BigInteger n;
private BigInteger e;
private BigInteger d;
public RSAAlgorithm() {
init();
}
// rsa系统初始化,包括产生公钥(e,n)私钥(p,q,d);
private void init() {
System.out.println("----------开始初始化--------生成公钥密钥");
// 用Miller-Rabin概率素数测试法产生大素数
p = BigInteger.probablePrime(512, new Random());
q = BigInteger.probablePrime(500, new Random());
n = p.multiply(q);
e = BigInteger.valueOf(7);
BigInteger T = p.subtract(BigInteger.ONE).multiply(
q.subtract(BigInteger.ONE));
// (e,T)=1&&(1<e<T)==true
while (T.gcd(e).compareTo(BigInteger.ONE) != 0) {
e.add(BigInteger.ONE);
}
d = e.modInverse(T);
System.out.println("----------初始化完成--------");
}
/**
* @param withPrivekey
* 用私钥加密是为true,用公钥加密时为false
* @return
*/
public BigInteger encrypt(BigInteger plainInt, boolean withPrivakey) {
if (withPrivakey)
return modPow(plainInt, d, n);
else
return modPow(plainInt, e,
n);
}
/**
* @param withPrivekey
* 用私钥解密是为true,用公钥解密时为false
* @return
*/
public BigInteger decrypt(BigInteger encryptedInt, boolean withPrivakey) {
if (withPrivakey)
return modPow(encryptedInt, d, n);
else
return modPow(encryptedInt,
e, n);
}
// 核心:大数的模幂运算~~~~蒙哥玛利模幂算法(a^^b)%c
private BigInteger XmodPow(BigInteger a, BigInteger b, BigInteger c) {
if (b.compareTo(BigInteger.ONE) == 0)
return a.mod(c);
BigInteger index = BigInteger.ONE;
BigInteger result = a.mod(c);
BigInteger temp = BigInteger.ONE;
while (index.compareTo(b) == -1) {
temp = result;
result = result.multiply(result).mod(c);
index = index.add(index);
}
if(index.compareTo(b) ==1)
result = temp.multiply(modPow(a,b.subtract(index.divide(BigInteger.valueOf(2))),c)).mod(c);
return result;
}
//核心:大数的模幂运算~~~~二元法(a^^b)%c,效率较XmodPow()好很多
private BigInteger modPow(BigInteger a, BigInteger b, BigInteger c) {
String bBinaryString = ToBinaryString(b.toByteArray());
BigInteger finalResult = BigInteger.ONE;
BigInteger tempResult = BigInteger.ONE;
for (int i = bBinaryString.length() - 1; i >= 0; i--) {
if (i == (bBinaryString.length() - 1))
tempResult= a.mod(c);
else
tempResult = tempResult.multiply(tempResult).mod(c);
if (bBinaryString.charAt(i) == '1') {
finalResult = finalResult.multiply(tempResult).mod(c);
}
}
return finalResult;
}
// 判断一个大数的能否被2整除
/*
* private boolean isCompsite(BigInteger bigInt) { if (bigInt.byteValue() %
* 2 == 0) return true; return false; }
*/
// 字节数组转化为字符串
private String ToBinaryString(byte[] binary) {
StringBuffer strBuf = new StringBuffer();
String intStr;
for (int i = 0; i < binary.length; i++) {
intStr = Integer.toBinaryString(binary[i]);
if (intStr.length() == 8)
strBuf.append(intStr);
else if (intStr.length() > 8) {
strBuf.append(intStr.substring(intStr.length() - 8, intStr
.length()));
} else {
char[] padding = new char[8 - intStr.length()];
for (int j = 0; j < padding.length; j++)
padding[j] = '0';
strBuf.append(padding);
strBuf.append(intStr);
}
}
return strBuf.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -