|

楼主 |
发表于 2022-5-3 19:21:50
|
显示全部楼层
1、RSA.h
- #pragma once
- #pragma once
- #include <string>
- #include <vector>
- struct Key
- {
- long pkey;
- //私钥(ekey, pkey): (e,n)
- long ekey;
- //公钥(dkey, pkey): (d, n)
- long dkey;
- };
- class RSA
- {
- public:
- RSA();
- Key getKey()
- {
- return _key;
- }
- void ecrept(const char* plain_file_in, const char* ecrept_file_out,
- long ekey, long pkey);//加密
- void decrept(const char* ecrept_file_in, const char* plain_file_out,
- long dkey, long pkey);//解密
- std::vector<long> ecrept(std::string& str_in, long ekey, long pkey);//字符串加密
- std::string decrept(std::vector<long>& ecrept_str, long dkey, long pkey);//解密为字符串
- void printInfo(std::vector<long>& ecrept_str);//打印信息
- private:
- long ecrept(long msg, long key, long pkey);//加密单个信息
- long produce_prime();//产生素数
- bool is_prime(long prime);//是否为素数
- void produce_keys();
- long produce_pkey(long prime1, long prime2);//计算pq乘积n
- long produce_orla(long prime1, long prime2);//计算欧拉函数φ(n)
- long produce_ekey(long orla);//产生e
- long produce_gcd(long ekey, long orla);//产生最大公约数
- long produce_dkey(long ekey, long orla);//产生d
- private:
- Key _key;
- };
复制代码 2、RSA.cpp
- #include"RSA.h"
- #include<time.h>
- #include<math.h>
- #include<iostream>
- #include<fstream>
- RSA::RSA()
- {
- produce_keys();
- }
- void RSA::ecrept(const char* plain_file_in, const char* ecrept_file_out,
- long ekey, long pkey)//加密
- {
- std::ifstream fin(plain_file_in);
- std::ofstream fout(ecrept_file_out, std::ofstream::app);
- if (!fin.is_open())
- {
- std::cout << "open file failed" << std::endl;
- return;
- }
- const int NUM = 256;
- char buffer[NUM];
- long buffer_out[NUM];
- int curNum;
- while (!fin.eof())
- {
- fin.read(buffer, NUM);
- curNum = fin.gcount();
- for (int i = 0; i < curNum; ++i)
- {
- buffer_out[i] = ecrept(buffer[i], ekey, pkey);
- }
- fout.write((char*)buffer_out, curNum * sizeof(long));
- }
- fin.close();
- fout.close();
- }
- void RSA::decrept(const char* ecrept_file_in, const char* plain_file_out,
- long dkey, long pkey)//解密
- {
- std::ifstream fin(ecrept_file_in);
- std::ofstream fout(plain_file_out, std::ofstream::app);
- if (!fin.is_open())
- {
- std::cout << "open file failed" << std::endl;
- return;
- }
- const int NUM = 256;
- long buffer[NUM];
- char buffer_out[NUM];
- int curNum;
- while (!fin.eof())
- {
- fin.read((char*)buffer, NUM * sizeof(long));
- curNum = fin.gcount() / sizeof(long);
- for (int i = 0; i < curNum; ++i)
- {
- buffer_out[i] = ecrept(buffer[i], dkey, pkey);
- }
- fout.write(buffer_out, curNum);
- }
- fin.close();
- fout.close();
- }
- std::vector<long> RSA::ecrept(std::string& str_in, long ekey, long pkey)//字符串加密
- {
- std::vector<long> vecout;
- size_t sz = str_in.size();
- for (const auto& e : str_in)
- {
- vecout.push_back(ecrept(e, ekey, pkey));
- }
- return vecout;
- }
- std::string RSA::decrept(std::vector<long>& ecrept_str, long dkey, long pkey)//解密为字符串
- {
- std::string strout;
- for (const auto& e : ecrept_str)
- {
- strout.push_back(ecrept(e, dkey, pkey));
- }
- return strout;
- }
- void RSA::printInfo(std::vector<long>& ecrept_str)//打印信息
- {
- for (const auto& e : ecrept_str)
- {
- std::cout << e << ' ';
- }
- std::cout << std::endl;
- }
- //加密解密算法
- //模幂运算(a^b)%c
- long RSA::ecrept(long msg, long key, long pkey)
- {
- long msg_out = 1;
- //A0:a^(2^0) % c = a^1 = a%c
- long a = msg;
- long b = key;
- int c = pkey;
- while (b)
- {
- if (b & 1)
- //msg_out = (A0 * A1 ...Ai ... An) % c
- msg_out = (msg_out * a) % c;
- b >>= 1;
- //Ai = (A(i - 1) * A(i - 1)) % c
- a = (a * a) % c;
- }
- return msg_out;
- }
- long RSA::produce_prime()
- {
- srand(time(nullptr));
- long prime = 0;
- while (1)
- {
- prime = rand() % 50 + 2;
- if (is_prime(prime))
- break;
- }
- return prime;
- }
- bool RSA::is_prime(long prime)
- {
- if (prime < 2)
- return false;
- for (int i = 2; i < sqrt(prime); ++i)
- {
- if (prime % i == 0)
- return false;
- }
- return true;
- }
- void RSA::produce_keys()
- {
- long prime1 = produce_prime();
- long prime2 = produce_prime();
- while (prime1 == prime2)
- prime2 = produce_prime();
- _key.pkey = produce_pkey(prime1, prime2);
- long orla = produce_orla(prime1, prime2);
- _key.ekey = produce_ekey(orla);
- _key.dkey = produce_dkey(_key.ekey, orla);
- }
- long RSA::produce_pkey(long prime1, long prime2)
- {
- return prime1 * prime2;
- }
- long RSA::produce_orla(long prime1, long prime2)
- {
- return (prime1 - 1) * (prime2 - 1);
- }
- //随机选择一个整数e,条件是1 < e < φ(n),且e与φ(n) 互质
- long RSA::produce_ekey(long orla)
- {
- long ekey;
- srand(time(nullptr));
- while (1)
- {
- ekey = rand() % orla;
- if (ekey > 1 && produce_gcd(ekey, orla) == 1)
- break;
- }
- return ekey;
- }
- //求最大公约数
- long RSA::produce_gcd(long ekey, long orla)
- {
- //gcd(a, b)------gcd(b ,a%b)
- long ret;
- while (ret = ekey % orla)
- {
- ekey = orla;
- orla = ret;
- }
- return orla;
- }
- long RSA::produce_dkey(long ekey, long orla)
- {
- //(dkey * ekey) % orla == 1
- long dkey = orla / ekey;//dkey最小为orla / ekey,才能保证dkey * ekey与orla相等
- //只有dkey * ekey比orla大才能有余数为1
- while (1)
- {
- if ((dkey * ekey) % orla == 1)
- break;
- ++dkey;
- }
- return dkey;
- }
复制代码 3、Test.cpp
- #include"RSA.h"
- #include<iostream>
- void test()
- {
- RSA rsa;
- Key key = rsa.getKey();
- std::string strin;
- while (1)
- {
- std::cout << "输入要加密的信息:" << std::endl;
- std::cin >> strin;
- std::vector<long>strecrept = rsa.ecrept(strin, key.ekey, key.pkey);
- std::string strout = rsa.decrept(strecrept, key.dkey, key.pkey);
- std::cout << "加密后的信息:" << std::endl;
- rsa.printInfo(strecrept);
- std::cout << "解密后的信息:" << std::endl;
- std::cout << strout << std::endl;
- std::cout << std::endl;
- }
- }
- int main()
- {
- test();
- system("pause");
- return 0;
- }
复制代码
|
|