当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
Quick excel export
[LeetCode]508. The most frequent subtree elements and
matlab查找某一行或者某一列在矩阵中的位置
洛谷P5706 再分肥宅水
What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?
GBase 8a OLAP分析函数cume_dist的使用样例
ABC-Teleporter Setting-(思维+最短路)
Interval DP of Changyou dynamic programming
win11桌面出現“了解此圖片”如何删除
How to participate in openharmony code contribution
空指针异常
Go从入门到实战——依赖管理(笔记)
快递e栈——数组篇小型项目
[leetcode] 508. Élément de sous - arbre le plus fréquent et
动态刷新mapper看过来
Go從入門到實戰——接口(筆記)
Go from introduction to practice - polymorphism (note)
STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
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
集合代码练习








![[leetcode] dynamic programming solution split integer i[silver fox]](/img/18/8dc8159037ec1262444db8899cde0c.png)
