当前位置:网站首页>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;
}
边栏推荐
- Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
- GBase 8a OLAP分析函数cume_dist的使用样例
- 01-Golang-环境搭建
- Go从入门到实战—— 多路选择和超时控制(笔记)
- gomock mockgen : unknown embedded interface
- Codeforces Round #723 (Div. 2)
- Magic POI error in reading excel template file
- List of language weaknesses --cwe, a website worth learning
- Go from introduction to practice - Interface (notes)
- Codeforces Round #717 (Div. 2)
猜你喜欢
关于异常处理的知识整理
What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?
快递e栈——数组篇小型项目
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
Tiktok's interest in e-commerce has hit the traffic ceiling?
Go从入门到实战——package(笔记)
Codeforces Round #717 (Div. 2)
How to participate in openharmony code contribution
Go从入门到实战——CSP并发机制(笔记)
Knowledge sorting of exception handling
随机推荐
Let Ma Huateng down! Web3.0, hopeless
[LeetCode]动态规划解分割数组I[Red Fox]
Acwing周赛57-数字操作-(思维+分解质因数)
Quick excel export
win11桌面出現“了解此圖片”如何删除
100 important knowledge points that SQL must master: filtering data
集合代码练习
Simulink导出FMU模型文件方法
Go从入门到实战——Context与任务取消(笔记)
[LeetCode]513. 找树左下角的值
[LeetCode]100. Same tree
Little known MySQL import data
How to participate in openharmony code contribution
Method of reading file contents by Excel
"Apprendre cette image" apparaît sur le Bureau win11 comment supprimer
Codeforces Round #716 (Div. 2)
TreeSet详解
io流代码
流程控制任务
vmware虚拟机PE启动