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. [golang note] 流程控制

    流程控制 • 流程控制语句作用 ▪ 选择:根据条件跳转到不同的执行序列. ▪ 循环:根据条件反复执行某个序列. ▪ 跳转:据条件返回到某执行序列. • 流程控制语句类型 ▪ 条件语句:关键字为if.e ...

  2. std::bind

    参考资料 • cplusplus.com:http://www.cplusplus.com/reference/functional/bind/ • cppreference.com:http://e ...

  3. springboot 整合 CXF 版本异常 java.lang.NoClassDefFoundError:ServletRegistrationBean

    在使用SpringBoot 项目整合webservice组件 CXF的时候,在启动时,抛出异常如下,查阅资料初步判断为版本问题.升级到高版本后正常启动. cxf 刚开始使用版本  3.1.7 后更新为 ...

  4. vs git 推送远程会失败.

    Gitblit使用admin创建版本库.   使用VS无法推送到远程服务器. VS 2017 Git failed with a fatal error 可能是帐号不应不上.   本地上帐号与服务器的 ...

  5. SpringCloud Config Server中{application}等占位符使用场景设置默认拉去分支

    Spring Cloud Config服务器支持一个Git仓库URL,其中包含{application}和{profile}(以及{label})的占位符. 1.各个占位符所代表的含义 applica ...

  6. Django restful Framework 之序列化与反序列化

    1. 首先在已建好的工程目录下新建app命名为snippets,并将snippets app以及rest_framework app加到工程目录的 INSTALLED_APPS 中去,具体如下: IN ...

  7. cdoj1633 去年春恨却来时,落花人独立,微雨燕双飞

    地址:http://acm.uestc.edu.cn/#/problem/show/1633 题目: 去年春恨却来时,落花人独立,微雨燕双飞 Time Limit: 3000/1000MS (Java ...

  8. AndroidManifest.xml中的注册组件

    界面跳转时Activity的识别方法有两种:第一种,通过name 第二种,通过<intent-filter> 通过配置文件中配置<intent-filter>来实现Activi ...

  9. linux上使用wget下载文件

    首次安装的centos操作系统是没有安装wget的,所以首先需要先安装wget,然后才能使用wget下载文件. 1.第一步,保证centos能正常连网.使用命令  :yum -y install wg ...

  10. mysql服务解压版的安装(5.7)

    推荐博客: https://www.cnblogs.com/LxyXY/p/7708016.html