因工作需要,需要给文件加密,在网上找寻多时,找到有人写的aes的类,可以供QT使用
转载了人家的测试代码,
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TAesClass *aes = new TAesClass;
char mingwen[1024] = "helloworld";
DWORD size = strlen(mingwen);
char miwen[1024];
char jiemi[1024];
UCHAR key[1024] = "aabbcc";
UCHAR *p = key;
aes->InitializePrivateKey(16, p); //进行初始化
aes->OnAesEncrypt((LPVOID)mingwen, size, (LPVOID)miwen); //进行加密
aes->OnAesUncrypt((LPVOID)miwen, (DWORD)sizeof(miwen),(LPVOID)jiemi); //进行解密
//打印信息
qDebug() << "mingwen:" << mingwen;
qDebug() << "miwen:" << miwen;
qDebug() << "jiemi:" <<jiemi;
free(aes);
aes = 0;
return a.exec();
}
这儿首先需要设置key,然后在初始化后只要使用
aes->OnAesEncrypt((LPVOID)mingwen, size, (LPVOID)miwen); //进行加密
aes->OnAesUncrypt((LPVOID)miwen, (DWORD)sizeof(miwen),(LPVOID)jiemi); //进行解密
就可以进行加解密字符串
char mingwen[1024] = "helloworld";
就是需要加密的字符串
在此基础上,做了些改变,让他可以加密文件
void Widget::encrypt()
{
QString filename;
filename=QFileDialog::getOpenFileName(this,
tr("选择文件"),
"",
tr("Files (*.*)"));
QFile file(filename); //判断文件名
static QByteArray dat;
char *m_pUChar;
int m_iAllByte;
//传输文件
file.open(QIODevice::ReadWrite);
dat = file.readAll();
m_pUChar = dat.data();
m_iAllByte = dat.size(); //获得文件大小
qDebug() << m_iAllByte;
file.close();
TAesClass *aes = new TAesClass;
char miwen[m_iAllByte * 4];
UCHAR key[1024] = "aabbcc";
UCHAR *p = key;
aes->InitializePrivateKey(32, p); //进行初始化
int len = aes->OnAesEncrypt((LPVOID)m_pUChar, m_iAllByte, (LPVOID)miwen); //进行加密
QFile file2("1.data");
file2.open(QIODevice::WriteOnly);
file2.write(miwen ,len);
file2.close();
free(aes);
aes = 0;
}
void Widget::decrypt()
{
QString filename;
filename=QFileDialog::getOpenFileName(this,
tr("选择文件"),
"",
tr("Files (*.*)"));
QFile file(filename); //判断文件名
static QByteArray dat;
char *m_pUChar;
int m_iAllByte;
//传输文件
file.open(QIODevice::ReadWrite);
dat = file.readAll();
m_pUChar = dat.data();
m_iAllByte = dat.size(); //获得文件大小
//m_iAllByte = file->size(); //获得文件大小
qDebug() << m_iAllByte;
file.close();
TAesClass *aes = new TAesClass;
char jiemi[m_iAllByte * 3];
UCHAR key[1024] = "aabbcc";
UCHAR *p = key;
aes->InitializePrivateKey(32, p); //进行初始化
int len = aes->OnAesUncrypt((LPVOID)m_pUChar, m_iAllByte,(LPVOID)jiemi); //进行解密
qDebug() << "len = "<< len;
QFile file2("jiemi.png");
file2.open(QIODevice::WriteOnly);
file2.write(jiemi ,len);
file2.close();
}
void Widget::encrypt()用来加密文件
void Widget::decrypt()用来解密文件
AES的类也已经上传了,有需要的人去下载,地址是: