用c++实现详细的有界面的RSA算法
时间: 2024-03-24 09:35:58 浏览: 142
要用C++实现带有界面的RSA算法,可以使用Qt框架开发GUI界面,并在其中调用C++实现的RSA算法。
以下是一个详细的示例代码,其中包含了RSA算法的加密和解密函数,以及Qt界面的实现:
RSA.h
```cpp
#ifndef RSA_H
#define RSA_H
#include <string>
class RSA {
public:
RSA();
void generateKeys(int numBits);
std::string encrypt(std::string plainText);
std::string decrypt(std::string cipherText);
int getPublicKey();
int getModulus();
private:
int p, q, n, e, d;
int gcd(int a, int b);
int modPow(int base, int exp, int modulus);
int modInverse(int a, int m);
bool isPrime(int num);
};
#endif // RSA_H
```
RSA.cpp
```cpp
#include <cstdlib>
#include <ctime>
#include <cmath>
#include "RSA.h"
RSA::RSA() {
p = 0;
q = 0;
n = 0;
e = 0;
d = 0;
}
// 生成RSA密钥对
void RSA::generateKeys(int numBits) {
srand(time(NULL));
// 随机生成两个大质数
p = rand() % (1 << (numBits / 2 - 1)) + (1 << (numBits / 2 - 2)) + 1;
while (!isPrime(p)) {
p += 2;
}
q = rand() % (1 << (numBits - numBits / 2 - 1)) + (1 << (numBits - numBits / 2 - 2)) + 1;
while (!isPrime(q)) {
q += 2;
}
// 计算n和欧拉函数
n = p * q;
int phi = (p - 1) * (q - 1);
// 随机生成e,使得e和phi互质
e = rand() % (phi - 2) + 2;
while (gcd(e, phi) != 1) {
e++;
}
// 计算d,使得d*e % phi = 1
d = modInverse(e, phi);
}
// RSA加密
std::string RSA::encrypt(std::string plainText) {
std::string cipherText = "";
for (int i = 0; i < plainText.length(); i++) {
int c = (int)plainText[i];
c = modPow(c, e, n); // 加密
cipherText += (char)c;
}
return cipherText;
}
// RSA解密
std::string RSA::decrypt(std::string cipherText) {
std::string plainText = "";
for (int i = 0; i < cipherText.length(); i++) {
int c = (int)cipherText[i];
c = modPow(c, d, n); // 解密
plainText += (char)c;
}
return plainText;
}
// 获取公钥
int RSA::getPublicKey() {
return e;
}
// 获取模数
int RSA::getModulus() {
return n;
}
// 判断一个数是否为质数
bool RSA::isPrime(int num) {
if (num < 2) {
return false;
}
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
// 计算两个数的最大公约数
int RSA::gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// 计算一个数的模幂
int RSA::modPow(int base, int exp, int modulus) {
base %= modulus;
int result = 1;
while (exp > 0) {
if (exp & 1) {
result = (result * base) % modulus;
}
base = (base * base) % modulus;
exp >>= 1;
}
return result;
}
// 计算一个数的模逆元
int RSA::modInverse(int a, int m) {
int m0 = m, t, q;
int x0 = 0, x1 = 1;
if (m == 1) {
return 0;
}
while (a > 1) {
q = a / m;
t = m;
m = a % m, a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
if (x1 < 0) {
x1 += m0;
}
return x1;
}
```
mainwindow.h
```cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "RSA.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_generateKeysButton_clicked();
void on_encryptButton_clicked();
void on_decryptButton_clicked();
private:
Ui::MainWindow *ui;
RSA rsa;
};
#endif // MAINWINDOW_H
```
mainwindow.cpp
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QInputDialog>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
// 生成RSA密钥对
void MainWindow::on_generateKeysButton_clicked()
{
int numBits = ui->numBitsSpinBox->value();
rsa.generateKeys(numBits);
QMessageBox::information(this, tr("密钥"), tr("p=%1\nq=%2\ne=%3\nd=%4").arg(rsa.p).arg(rsa.q).arg(rsa.e).arg(rsa.d));
ui->publicKeyLineEdit->setText(QString::number(rsa.getPublicKey()));
ui->modulusLineEdit->setText(QString::number(rsa.getModulus()));
}
// RSA加密
void MainWindow::on_encryptButton_clicked()
{
QString plainText = ui->plainTextEdit->toPlainText();
if (plainText.isEmpty()) {
QMessageBox::warning(this, tr("错误"), tr("请输入明文"));
return;
}
QString cipherText = QString::fromStdString(rsa.encrypt(plainText.toStdString()));
ui->cipherTextEdit->setText(cipherText);
}
// RSA解密
void MainWindow::on_decryptButton_clicked()
{
QString cipherText = ui->cipherTextEdit->toPlainText();
if (cipherText.isEmpty()) {
QMessageBox::warning(this, tr("错误"), tr("请输入密文"));
return;
}
int d = QInputDialog::getInt(this, tr("输入d"), tr("请输入d:"));
int n = QInputDialog::getInt(this, tr("输入n"), tr("请输入n:"));
QString plainText = QString::fromStdString(rsa.decrypt(cipherText.toStdString(), d, n));
ui->plainTextEdit->setText(plainText);
}
```
其中,RSA类包含了生成RSA密钥对、RSA加密和RSA解密等函数,以及一些辅助函数,例如判断一个数是否为质数、计算两个数的最大公约数、计算一个数的模幂和计算一个数的模逆元等。MainWindow类则是Qt界面的主要实现,其中包含了生成密钥对、RSA加密和RSA解密等按钮的点击事件处理函数。
阅读全文