0
点赞
收藏
分享

微信扫一扫

HDUOJ-------(1211)RSA


RSA

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

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 杭电ACM省赛集训队选拔赛之热身赛   Recommend Eddy 何为快速幂.....自己百度去...  自己根据以往经验敲的...所以自己验证去吧... 说下简单的快速幂吧....

//求 ans=a的b次幂mod n
quick_pow(int a,int b,int ans,int n)
{
while(a)
{
if(a&1)
{
ans*=b;
ans%=n;
a--;
}
else
{
b*=b;
b%=n;
b>>=1;
}
}
}


扩展欧几里得代码:

1   // ax+by=c;  c/q=k;(k=1,2,3,.....n)
2 int x,y,q;
3 void exgcd(int a ,int b)
4 {
5 if(b==0)
6 {
7 y=0,x=1,q=a;
8 }
9 else
10 {
11 int temp=x;
12 x=y;
13 y=temp-a/b*y;
14 }
15 }




简单题。。。。 代码:

HDUOJ-------(1211)RSA_javaHDUOJ-------(1211)RSA_扩展欧几里得_02

1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4 #define LL _int64
5 LL d,a,b;
6 /*ax+by=c*/
7 /*欧几里得扩展*/
8 void exgcd(LL x ,LL y)
9 {
10 if(y==0)
11 {
12 a=1,b=0,d=x;
13 }
14 else
15 {
16 exgcd(y,x%y);
17 LL temp=a;
18 a=b,b=temp-x/y*b;
19 }
20 }
21 int main()
22 {
23 LL p,q,e,len,key;
24 while(scanf("%I64d %I64d %I64d %I64d",&p,&q,&e,&len)!=EOF)
25 {
26 exgcd(e,(p-1)*(q-1));
27 while(a<0)
28 {
29 a+=(p-1)*(q-1);
30 }
31 LL n=p*q;
32 LL cnt=a;
33 while(len--)
34 {
35 scanf("%I64d",&key);
36 key%=n;
37 LL ans=1;
38 /*可以用快速幂*/
39 a=cnt;
40 while(a)
41 {
42 if(a&1)
43 {
44 ans*=key;
45 ans%=n;
46 a--;
47 }
48 else
49 {
50 key*=key;
51 key%=n;
52 a>>=1L;
53 }
54 }
55 putchar(ans);
56 }
57 putchar(10);
58 }
59 return 0;
60 }

View Code



编程是一种快乐,享受代码带给我的乐趣!!!


举报

相关推荐

0 条评论