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. (转)CreateThread与_beginthread,内存泄漏为何因(原帖排版有些不好 ,所以我稍微整理下)

            在写c++代码时,一直牢记着一句话:决不应该调用CreateThread. 应该使用Visual   C++运行时库函数_beginthreadex.好像CreateThread函数就 ...

  2. jenkins SSH登录 Git配置(通过eclipse生成SSH 密钥)

    1.通过eclipse生成SSH 密钥 菜单栏的windows-->preferences-->General-->Network Connections-->SSH2--&g ...

  3. vim中快速定位到某行以及快捷删除多行

    vim filename 在命令行中直接输入 numberG 比如 100G直接定位到100行 输入 :set number即显示行号 : i,.d删除从第i行到目前所在行内容

  4. 元类 metaclass

    metaclass 类由Type创建 对象由创建 MetaClass作用 用来指定当前类由谁来创建(默认type创建). MetaClass 会被继承,如果父类指定了元类,那么子类也是由这个元类创建 ...

  5. append和extend区别

    append append方法用于在列表末尾添加新的对象 它是把添加的对象当成一个整体追加到末尾 a=[1,23,436] b=[] b.append(a) print(b)""& ...

  6. 你知道C语言为什么会有“_”(下划线)吗?

    学过C语言的都知道,变量名只能由字母.数字.下划线组成,且只能以字母或者下划线开头. 学英语时我们都学过连字符(“-”),这个东东主要用来连接单词的.那么C语言为什么不直接 用连字符,而要改用下划线呢 ...

  7. java红黑树

    从这里学了一些知识点https://blog.csdn.net/sun_tttt/article/details/65445754,感谢作者

  8. 好的博客参考之Spring

    https://blog.csdn.net/bao19901210/article/details/41724355

  9. 20145103JAVA第二次实验报告

    实验二 Java面向对象程序设计 实验内容 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握UML建模 4.熟悉S.O.L.I.D原则 5.了解设计模式 实验 ...

  10. 20145327 《Java程序设计》第九周学习总结

    20145327 <Java程序设计>第九周学习总结 教材学习内容总结 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作,开发人员无需接触底层 ...