Qt/C++使用小记8【qt对称加密/数据加密解密/哈希加密】

收获8

众所周知,有时需要将数据保存到普通文本内,但是又不想被直观的被看见数据内容,就想着能不能加个密,转成别的字符,读取出来的时候,再在程序内解读出来,我尝试过用哈希算法加密,但是哈希算法是单项的,保存单项密码是可以的也是比较安全的,哈希使用如下(附上了字符串转QByteArray和QByteArray转字符串):

#include <QCryptographicHash>
#include <QDebug>

// 使用 SHA-256 哈希算法加密密码
QString password("123456");
QByteArray hashedPassword = QCryptographicHash::hash(password.toUtf8(),
                            QCryptographicHash::Sha256).toHex();

//  打印哈希加密结果 (QByteArray转QString)
qDebug()<<static_cast<QString>(hashedPassword);

#ifdef USE_TO_BYTEARRAY
    //  QString转QByteArray方式有俩 如下
    QByteArray data = str.toLatin1();
    //  或者
    QByteArray data = str.toLocal8Bit();
#endif

输出结果:

"8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92"

前文提到,因为哈希值是单项的,则不能由结果转回为“123456”了,要比较也是拿一个“123456”再转一次成为哈希值比较俩哈希值是否相等,来判断是不是同一字符串,仅仅保存密码的对比是很方便高效和安全的,但是不是对称加密方式。

便有了如下,直接上代码:

首先创建一个工程,Ui就俩按钮QPushbutton和四个QLineEdit
.h文件
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

    /**
     * @brief EncryptDecryptFunc    加密解密函数
     * @param strSource             源字符串
     * @param cKey                  异或加密(这里的key是可以指定为任意字符的,
     *                              解密也要同一字符才行,这里默认给f)
     * @return                      异或后的字符串
     */
    QString EncryptDecryptFunc(QString strSource, const QChar cKey = 'f');

private slots:
    /**
     * @brief on_pushButton_encrypt_clicked 加密按钮槽函数
     */
    void on_pushButton_encrypt_clicked();

    /**
     * @brief on_pushButton_decrypt_clicked 解密按钮槽函数
     */
    void on_pushButton_decrypt_clicked();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
.cpp文件
#include "Widget.h"
#include "ui_Widget.h"
#include <QDebug>
#include <QCryptographicHash>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

/**
 * @brief EncryptDecryptFunc    加密解密函数
 * @param strSource             源字符串
 * @param cKey                  异或加密(这里的key是可以指定为任意字符的,
 *                              解密也要同一字符才行,这里默认给f)
 * @return                      异或后的字符串
 */
QString Widget::EncryptDecryptFunc(QString strSource, const QChar cKey)
{
    //  分别遍历并异或(相同为0 不同为1)
    for(auto iter = strSource.begin(); iter != strSource.end(); ++iter)
        (*iter) = (*iter).toLatin1() ^ cKey.toLatin1();

    return strSource;
}

/**
 * @brief on_pushButton_encrypt_clicked 加密按钮槽函数
 */
void Widget::on_pushButton_encrypt_clicked()
{
    //  获取源字符串
    QString strSource = ui->lineEdit_encrypt_left->text();
    //  先转为本地八位
    QByteArray byteSource8Bit = strSource.toLocal8Bit();
    //  再转为Base64
    QByteArray byteBase64 = byteSource8Bit.toBase64();
    //  转为字符串
    QString strEncryptResult = static_cast<QString>(byteBase64);

    //  调用函数加密并显示
    ui->lineEdit_encrypt_right->setText(EncryptDecryptFunc(strEncryptResult));
}

/**
 * @brief on_pushButton_decrypt_clicked 解密按钮槽函数
 */
void Widget::on_pushButton_decrypt_clicked()
{
    //  获取源字符串  并调用函数解密
    QString strSource = ui->lineEdit_decrypt_left->text();
    //  调用函数解密
    strSource = EncryptDecryptFunc(strSource);
    //  转为本地八位
    QByteArray byteSource8Bit = strSource.toLocal8Bit();
    //  还原  如果不是中文 这里以及够了 但是保险起见 加下面的一句解决中文乱码 以防万一
    QByteArray byteBase64 = byteSource8Bit.fromBase64(byteSource8Bit);
    //  处理中文
    QString strResult = QString::fromLocal8Bit(byteBase64);
    ui->lineEdit_decrypt_right->setText(strResult);
}
.ui内容

运行测试:
         

都没有问题,函数EncryptDecryptFunc内的第二个参数可以换为任意值,这里我默认的是'f',需要着重注意的就是,什么样的字符加的密,就必须用什么字符去解密!!!

参考来源【Qt 学习之路】Qt以Base64加密作为基础实现3种加解密方式(包含中文处理)_qt base64-CSDN博客

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值