前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDUOJ-------(1211)RSA

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

作者头像
Gxjun
发布2018-03-21 13:29:08
6000
发布2018-03-21 13:29:08
举报
文章被收录于专栏:mlml

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) = me mod n When you want to decrypt data, use this method : M = D(c) = cd 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

何为快速幂.....自己百度去...  自己根据以往经验敲的...所以自己验证去吧...

说下简单的快速幂吧....

代码语言:javascript
复制
//求 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;
  }
}
}

扩展欧几里得代码:

代码语言:javascript
复制
 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     }

简单题。。。。

代码:

代码语言:javascript
复制
 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 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-10-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • RSA
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档