zoukankan      html  css  js  c++  java
  • ural 1141. RSA Attack RSA算法

    1141. RSA Attack

    Time limit: 1.0 second
    Memory limit: 64 MB
    The RSA problem is the following: given a positive integer n that is a product of two distinct odd primes p and q, a positive integer e such that gcd(e, (p-1)*(q-1)) = 1, and an integer c, find an integer m such that me = c (mod n).

    Input

    One number K (K <= 2000) in the first line is an amount of tests. Each next line represents separate test, which contains three positive integer numbers – e, n and c (e, n, c <= 32000, n = p*q, p, q – distinct odd primes, gcd(e, (p-1)*(q-1)) = 1, e < (p-1)*(q-1) ).

    Output

    For each input test the program must find the encrypted integer m.

    Sample

    inputoutput
    3
    9 187 129
    11 221 56
    7 391 204
    
    7
    23
    17
    

    Problem Author: Michael Medvedev

    思路:这个主要是一个关于RSA加密算法的问题,如果想具体了解可以看阮一峰的网络日志

    该题的解法很简单, 首先把n分解成两个质数p, q;

    然后求e与(p-1)*(q-1)的逆元d

    最后求一发 c^d (mod n)

     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 const int MAXN = 32000;
     5 int N, T;
     6 int prime[MAXN+10], cnt = 0;
     7 bool a[MAXN+10];
     8 int extgcd(int a, int b, int &x, int &y){
     9     int d = a;
    10     if(b != 0){
    11         d = extgcd(b, a%b, y, x);
    12         y -= (a/b)*x;
    13     }else {
    14         x = 1, y = 0;
    15     }return d;
    16 }
    17 
    18 int mod_inv(int a, int m){
    19     int x, y;
    20     extgcd(a, m, x, y);
    21     return (m+x%m)%m;
    22 }
    23 
    24 void init(){
    25     for(int i = 2; i <= MAXN; i++) a[i] = true;
    26     for(int i = 2; i <= MAXN; i++){
    27         if(a[i]){
    28             prime[++cnt] = i;
    29         }
    30         for(int j = 1; j <= MAXN; j++){
    31             if(prime[j]*i > MAXN) break;
    32             a[prime[j]*i] = false;
    33             if(i%prime[j] == 0) break;
    34         }
    35     }
    36 }
    37 int quick_pow(int a, int x, int p){
    38     int ans = 1;
    39     while(x > 0){
    40         if(x&1){
    41             x--;
    42             ans = ans*a%p;
    43         }
    44         x >>= 1;
    45         a = a*a%p;
    46     }
    47     return ans;
    48 }
    49 
    50 int main() {
    51     //freopen("in.txt", "r", stdin);
    52     int T;
    53     init();
    54     scanf("%d", &T);
    55     while(T--){
    56         int e, p, q, n, c, m, d;
    57         scanf("%d%d%d", &e, &n, &c);
    58         for(int i = 1; i <= cnt && prime[i] <= n; i++){
    59             if(prime[i]%2 == 1 && n % prime[i] == 0){
    60                 p = prime[i];
    61                 q = n/prime[i];
    62                 break;
    63             }
    64         }
    65         d = mod_inv(e, (p-1)*(q-1));
    66         m = quick_pow(c, d, n);
    67         printf("%d
    ", m);
    68     }
    69     return 0;
    70 }
  • 相关阅读:
    [CQOI2015]选数
    [AHOI2009]中国象棋
    [ZJOI2012]灾难
    [NOI2018]屠龙勇士
    [APIO2016]划艇
    [ZJOI2011]礼物
    cent 7 识别exfat
    C语言风格的 for 循环(SHELL的循环写法 已验证20200517)
    系统安装时间
    单用户模式修改root密码
  • 原文地址:https://www.cnblogs.com/cshg/p/5892329.html
Copyright © 2011-2022 走看看