zoukankan      html  css  js  c++  java
  • HDU 1211 RSA(快速幂)

    Description

    RSA is one of the most powerful methods to encrypt data. The RSA algorithm is described as follow:

    > choose two large prime integer p, q
    > calculate n = p × q, calculate F(n) = (p - 1) × (q - 1)
    > choose an integer e(1 < e < F(n)), making gcd(e, F(n)) = 1, e will be the public key
    > calculate d, making d × e mod F(n) = 1 mod F(n), and d will be the private key

    You can encrypt data with this method :

    C = E(m) = m e mod n

    When you want to decrypt data, use this method :

    M = D(c) = c d mod n

    Here, c is an integer ASCII value of a letter of cryptograph and m is an integer ASCII value of a letter of plain text.

    Now given p, q, e and some cryptograph, your task is to "translate" the cryptograph into plain text.

    Input

    Each case will begin with four integers p, q, e, l followed by a line of cryptograph. The integers p, q, e, l will be in the range of 32-bit integer. The cryptograph consists of l integers separated by blanks.

    Output

    For each case, output the plain text in a single line. You may assume that the correct result of plain text are visual ASCII letters, you should output them as visualable letters with no blank between them.

    Sample Input

    101 103 7 11
    7716 7746 7497 126 8486 4708 7746 623 7298 7357 3239

    Sample Output

    I-LOVE-ACM.

    题意:给定密约和密文,让我们把密文翻译成纯文本
    思路:由于M = D(c) = c d mod n,于是我们想到快速幂求出M,即该数据对应的ASCII值,然后以字符的方式输出即可

    代码如下:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <algorithm>
     5 #include <cstring>
     6 #include <cmath>
     7 using namespace std;
     8 
     9 int quickpow(int a,int k,int mod)       //作用:求出密文对应的ASCII码
    10 {
    11     int r=1;
    12     while(k)
    13     {
    14         if(k&1) r=(r*a)%mod;
    15         a=((a%mod)*(a%mod))%mod;
    16         k>>=1;
    17     }
    18     return r%mod;
    19 }
    20 
    21 int main()
    22 {
    23     int p,q;
    24     int e;
    25     int l;
    26     while(~scanf("%d%d%d%d",&p,&q,&e,&l))
    27     {
    28         int n=p*q;
    29         int fn=(p-1)*(q-1);
    30         int d;
    31         for(int i=fn;;i+=fn)     //求出d的值,最多求e次
    32         {
    33             if((i+1)%e==0)
    34             {
    35                 d=(i+1)/e;
    36                 break;
    37             }
    38         }
    39         for(int i=0;i<l;i++)
    40         {
    41             int a;
    42             scanf("%d",&a);
    43             int m=quickpow(a,d,n)%n;
    44             if(i!=l-1) printf("%c",m);
    45             else printf("%c
    ",m);
    46         }
    47     }
    48     return 0;
    49 }
    
    
    
     
    
    
    
    
    
  • 相关阅读:
    在多线程中使用静态方法是否有线程安全问题(转载)
    为什么乐观的人多能成功呢?
    每个人都是超级英雄-《技巧:如何用一年的时间获得十年的经验》
    003|再谈10000小时,三板斧破四困境
    002|也谈10000小时
    在职场中如何通过讲故事,影响他人、支持自己(下篇)
    全面解读:微信服务号升级和群发增至4条的应用方法
    Technical reading July-15
    read links July-14
    Technical news July-11
  • 原文地址:https://www.cnblogs.com/Amidgece/p/5764972.html
Copyright © 2011-2022 走看看