关闭

POJ 2447 RSA —— RSA加密算法的破解过程

标签: ACMrsa数论
589人阅读 评论(0) 收藏 举报
分类:
RSA
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 3358   Accepted: 709

Description

RSA is the best-known public key encryption algorithm. In this algorithm each participant has a private key that is shared with no one else and a public key which is published so everyone knows it. To send a secure message to this participant, you encrypt the message using the widely known public key; the participant then decrypts the messages using his or her private key. Here is the procedure of RSA: 

First, choose two different large prime numbers P and Q, and multiply them to get N (= P * Q). 
Second, select a positive integer E (0 < E < N) as the encryption key such that E and T= (P - 1) * (Q - 1) are relatively prime. 
Third, compute the decryption key D such that 0 <= D < T and (E * D) mod T = 1. Here D is a multiplicative inverse of E, modulo T. 

Now the public key is constructed by the pair {E, N}, and the private key is {D, N}. P and Q can be discarded. 

Encryption is defined by C = (M ^ E) mod N, and decryption is defined by M = (C ^ D) mod N, here M, which is a non-negative integer and smaller than N, is the plaintext message and C is the resulting ciphertext. 

To illustrate this idea, let’s see the following example: 
We choose P = 37, Q = 23, So N = P * Q = 851, and T = 792. If we choose E = 5, D will be 317 ((5 * 317) mod 792 = 1). So the public key is {5, 851}, and the private key is {317, 851}. For a given plaintext M = 7, we can get the ciphertext C = (7 ^ 5) mod 851 = 638. 

As we have known,for properly choosen very large P and Q, it will take thousands of years to break a key, but for small ones, it is another matter. 

Now you are given the ciphertext C and public key {E, N}, can you find the plaintext M?

Input

The input will contain several test cases. Each test case contains three positive integers C, E, N (0 < C < N, 0 < E < N, 0 < N < 2 ^ 62).

Output

Output the plaintext M in a single line.

Sample Input

638 5 851

Sample Output

7

Source

POJ Monthly,static

一道数论的模板题,做法很简单,但是需要一个比较复杂的证明

关于RSA请参考http://en.wikipedia.org/wiki/RSA_(algorithm)

算法举例:


我们只要模拟这一过程即可。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 40000 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-10
const long long MOD = 1000000000 + 7;
const int mod = 10007;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
typedef vector<int> vec;
typedef vector<vec> mat;


#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
#define gcc 10007
inline __int64 gcd(__int64 a , __int64 b)
{
    if(b > a)return gcd(b , a);
    return b == 0 ? a : gcd(b , a % b);
}
inline __int64 Produce_Mod(__int64 a , __int64 b , __int64 Mod)
{
    __int64 sum = 0;
    while(b > 0)
    {
        if(b & 1)sum = (sum + a) % Mod;
        a = (a + a) % Mod;
        b >>= 1;
    }
    return sum;
}
inline __int64 Power(__int64 a , __int64 b , __int64 Mod)
{
    __int64 sum = 1;
    while(b > 0)
    {
        if(b & 1)sum = Produce_Mod(sum , a , Mod);
        a = Produce_Mod(a , a , Mod);
        b >>= 1;
    }
    return sum;
}
__int64 Pollard_rho(__int64 n)
{
    int i = 1;
    __int64 x = rand() % (n - 1) + 1;
    __int64 y = x;
    __int64 k = 2;
    __int64 d;
    do
    {
        i++;
        d = gcd(n + y - x , n);
        if(d > 1 && d < n)
        {
            return d;
        }
        if(i == k)y = x , k *= 2;
        x = ((Produce_Mod(x , x , n) - gcc) % n + n) % n;
    }while(y != x);
    return n;
}
__int64 extgcd(__int64 a , __int64 b , LL &x , LL &y)
{
    if(b == 0){x = 1;y = 0; return a;}
    __int64 d = extgcd(b , a % b , x , y);
    LL t = x ; x = y ; y = t - a / b * y;
    return d;
}
int main()
{
    //ios::sync_with_stdio(false);
    #ifdef Online_Judge
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif // Online_Judge
    __int64 c , e , n , p , q ,m , t , d;
    while(~scanf("%lld%lld%lld" , &c , &e , &n))
    {
        p = Pollard_rho(n);q = n / p;
        t = (p - 1) * (q - 1);
        LL x;
        extgcd(e , t , d , x);
        d = (d % t + t) % t;
        printf("%lld\n" , Power(c , d , n));
    }
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:160016次
    • 积分:4189
    • 等级:
    • 排名:第7360名
    • 原创:266篇
    • 转载:12篇
    • 译文:0篇
    • 评论:14条
    最新评论