RSA

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

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
 
 
解密方式 M=c^d mod n
c就是题目中给的数字,n=p*q,现在关键是求d,由  d*e≡1 (mod fn)  fn=(p-1)*(q-1),gcd(e,fn)=1,易得d=e-1,只要求出e  mod fn的逆元即可。
此逆元不能取模fn又很大,所以费马小定理不是很实用,这里采用拓展欧几里得求逆元。
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define lld long long
void exGcd (lld a, lld b, lld &d, lld &x, lld &y)
{
    if (b == 0)
    {
        x = 1 ;
        y = 0 ;
        d = a ;
        return ;
    }
    exGcd (b, a%b, d, x, y) ;
    lld tmp = x ;
    x = y ;
    y = tmp - a/b*y ;
}
LL qpow(LL a,LL b,LL c)
{
    LL r=1;
    while(b){
        if(b&1) r=r*a%c;
        a=a*a%c;
        b>>=1;
    }
    return r;
}
int main()
{
    LL p,q,e,n,fn,l;
    lld d,x,y;
    while(cin>>p>>q>>e>>l){n=p*q;
            fn=(p-1)*(q-1);
            exGcd(e,fn,d,x,y);
            x=(x%fn+fn)%fn;
        for(int i=1;i<=l;++i){
            LL num;
            scanf("%lld",&num);
            printf("%c",qpow(num,x,n));
        }cout<<endl;
    }
    return 0;
}

 
 
 

hdu 1211 逆元的更多相关文章

  1. HDU 1211 EXGCD

    EXGCD的模板水题 RSA算法给你两个大素数p,q定义n=pq,F(n)=(p-1)(q-1) 找一个数e 使得(e⊥F(n)) 实际题目会给你e,p,q计算d,$de \mod F(n) = 1$ ...

  2. hdu 1211 RSA (逆元)

    RSA Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  3. HDU 4828 逆元+catalan数

    Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  4. HDU 5651 逆元

    xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  5. hdu 5685(逆元)

    Problem A Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  6. HDU 1211

    水.模拟即可.使用EXGCD求逆元 #include <iostream> #include <cstdio> #include <cstring> #includ ...

  7. hdu 1211 RSA

    // 表示题目意思我是理解了蛮久 英语太水了 //首先这是解密公式 m=c^d mod n// 给你 p q e 然后 n=p*q fn=(p-1)*(q-1)// 给你 e,根据公式 e*d mod ...

  8. hdu 1576(逆元)

    A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  9. HDU 5976 数学,逆元

    1.HDU 5976 Detachment 2.题意:给一个正整数x,把x拆分成多个正整数的和,这些数不能有重复,要使这些数的积尽可能的大,输出积. 3.总结:首先我们要把数拆得尽可能小,这样积才会更 ...

随机推荐

  1. matplotlib中的颜色及线条控制

    出自 http://www.cnblogs.com/darkknightzh/p/6117528.html 参考网址: http://stackoverflow.com/questions/22408 ...

  2. matplotlib绘制柱状图

    参考自Matplotlib Python 画图教程 (莫烦Python)(11)_演讲•公开课_科技_bilibili_哔哩哔哩 https://www.bilibili.com/video/av16 ...

  3. 安卓和ios的区别

    安卓不闪退,会卡死,有几率复活,也有可能要强制重启,iOS默认闪退,强制重启的几率小很多. 总的来说,如果要深层次挖掘Android的漏洞就要明白linux内核安全,如果要挖身深层次挖掘iOS的漏洞就 ...

  4. iOS App 上架(Analysis 工具使用)

    随着iOS开发的流行,针对iOS开发涉及的方方面面,早有一些公司提供了专门的解决方案或工具.这些解决方案或工具包括:用户行为统计工具(友盟,Flurry,Google Analytics等), App ...

  5. Xcode插件开发案例教程

    引言 在平时开发过程中我们使用了很多的Xcode插件,虽然官方对于插件制作没有提供任何支持,但是加载三方的插件,默认还是被允许的.第三方的插件,存放在 ~/Library/Application Su ...

  6. Look for this newest GS Jordan 6 Floral

    Named 'Bulls Over Broadway' and 'Gym Red', the most recent variation from the New Jordans 2015 is fo ...

  7. XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem K. Piecemaking

    题目:Problem K. PiecemakingInput file: standard inputOutput file: standard outputTime limit: 1 secondM ...

  8. NC学习笔记

    NC基础技术  一.NC开发基本概念        1.单据:一般是由表头,表体,表位组成.                   表头:一般是一些主信息,例如日期,项目等               ...

  9. Spring MVC 知识总结

    参考文章:http://www.oschina.net/question/84460_9608 孔浩视频 1. 几个关键类: RequestMappingHandlerMapping 和 Reques ...

  10. Cloudera Manager安装之时间服务器和时间客户端(二)

    福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号:   大数据躺过的坑      Java从入门到架构师      人工智能躺过的坑         Java全栈大联盟   ...