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 }
    
    
    
     
    
    
    
    
    
  • 相关阅读:
    mysql排序
    MySQL命令行导出数据库
    os.path python使用遍历文件夹文件
    linux后台运行和关闭、查看后台任务
    WebDriver中自动识别验证码--Python实现
    一起学react (1) 10分钟 让你dva从入门到精通
    git命令大全
    jQuery中的100个技巧(译)
    Flow入门初识
    深入理解typescript的Functions
  • 原文地址:https://www.cnblogs.com/Amidgece/p/5764972.html
Copyright © 2011-2022 走看看