HDU RSA 扩展欧几里得
> 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.
7716 7746 7497 126 8486 4708 7746 623 7298 7357 3239
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<cstring>
- using namespace std;
- typedef long long LL;
- /*
- 题目要求
- > 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
- 知道 P Q E
- gcd(a,b) == 1 等价于 存在x,y a*x+b*y==1
- 存在x,y
- e*x + F(n)*y = 1
- d*e%F(n) = 1%F(n)
- d*e + F(n)*y = 1; 通过求逆元方法解出 d 即可
- */
- LL p,q,e,l;
- LL ex_gcd(LL a,LL b,LL &x,LL &y)
- {
- if(b==)
- {
- x = ;
- y = ;
- return a;
- }
- LL ans = ex_gcd(b,a%b,x,y);
- LL tmp = x;
- x = y;
- y = tmp - a/b*x;
- return ans;
- }
- LL cal(LL a,LL b,LL c)
- {
- LL x=,y=;
- LL gcd = ex_gcd(a,b,x,y);
- if(c%gcd!=) return -;
- x *= c/gcd;
- b /= gcd;
- if(b<) b = -b;
- LL ans = x%b;
- if(ans<) ans+=b;
- return ans;
- }
- int main()
- {
- while(scanf("%lld%lld%lld%lld",&p,&q,&e,&l)!=EOF)
- {
- LL fn = (p-)*(q-),n = p*q;
- LL d = cal(e,fn,);
- LL tmp,ans;
- for(int i=;i<l;i++)
- {
- scanf("%lld",&tmp);
- tmp %= n;
- ans = ;
- for(int j=;j<d;j++)
- ans = (ans*tmp)%n;
- printf("%c",ans%n);
- }
- printf("\n");
- }
- }
HDU RSA 扩展欧几里得的更多相关文章
- HDU 5114 扩展欧几里得
题目大意:给你两个球的坐标 他们都往(1, 1)这个方向以相同的速度走,问你他们在哪个位置碰撞. 思路:这种题目需要把x方向和y方向分开来算周期,两个不同周期需要用扩展欧几里得来求第一次相遇. #in ...
- hdu 2669(扩展欧几里得)
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 4180 扩展欧几里得
RealPhobia Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2669 扩展欧几里得(裸)
#include<stdio.h> #include<iostream> #define ll __int64 ll gcd(ll a,ll b,ll &x,ll &a ...
- 扩展欧几里得 hdu 1576
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 不知道扩展欧几里得的同学可以参考:https://blog.csdn.net/zhjchengf ...
- URAL 1141. RSA Attack(欧拉定理+扩展欧几里得+快速幂模)
题目链接 题意 : 给你n,e,c,并且知道me ≡ c (mod n),而且n = p*q,pq都为素数. 思路 : 这道题的确与题目名字很相符,是个RSA算法,目前地球上最重要的加密算法.RSA算 ...
- hdu 5512 Pagodas 扩展欧几里得推导+GCD
题目链接 题意:开始有a,b两点,之后可以按照a-b,a+b的方法生成[1,n]中没有的点,Yuwgna 为先手, Iaka后手.最后不能再生成点的一方输: (1 <= n <= 2000 ...
- hdu 1573 A/B (扩展欧几里得)
Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973)= 1). Input 数据的第一行 ...
- hdu 1576 A/B 【扩展欧几里得】【逆元】
<题目链接> <转载于 >>> > A/B Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)( ...
随机推荐
- ACM_错排(递推dp)
RPG的错排 Time Limit: 2000/1000ms (Java/Others) Problem Description: 今年暑假GOJ集训队第一次组成女生队,其中有一队叫RPG,但做为集训 ...
- yii框架下jquery在ajax更新后失效问题
解决方案,以live的形式重新绑定一次, /***回复隐藏收起效果***/ $(".btn-reply").live('click',function(event){ var da ...
- 经典矩阵dp寻找递增最大长度
竖向寻找矩阵最大递增元素长度,因为要求至少一列为递增数列,那么每行求一下最大值就可以作为len[i]:到i行截止的最长的递增数列长度. C. Alyona and Spreadsheet time l ...
- iOS基础笔试题 - 集锦一
前言 下文转载自https://mp.weixin.qq.com/s?__biz=MzA4ODk0NjY4NA==&mid=454115946&idx=1&sn=c7f1b50 ...
- Dota2团战实力蔑视人类,解剖5只“AI英雄”
去年,OpenAI 在 DOTA 的 1v1 比赛中战胜了职业玩家 Dendi,而在距离进阶版 OpenAI Five 系统战胜人类业余玩家不过一个月的时间,今天凌晨,它又以 2:1 的战绩再次完成对 ...
- ImmutableJS
引用大神的一句话:(具体是谁自己问度娘) Shared mutable state is the root of all evil(共享的可变状态是万恶之源) -- Pete Hunt JavaS ...
- JMeter在linux上分布式压测环境配置(一)
环境配置 一.在Linux服务器先安装SDK 1.先从官网下载jdk1.8.0_131.tar.gz,l(linux版本,32位,64位根据系统来判断) 2.在/usr/目录下创建java文件夹,(当 ...
- 梦想CAD控件安卓图层
新建图层 CAD中我们设置好图层后除了我们平常的绘图时选择线段的颜色,线型,线宽等作用,而且我们还可以在出图时选择性显示图形,冻结图形,已达到我们想要的效果. 实现代码说明: //增加一个图层 参数为 ...
- vue启动
首先在终端terminal连上npm 镜像库 npm config set registry https://registry.npm.taobao.orgnpm installnpm run loc ...
- css 实现鼠标滑过流光效果
来划我啊 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...