以下是源码,亲测可用。
目前只能实现英文部分的加密和解密。
#include “mainwindow.h”
#include “ui_mainwindow.h”
#include
#include<math.h>
#include<windows.h>
#include<stdio.h>
#include
typedef long long ll;
int e, d, n;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
nums(100)
{
ui->setupUi(this);
}
int MainWindow::gcd(int a, int b) //求最大公约数
{
int c = 0;
if(a<b) qSwap(a,b);
c = b;
do
{
b = c;
c = a%b;
a = b;
}
while (c != 0);
return b;
}
int MainWindow::PrimarityTest(int a, int i) //判断i是否是素数
{
int flag=0;
for(a;a<i;a++)
{
if(i%a==0)
{
flag=1;
break;
}
}
if(flag) return 0;
return 1;
}
int MainWindow::ModularExponention(int a, int b, int n) //求a^bmodn
{
int y;
/*使用二进制平方乘法计算 pow(a,b) % n*/
y=1;
while(b != 0)
{
/*对于b中的每个1,累加y*/
if(b & 1)
y = (y*a) % n;
/*对于b中的每一位,计算a的平方*/
a = (a*a) % n;
/*准备b中的下一位*/
b = b>>1;
}
return y;
// complete this part
}
void MainWindow::extgcd(int a,int b,int& d,int& x,int& y) //获取(1/a)modb得结果
{
if(!b)
{
d=a;
x=1;
y=0;
}
else
{
extgcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
int MainWindow::ModularInverse(int a,int b) //获取(1/a)modb得结果
{
int d,x,y;
extgcd(a,b,d,x,y);
return d==1?(x+b)%b:-1;
}
void MainWindow::KeyGeneration() //获取公钥密钥
{
int p, q;
int phi_n;
do
{
do
p = rand();
while (p % 2 == 0);
}
while (!PrimarityTest(2, p));
do
{
do
q = rand();
while (q % 2 == 0);
}
while (!PrimarityTest(2, q));
n = p * q;
phi_n = (p - 1) * (q - 1);
do
e = rand() % (phi_n - 2) + 2; // 1 < e < phi_n
while (gcd(e, phi_n) != 1);
d = ModularInverse(e,phi_n);
}
int MainWindow::Encryption(int value) //加密
{
int cipher;
cipher = ModularExponention(value, e, n);
return cipher;
}
int MainWindow::Decryption(int value) //解密
{
int decipher;
decipher = ModularExponention(value, d, n);
return decipher;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_Addin_clicked() //对英文加密和解密
{
QFile file(“C:\Users\Administrator\Desktop\test.txt”);
if(!file.open(QIODevice::ReadWrite)) //以写的方式打开文件,如果文件不存在则创建,
qDebug()<<file.errorString();
KeyGeneration(); //获取公钥密钥
QString temp="C:\\Users\\Administrator\\Desktop\\ShowTest.txt";
QFile m_file(temp);
if(!m_file.open(QIODevice::ReadWrite))//以写的方式打开文件,如果文件不存在则创建,
qDebug()<<m_file.errorString();
QString ss=file.readAll();
QStringList m_lists=ss.split("\n");
int pos=m_lists.size();
//qDebug()<<"the lenghts is"<<pos<<" "<<ss;
int p=0;
while(pos>1)
{
// QByteArray buf=file.readLine();
// qDebug()<<"the array is"<<buf;
QString m_temp=m_lists.at(p);
//qDebug()<<"the content is"<<m_temp;
if(m_temp.size()>0)
{
for(int i=0;i<m_temp.size();i++)
{
char s=m_temp.at(i).toAscii();
// qDebug()<<"the every data is"<<s;
int value = toascii(s); //toascii将字符转化成对应ascall值
int m=Encryption(value); //加密输出
QString mm=QString("%1").arg(m);
ui->Show->append(mm);
}
}
else
{
}
pos--;
p++;
}
p=0;
QByteArray m_bytes;
nums=1;
while (nums>0)
{
QString s=ui->Show->toPlainText();
QString temps;
QStringList m_list = s.split("\n");
for(int i=0;i<m_list.size();i++)
{
// qDebug()<<"the temp value is"<<m_list.at(i);
temps=m_list.at(i);
m_bytes.append(Decryption(temps.toInt())); //解密
}
nums--;
}
QString m_ss=m_bytes;
ui->Show->setText(m_ss);
}
void MainWindow::on_Encryption_clicked()
{
}
void MainWindow::on_Decrypt_clicked()
{
}
void MainWindow::SetEncryption()
{
}
void MainWindow::Decrypt()
{
}
使用以上的算法对源码进行加密和解密,解密之后显示在qt的文本框上。现实的效果如下:
源码可以查看本博主的资源。