当前位置:网站首页>qt base64加解密
qt base64加解密
2022-06-27 19:33:00 【东方忘忧】
这里提供两种加解密的方法。
第一种方法:使用QByteArray的toBase64和fromBase64来实现。
第二种方法:使用base64.cpp文件中的base64_encode和base64_decode来实现。下载地址
代码示例如下:
#ifndef WIDGET_H
#define WIDGET_H
#include <QtWidgets>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
QPushButton* m_encryptbtn;
QPushButton* m_decryptbtn;
QLineEdit* m_encryptedit;
QLineEdit* m_decryptedit;
};
#endif // WIDGET_H
.cpp
#include "widget.h"
#include "ui_widget.h"
#include "mycrypto.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
m_encryptbtn = new QPushButton("encrypt",this);
m_decryptbtn = new QPushButton("decrypt",this);
m_decryptedit = new QLineEdit(this);
m_encryptedit = new QLineEdit(this);
QGridLayout* lay = new QGridLayout(this);
lay->addWidget(m_encryptedit,0,0,1,1);
lay->addWidget(m_decryptedit,0,1,1,1);
lay->addWidget(m_encryptbtn,1,0,1,1);
lay->addWidget(m_decryptbtn,1,1,1,1);
this->setLayout(lay);
connect(m_encryptbtn,&QPushButton::clicked,this,[=](){
#if 0 //方法一
m_decryptedit->setText(m_encryptedit->text().toLocal8Bit().toBase64());
#else //方法二
m_decryptedit->setText(MyCrypto::encrypt(m_encryptedit->text().toStdString()).data());
#endif
});
connect(m_decryptbtn,&QPushButton::clicked,this,[=](){
#if 0
m_encryptedit->setText(QByteArray::fromBase64(m_decryptedit->text().toLocal8Bit()));
#else
m_encryptedit->setText(MyCrypto::decrypt(m_decryptedit->text().toStdString()).data());
#endif
});
}
Widget::~Widget()
{
delete ui;
}
.h
#ifndef MYCRYPTO_H
#define MYCRYPTO_H
#include <QString>
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <QDataStream>
class MyCrypto
{
public:
static const std::string encrypt(const std::string & orignal);
static const std::string decrypt(const std::string & orignal);
};
#endif // MYCRYPTO_H
.cpp
#include "mycrypto.h"
#include <QString>
#include <QDebug>
#include <iostream>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cctype>
#include <string>
#include <base64.h>
using namespace std;
/**
* @brief MyCrypto::encrypt 加密
* @param orignal
* @return
*/
const std::string MyCrypto::encrypt(const std::string &orignal)
{
std::string encoded = base64_encode(reinterpret_cast<const unsigned char*>(orignal.c_str()), orignal.length());
return encoded;
}
/**
* @brief MyCrypto::decrypt 解密
* @param orignal
* @return
*/
const std::string MyCrypto::decrypt(const std::string &orignal)
{
std::string decoded = base64_decode(orignal);
return decoded;
}
边栏推荐
- Use the storcli tool to configure raid. Just collect this article
- SQL必需掌握的100个重要知识点:IN 操作符
- Icml2022 | scalable depth Gaussian Markov random field
- 01-Golang-环境搭建
- "Apprendre cette image" apparaît sur le Bureau win11 comment supprimer
- String类的常用方法
- Modify large online games through CE modifier
- Educational Codeforces Round 108 (Rated for Div. 2)
- Knowledge sorting of exception handling
- OpenSSL 编程 二:搭建 CA
猜你喜欢

Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer

图解基于AQS队列实现的CountDownLatch和CyclicBarrier

富文本 考试 填空题

Express e stack - small items in array

Save method of JPA stepping pit series

Codeforces Round #719 (Div. 3)

Go从入门到实战——行为的定义和实现(笔记)

今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献

Bit. Store: long bear market, stable stacking products may become the main theme

Icml2022 | scalable depth Gaussian Markov random field
随机推荐
Golang 使用正则来匹配出子字符串函数
Quick excel export
Go从入门到实战——依赖管理(笔记)
Express e stack - small items in array
Unleash the innovative power of open source database | [Gansu] opengauss meetup has come to a successful conclusion
Go从入门到实战——仅执行一次(笔记)
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
oss上传调用的是哪个方法
MySQL performance optimization index function, hidden, prefix, hash index usage (2)
OpenSSL 编程 一:基本概念
Educational Codeforces Round 108 (Rated for Div. 2)
AI painting minimalist tutorial
Go从入门到实战——仅需任意任务完成(笔记)
Special training of guessing game
安装gatewayworker之后启动start.php
After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"
Codeforces Round #716 (Div. 2)
100 important knowledge points that SQL must master: retrieving data
Flask----应用案例
01-Golang-环境搭建