URAL 1141. RSA Attack RSA加密算法
来源:互联网 发布:大数据专业的就业前景 编辑:程序博客网 时间:2024/05/10 20:27
题目来源:URAL 1141. RSA Attack
题意:给你e n c 并且有m^e = c(mod n) 求 m
思路:首先学习RSA算法 here
过程大致是
1.发送的信息是m
2.随机选择两个质数 p和q, n = q*p, n的欧拉函数值φ(n)= (p-1)*(q-1)这个需要证明
3.选择一个与φ(n)互质的并且小于φ(n)的数e, 计算c = m^e(mod n)
4.发送c
5解密 求e的逆元d 逆元就是2个数乘一下在mod一个数等于1 这里就是e*d = 1(mod φ(n))
求逆元用扩展欧几里德或者直接求快速幂
6.计算c^d(mod n) 就是m
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;//返回a^p mod n 快速幂int pow_mod(int a, int p, int n){int ans = 1;while(p){if(p&1){ans *= a;ans %= n;}a *= a;a %= n;p >>= 1;}return ans;}bool prime(int x){for(int i = 2; i*i <= x; i++){if(x%i == 0)return false;}return true;}int main(){int T;scanf("%d", &T);while(T--){int e, n, c;scanf("%d %d %d", &e, &n, &c);int p, q;for(int i = 2; i*i <= n; i++){if(n%i == 0 && prime(i) && prime(n/i)){p = i;q = n/i;break;}}int x = (p-1)*(q-1), y = x;for(int i = 2; i*i <= n; i++){if(x % i == 0){y = y / i * (i-1);while(x % i == 0)x /= i;}}if(x > 1)y = y / x * (x-1);int inv = pow_mod(e, y-1, (p-1)*(q-1));printf("%d\n", pow_mod(c, inv, n));}return 0;}
0 0
- URAL 1141. RSA Attack RSA加密算法
- Ural 1141. RSA Attack 扩展欧几里得算法
- RUAL 1141. RSA Attack
- RSA 加密算法
- RSA加密算法
- rsa加密算法
- RSA加密算法
- Rsa加密算法
- RSA加密算法
- RSA加密算法
- RSA加密算法
- rsa加密算法
- 加密算法:RSA
- RSA加密算法
- RSA加密算法
- RSA加密算法
- RSA 加密算法
- RSA 加密算法
- 康益明爱崔紫娟-很爱很爱
- redis 的两种持久化方式及原理
- 我爱你崔紫娟——康益明
- SQL Server导入导出excel及常见问题
- ubuntu下python,w3af开发环境配置及工程搭建
- URAL 1141. RSA Attack RSA加密算法
- 3.5 访问者模式(5.11)
- MFC文件的操作
- 构建iOS持续集成平台(一)——自动化构建和依赖管理
- C#中的DataSet、string、DataTable 、对象转换成Json
- 崔紫娟我爱你
- 爱崔紫娟的康益明
- 如何避免死锁
- 康益明爱崔紫娟