当前位置:网站首页>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;
}
边栏推荐
- Go from entry to practice - multiple selection and timeout control (notes)
- GBase 8a OLAP分析函数 cume_dist的使用样例
- Go从入门到实战——依赖管理(笔记)
- 关于异常处理的知识整理
- A set of system to reduce 10 times the traffic pressure in crowded areas
- Is it safe to open an account and buy stocks? Who knows
- ∫(0→1) ln(1+x) / (x² + 1) dx
- Modify large online games through CE modifier
- Educational Codeforces Round 108 (Rated for Div. 2)
- Squid proxy server
猜你喜欢

MySQL usage notes 1

"Apprendre cette image" apparaît sur le Bureau win11 comment supprimer

Here are 12 commonly used function formulas for you. All used ones are good

Icml2022 | scalable depth Gaussian Markov random field

Go from introduction to actual combat - context and task cancellation (notes)

DO280OpenShift访问控制--security policy和章节实验

跟我一起AQS SOS AQS

MySQL performance optimization index function, hidden, prefix, hash index usage (2)

Knowledge sorting of exception handling

于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
随机推荐
Go from introduction to practice -- coordination mechanism (note)
Unleash the innovative power of open source database | [Gansu] opengauss meetup has come to a successful conclusion
How to delete "know this picture" on win11 desktop
At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
Scrum和看板的区别
强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”
100 important knowledge points that SQL must master: combining where clauses
Go from introduction to actual combat - package (notes)
MySQL client tools are recommended. I can't imagine that it is best to use Juran
Go从入门到实战——多态(笔记)
Flask----应用案例
Magic POI error in reading excel template file
Go从入门到实战——Context与任务取消(笔记)
Day8 ---- 云资讯项目介绍与创建
Acwing周赛57-最长连续子序列-(二分or树状数组)
动态刷新mapper看过来
oss上传调用的是哪个方法
空指针异常
What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?
Array assignment