【发布时间】:2017-11-06 11:32:15
【问题描述】:
import java.math.BigDecimal;
import java.math.BigInteger;
public class RSAEncryption {
public static void main(String[] args){
//STAGE ONE AND TWO
int p=61;
int q=53;
//STAGE THREE
Integer pq=p*q;
//STAGE FOUR
int pTotient=p-1;
int qTotient=q-1;
//STAGE FIVE
// int finalTotient=pTotient*qTotient;
int finalTotient=780;
//STAGE SIX AND SEVEN
int e=0;
if((p>17)||(q>17)){
e=17;
}else{
e=7;
}
//STAGE EIGHT AND NINE
int d=0;
int count=0;
while ((e*d%finalTotient)!=1){
d=d+1;
count=count+1;
}
//STAGE TEN
int ed=e*d;
System.out.println("p = "+p);
System.out.println("q = "+q);
System.out.println("pq = "+pq);
System.out.println("final totient = "+finalTotient);
System.out.println("calculation of ed = "+ed);
System.out.println("d = "+d);
//ENCRYPTION
int message=65;
//PUBLIC KEY
int result00 = (int)Math.pow(message, e);
System.out.println(result00);
int testing00=result00%3233;
System.out.println(testing00);
BigInteger dd00=new BigInteger("65");
BigInteger test00=dd00.pow(17);
System.out.println("BigInteger Power Calc: "+test00);
int to100=test00.intValue();
int finalt=to100%3233;
System.out.println(to100);
从上面的代码可以看出我正在尝试用 Java 制作一个 RSA 加密算法。问题是Java“Math.pow()”内置的幂函数不够精确,最终输出“to100”应该等于2790。我不确定我错过了什么并花了几个月。
我也尝试过使用 BigInteger.pow() 但无济于事。我不确定为什么 BigInteger.pow() 方法不起作用。
我正在尝试获取 c :
c = 65^17 mod 3233 = 2790。如这里的示例所示:https://en.wikipedia.org/wiki/RSA_(cryptosystem)
【问题讨论】:
-
BigInteger类有一个方法可以做just this thing。 -
例如here。
标签: java encryption rsa biginteger