qt base64加解密

86 篇文章 1 订阅

这里提供两种加解密的方法。
第一种方法:使用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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东方忘忧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值