当前位置:网站首页>QT base64 encryption and decryption
QT base64 encryption and decryption
2022-06-27 21:52:00 【Oriental forgetfulness】
Two encryption and decryption methods are provided here .
The first method : Use QByteArray Of toBase64 and fromBase64 To achieve .
The second method : Use base64.cpp In the document base64_encode and base64_decode To achieve . Download address
The code example is as follows :
#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 // Method 1
m_decryptedit->setText(m_encryptedit->text().toLocal8Bit().toBase64());
#else // Method 2
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 encryption
* @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 Decrypt
* @param orignal
* @return
*/
const std::string MyCrypto::decrypt(const std::string &orignal)
{
std::string decoded = base64_decode(orignal);
return decoded;
}
边栏推荐
猜你喜欢

Burp suite遇到的常见问题

Go从入门到实战——Panic和recover(笔记)

∫(0→1) ln(1+x) / (x ² + 1) dx

跟我一起AQS SOS AQS

【MySQL】数据库函数通关教程下篇(窗口函数专题)

Go from introduction to actual combat -- channel closing and broadcasting (notes)

JVM memory structure when creating objects

Codeforces Global Round 14

Go从入门到实战——接口(笔记)
![[leetcode] dynamic programming solution partition array i[red fox]](/img/b2/df87c3138c28e83a8a58f80b2938b8.png)
[leetcode] dynamic programming solution partition array i[red fox]
随机推荐
Codeforces Global Round 14
Go from introduction to actual combat - panic and recover (notes)
Go从入门到实战——行为的定义和实现(笔记)
CEPH distributed storage
[LeetCode]508. The most frequent subtree elements and
100 important knowledge points that SQL must master: retrieving data
[LeetCode]161. 相隔为 1 的编辑距离
gomock mockgen : unknown embedded interface
[LeetCode]30. 串联所有单词的子串
IO stream code
100 important knowledge points that SQL must master: filtering data
Simulink导出FMU模型文件方法
Go从入门到实战——所有任务完成(笔记)
跟我一起AQS SOS AQS
猜拳游戏专题训练
本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
富文本 考试 填空题
∫(0→1) ln(1+x) / (x² + 1) dx
uniapp拦截请求
.NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法