RSA加密的简单实现【C++ Code】
来源:互联网 发布:搞笑一家人知乎 编辑:程序博客网 时间:2024/06/03 17:20
原理不再细述, 下面是简单的实现,没有使用比较大的质数, 主要是用C++不想写大数…
支持对中文的加密
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int MAX_N = 1000;wifstream wfin;wofstream wfout;//locale loc("zh_CN.UTF-8"); //Linux 下需要//拓展欧几里得 已知a,b 求满足ax + by = gcd(a, b) 的一组解int exgcd(int a, int b, int &x, int &y){ if (b == 0){ x = 1; y = 0; return a; } int ans = exgcd(b, a % b, x, y); int tmp = x; x = y; y = tmp - a / b * y; return ans;}// 暴力找出200 ~ MAX_N 的所有质数void get_Prime_Number(int *Prime, int &cnt){ cnt = 0; for (int i = 201;i < MAX_N; i+=2){ bool flag = false; for (int j = 2;(j * j) <= i; j++){ if (i % j == 0){ flag = true; break; } } if (!flag) Prime[cnt++] = i; }}//RSA初始化void RSA_Initialize(int &n, int &e, int &d){ int Prime[MAX_N], cnt = 0; get_Prime_Number(Prime, cnt); srand((unsigned)time(NULL)); int r1 = rand() % cnt; int r2 = rand() % cnt; int p1 = Prime[r1], p2 = Prime[r2]; // 随机取两个质数. n = p1 * p2; // 计算n int m = (p1 - 1) * (p2 - 1); int y; // 选择一个与n互质的元素,记为e,求得模反元素d for (int i = 3; i < m; i += 1331){ int gcd = exgcd(i, m, d, y); if (gcd == 1 && d > 0){ e = i; break; } }}// 快速幂求 a^b % cint Quick_Pow(int a, int b, int c){ int tot = 1; while (b){ if (b & 1) tot = (1LL * tot * a) % c; //可能会溢出 a = (1LL * a * a) % c; b >>= 1; } return tot;}//RSA 加密void RSA_Encrypt(int e, int n, wstring wstr){ int len = wstr.size(); for (int i = 0; i < len; i++){ wfout << Quick_Pow(wstr[i], e, n) << " "; //输出密文 } wfout << endl;}//RSA 解密void RSA_Decrypt(int d, int n, wstring wstr){ wstringstream wss(wstr); int x; while (wss >> x){ wfout << (wchar_t)Quick_Pow(x, d, n); //输出解密后的原文对应字符 } wfout << endl;}int main(){ wstring wstr; wfin.open("Plain_text.txt"); wfout.open("Cipher_text.txt"); //wfin.imbue(loc); //Linux 下需要 int n, e, d; RSA_Initialize(n, e, d); printf("Public Key (e, n) : (%d, %d).\n",e, n); printf("Private Key (d, n) : (%d, %d).\n\n",d, n); int Case = 0; while (getline(wfin, wstr)){ printf("Line %d data is being encrypted.\n",++Case); RSA_Encrypt(e, n, wstr); } wfin.close(); wfout.close(); wfin.open("Cipher_text.txt"); wfout.open("Decrypt_text.txt"); //wfout.imbue(loc); //Linux 下需要 Case = 0; while (getline(wfin, wstr)){ printf("Line %d data is being decrypted.\n",++Case); RSA_Decrypt(d, n, wstr); } wfin.close(); wfout.close();}
阅读全文
0 0
- RSA加密的简单实现【C++ Code】
- Javascript到PHP RSA加密通讯的简单实现
- Java实现简单AES RSA加密
- openssl C语言编码实现rsa加密
- Golang1.7.3简单的rsa加密封装
- RSA加密学习的简单例子
- RSA简单加密解密
- RSA加密 简单介绍
- Android RSA加密解密功能的实现
- RSA加密解密的实现详解_JAVA
- RSA加密算法的简单实现
- RSA的简单模拟实现
- RSA算法的简单实现
- RSA公钥私钥的简单实现
- Vc6.0下配置 Crypto++ ------用Crypto Library 实现简单的RSA加密
- 一个简单地C语言程序展示RSA加密原理
- Java 实现Rsa 加密
- RSA加密,java实现
- php 常见配置信息报错 url_init()
- UIButton 的 backgroundImage 和 image 的填充模式
- Hadoop—MapReduce
- 第2章 模型评估与选择学习笔记
- Springboot+Mybatis批量导入多条数据
- RSA加密的简单实现【C++ Code】
- 1046. 划拳(15)
- js对象转换
- 神经网络中激活函数的真正意义
- OpenCV实践之路——矩形图片的对角线分割
- opencv was not found in the pkg-config search path
- Java序列化与反序列化
- PAT Advanced 1044. Shopping in Mars (25)
- 分析器错误消息: 无法识别的属性“targetFramework”。请注意属性名称区分大小写。