hdu 1211 RSA (逆元)

本文深入探讨了RSA加密算法的核心原理,并通过实际代码实现了解密过程。包括选择大素数、计算模数、选取公钥与私钥等步骤。详细解析了逆元求法、大整数幂次运算以及如何将加密后的数字转换为明文字符。通过实例演示,读者能够掌握RSA算法的完整解密流程。

RSA

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1243    Accepted Submission(s): 901


Problem 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.
 

 

Author
JGShining(极光炫影)
 

 

Source
 

 

Recommend
Eddy   |   We have carefully selected several similar problems for you:   1299  1695  1573  1213  1576 
 

 

 1 //0MS    236K    1318 B    G++
 2 /*
 3 
 4     题意:
 5         RSA密码加解密法的解密
 6     
 7     模拟题:
 8         可以算水题,不过也磨了挺久,一是逆元求法不明确,
 9     二是O(lgn)的n次方模数算法忘了,三是没注意64位,
10     还有电脑有点卡!!郁闷 
11 
12 */
13 #include<stdio.h>
14 #include<string.h>
15 /***************************************
16 函数:ExGcd 
17 功能:求两个数的最大公约数和模P的乘法逆元。
18 输入:a,b 输入参数,求这两个数的最大公约数
19    和a模b的逆元 或 b模a的逆元。
20 输出:x,y 分别表示a模b的逆元和b模a的逆元。
21 返回:r 表示a b 的最大公约数。
22 *************************************/
23 __int64 Exgcd(__int64 a,__int64 b,__int64 &x,__int64 &y)
24 {
25     if(b==0){
26         x=1;
27         y=0;
28         return a;
29     }
30     __int64 r=Exgcd(b,a%b,x,y);
31     __int64 t=x;
32     x=y;
33     y=t-a/b*y;
34     return r;
35 } 
36 __int64 fac(__int64 a,__int64 d,__int64 n)
37 {
38     a%=n;
39     int t=1;
40     while(d){
41         if(d%2) t=(t*a)%n; 
42         a=(a*a)%n;
43         d/=2;
44     }
45     return t;
46 }
47 int main(void)
48 {
49     __int64 p,q,e;
50     __int64 l,a;
51     while(scanf("%I64d%I64d%I64d%I64d",&p,&q,&e,&l)!=EOF)
52     { 
53         char c[105];
54         memset(c,0,sizeof(c));
55         __int64 d1=0,d2=0;
56         __int64 n=p*q;
57         Exgcd(e,(p-1)*(q-1),d1,d2);
58         d1=(d1+(p-1)*(q-1))%((p-1)*(q-1));
59         //printf("%d %d",d1,d2); 
60         for(int i=0;i<l;i++){
61             scanf("%I64d",&a);
62             a=fac(a,d1,n);
63             int b=a;
64             c[i]=b;
65             //printf("%d %d %c\n",a,c[i],c[i]);
66         }
67         puts(c);
68     }
69     return 0;
70 }

 

转载于:https://www.cnblogs.com/GO-NO-1/p/3614660.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值