uva1431 - Cryptography Reloaded 大数

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 $ \equiv$ 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 (0$ \le$x <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 (n$ \le$10100, 3$ \le$e$ \le$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$ \equiv$ 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$ \equiv$ 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;
                }
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值