What do researchers working at ICPC (Institute for Cryptographic Programming and Computing) do for fun? Well, as you probably have expected, in addition to solving algorithm-related problems on online judges, they also like to toy with various cryptographic schemes. Recently one of the researchers, Tom, has become interested in RSA algorithm implementations used in handheld devices.
Note that the description of the general RSA algorithm is as follows:
-
1)
- Choose two distinct prime numbers p andq, and let n = pq; 2)
- Choose an integer e such thatgcd(e,(p - 1)(q - 1) = 1; 3)
-
Compute the integer d that satisfies the congruence relationde
1( mod(p - 1)(q - 1)).
Then, if person A wants to give person B a way to send an encrypted message to him, A can follow the above steps and release(n, e) as his public key. Upon receiving A's public key, B can simply encrypt messagex (0x <n) by computing y = xe modn. This would result in a message which ideally only A could decrypt with his private keyd: x = yd modn.
As the computation power of handheld devices is usually limited, a relatively smalle is usually used to encrypt data. However this can lead to great security risks. For example, it is quite simple to recoverp and q (i.e., factorn) when you have both the public key (n, e) and the private key d. Could you help Tom write a program to demonstrate this?
Input
There are multiple test cases in the input file.
Each test case contains three integers, n, d, and e (n10100, 3
e
31). All three integers are given without any preceding zeros. It is guaranteed that all numbers satisfy the condition as given in the problem statement.
Two successive test cases are separated by a blank line. A case with n = 0, d = 0 and e = 0 indicates the end of the input file, and should not be processed by your program.
Output
For each test case, please print two prime numbers, p andq, such that n = pq and p < q, in the format as illustrated below.
Sample Input
55 27 3 290203 168101 5 0 0 0
Sample Output
Case #1: 5 11 Case #2: 29 10007
知道n = pq,(e,(p - 1)(q - 1) = 1,再根据de 1( mod(p - 1)(q - 1))算出d。
d从0到(p - 1)(q - 1)一定有一个de%(p - 1)(q - 1)=1的,因此d<(p-1)(q-1),所以用de-1除以1...e里能整除的一定有一个是(p - 1)(q - 1)。
这样得到
p=(n+1-t+sqrt(sqrt((t-n-1)^2-4*n))/2
q=(n+1-t-sqrt(sqrt((t-n-1)^2-4*n))/2
只需要再判断根号里是不是整数(这两个式子如果根号里是整数就肯定是整数)。
不用判断e和(p-1)(q-1)是否互质,因为满足de 1( mod(p - 1)(q - 1))一定满足(e,(p - 1)(q - 1) = 1,否则不可能和1同余。
JAVA大数开方可以用二分。
import java.util.*;
import java.math.*;
public class Main {
public static BigInteger sqrt(BigInteger n){
BigInteger l=BigInteger.ZERO,r=n,mid;
while(l.compareTo(r)<0){
mid=l.add(r).divide(BigInteger.valueOf(2));
if(mid.multiply(mid).compareTo(n)<0) l=mid.add(BigInteger.ONE);
else if(mid.multiply(mid).compareTo(n)>0) r=mid.subtract(BigInteger.ONE);
else return mid;
}
return l;
}
public static void main(String[] args) {
int cas=0;
BigInteger N,D,p,q;
int E;
Scanner read=new Scanner(System.in);
while(true){
N=read.nextBigInteger();
D=read.nextBigInteger();
E=read.nextInt();
if(N.compareTo(BigInteger.ZERO)==0&&D.compareTo(BigInteger.ZERO)==0&&E==0) break;
for(int i=1;i<=E;i++) if(D.multiply(BigInteger.valueOf(E)).subtract(BigInteger.ONE).mod(BigInteger.valueOf(i)).compareTo(BigInteger.ZERO)==0){
BigInteger pq=D.multiply(BigInteger.valueOf(E)).subtract(BigInteger.ONE).divide(BigInteger.valueOf(i));
BigInteger t=pq.subtract(N).subtract(BigInteger.ONE).pow(2).subtract(N.multiply(BigInteger.valueOf(4)));
BigInteger sq=sqrt(t);
if(sq.multiply(sq).compareTo(t)==0){
p=N.subtract(pq).add(BigInteger.ONE).subtract(sq).divide(BigInteger.valueOf(2));
q=N.subtract(pq).add(BigInteger.ONE).add(sq).divide(BigInteger.valueOf(2));
if(p.compareTo(q)>0){
BigInteger temp=p;
p=q;
q=temp;
}
System.out.println("Case #"+ ++cas +": "+p+" "+q);
break;
}
}
}
}
}