QT加密解密读取XML文件

前端之家收集整理的这篇文章主要介绍了QT加密解密读取XML文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#-------------------------------------------------
#
# Project created by QtCreator 2018-03-16T23:59:22
#
#-------------------------------------------------

QT       += core gui xml

greaterThan(QT_MAJOR_VERSION,4): QT += widgets

TARGET = QtReadEncryptionXML
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so,uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui
 
 
//
//  文件名:mainwindow.h
//  工程名:QtReadEncryptionXML
//  简介:Qt读取加密的XML文件   运行环境MacOS:10.13.1
//  创建日期:Created by chenyijun on 2018/03/16.
//  版权:Copyright © 2018年 chenyijun. All rights reserved.
//
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void initView();
    void initData();
    void initConnect();

public slots:
    void slotReadOriginalXML();
    void slotEncryptionXML();
    void slotReadEncryptXML();
    void slotDecryptionXML();

private:
    Ui::MainWindow *ui;
    QString m_strOrgXML;
    QString m_strEncryptXML;
    QString m_strDecryptXML;
};

#endif // MAINWINDOW_H
 
 
//
//  文件名:mainwindow.cpp
//  工程名:QtReadEncryptionXML
//  简介:Qt读取加密的XML文件   运行环境MacOS:10.13.1
//  创建日期:Created by chenyijun on 2018/03/16.
//  版权:Copyright © 2018年 chenyijun. All rights reserved.
//


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QFile>
#include <QMessageBox>
#include <QDomDocument>

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

    initView();
    initData();
    initConnect();

}

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


void MainWindow::initView()
{

}

void MainWindow::initData()
{
    m_strOrgXML = "/Users/chenyijun/project/QtReadEncryptionXML/test_org.xml";
    m_strEncryptXML = "/Users/chenyijun/project/QtReadEncryptionXML/test_encrypt.xml";
    m_strDecryptXML = "/Users/chenyijun/project/QtReadEncryptionXML/test_decrypt.xml";
}

void MainWindow::initConnect()
{
    connect(ui->pushButton_ReadXML,SIGNAL(clicked()),this,SLOT(slotReadOriginalXML()));
    connect(ui->pushButton_ReadEncryption,SLOT(slotReadEncryptXML()));
    connect(ui->pushButton_Encryption,SLOT(slotEncryptionXML()));
    connect(ui->pushButton_Decryption,SLOT(slotDecryptionXML()));
}

void MainWindow::slotReadOriginalXML()
{
    QFile file(m_strOrgXML);
    if(!file.open(QFile::ReadOnly | QFile::Text))
    {
        QMessageBox::information(NULL,QString("title"),QString("open error!"));
        return;
    }

    QDomDocument document;
    QString error;
    int row = 0,column = 0;
    if(!document.setContent(&file,false,&error,&row,&column))
    {
        QMessageBox::information(NULL,QString("parse file Failed at line row and column") + QString::number(row,10) + QString(",") + QString::number(column,10));

        return;
    }

    if(document.isNull())
    {
        QMessageBox::information(NULL,QString("document is null!"));

        return;
    }

    QDomElement root = document.documentElement();

    //root_tag_name为root
    QString rootTagName = root.tagName();
    qDebug() << "rootTagName====================" << rootTagName;
    //获取第一级子节点,数目为2   分别为persons(4个节点)和index(3个节点)
    QDomNodeList mainList = root.childNodes();
    int mainListCount = mainList.count();
    qDebug() << "mainListCount====================" << mainListCount;
    for(int i1 = 0; i1 < mainListCount; ++i1)
    {
        QDomNode dom_node = mainList.item(i1);
        QDomElement element = dom_node.toElement();
        //获取name值,等价
        QString name_1 = element.attributeNode("name").value();
        QString name_2 = element.attribute("name");
        //qDebug() << "i1====" << i1 << "   name_1===" << name_1 << "    name_2===" << name_2 << "    element.tagName===" << element.tagName() << "    element.text==" << element.text();
        QDomNodeList m2list = element.childNodes();
        int m2listCount = m2list.count();
        for(int i2 = 0; i2 < m2listCount; ++i2)
        {
            QDomNode dom_node = m2list.item(i2);
            //读取子元素 方式1与方式2等价
            QDomElement element = dom_node.toElement();
            //qDebug() << "i2====" << i2  << "    element.tagName===" << element.tagName() << "    element.text==" << element.text();
            QDomNodeList child_list = element.childNodes();
            int child_listCount = child_list.count();
            for(int i3 = 0; i3 < child_listCount; ++i3)
            {
                QDomNode dom_node = child_list.item(i3);
                QDomElement element = dom_node.toElement();
                qDebug() << "i3" << i3 << "    element.tagName===" << element.tagName() << "    element.text==" << element.text();

            }
            //读取子元素方式2
//            QDomElement element = dom_node.firstChildElement();
//            while(!element.isNull())
//            {
//                   QString tag_name = element.tagName();
//                   QString tag_value = element.text();
//                   qDebug() << "tag_name===" << tag_name << "    tag_value==" << tag_value;
//                  element = element.nextSiblingElement();
//            }
            qDebug() << "\n";
        }
        qDebug() << "\n\n";
    }

}

void MainWindow::slotReadEncryptXML()
{
    QFile file(m_strEncryptXML);
    if(!file.open(QFile::ReadOnly | QFile::Text))
    {
        QMessageBox::information(NULL,column = 0;
    //解密
    QByteArray byteArray = QByteArray::fromBase64(file.readAll());
    //if(!document.setContent(&file,&column))
    if(!document.setContent(byteArray,QString("document is null!"));

        return;
    }

    QDomElement root = document.documentElement();

    //root_tag_name为root
    QString rootTagName = root.tagName();
    qDebug() << "rootTagName====================" << rootTagName;
    //获取第一级子节点,数目为2   分别为persons(4个节点)和index(3个节点)
    QDomNodeList mainList = root.childNodes();
    int mainListCount = mainList.count();
    qDebug() << "mainListCount====================" << mainListCount;
    for(int i1 = 0; i1 < mainListCount; ++i1)
    {
        QDomNode dom_node = mainList.item(i1);
        QDomElement element = dom_node.toElement();
        //获取name值,等价
        QString name_1 = element.attributeNode("name").value();
        QString name_2 = element.attribute("name");
        //qDebug() << "i1====" << i1 << "   name_1===" << name_1 << "    name_2===" << name_2 << "    element.tagName===" << element.tagName() << "    element.text==" << element.text();
        QDomNodeList m2list = element.childNodes();
        int m2listCount = m2list.count();
        for(int i2 = 0; i2 < m2listCount; ++i2)
        {
            QDomNode dom_node = m2list.item(i2);
            //读取子元素 方式1与方式2等价
//            QDomElement element = dom_node.toElement();
//            //qDebug() << "i2====" << i2  << "    element.tagName===" << element.tagName() << "    element.text==" << element.text();
//            QDomNodeList child_list = element.childNodes();
//            int child_listCount = child_list.count();
//            for(int i3 = 0; i3 < child_listCount; ++i3)
//            {
//                QDomNode dom_node = child_list.item(i3);
//                QDomElement element = dom_node.toElement();
//                qDebug() << "i3" << i3 << "    element.tagName===" << element.tagName() << "    element.text==" << element.text();

//            }
            //读取子元素方式2
            QDomElement element = dom_node.firstChildElement();
            while(!element.isNull())
            {
                   QString tag_name = element.tagName();
                   QString tag_value = element.text();
                   qDebug() << "tag_name===" << tag_name << "    tag_value==" << tag_value;
                  element = element.nextSiblingElement();
            }
            qDebug() << "\n";
        }
        qDebug() << "\n\n";
    }

}


void MainWindow::slotEncryptionXML()
{
    //QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    QFile originalFile(m_strOrgXML);
    if(!originalFile.open(QIODevice::ReadOnly))
    {
        QMessageBox::warning(0,"Read Tip","Read original File error!",QMessageBox::Yes);
    }
    //加密
    QByteArray byteArray = originalFile.readAll().toBase64();
    byteArray = byteArray.insert(10,"yijun");//加入一些特殊字符,解密的时候必须按规则解密,否则就算知道是Base64加密的也解密不了.
    qDebug() << "Encrypt======byteArray======" << byteArray;
    QFile encryptFile(m_strEncryptXML);
    if(!encryptFile.open(QIODevice::WriteOnly))
    {
        QMessageBox::warning(0,"Write Tip","encrypt error!",QMessageBox::Yes);
    }
    encryptFile.write(byteArray);
    originalFile.close();
    encryptFile.close();
}

void MainWindow::slotDecryptionXML()
{
    QFile file(m_strEncryptXML);
    if(!file.open(QIODevice::ReadOnly))
    {
        QMessageBox::warning(this,tr("Load Encrypt File"),file.errorString(),QMessageBox::Yes);
    }
    //解密
    QByteArray byteArray = file.readAll();
    byteArray = byteArray.remove(10,5);//按加密规则解密
    byteArray = QByteArray::fromBase64(byteArray);

    qDebug() << "Decrypt=====byteArray======" << byteArray;
    QFile decryptFile(m_strDecryptXML);
    if(!decryptFile.open(QIODevice::WriteOnly))
    {
        QMessageBox::warning(0,"decrypt error!",QMessageBox::Yes);
    }
    decryptFile.write(byteArray);
    file.close();
    decryptFile.close();

}
#include "mainwindow.h"
#include <QApplication>

int main(int argc,char *argv[])
{
    QApplication a(argc,argv);
    MainWindow w;
    w.show();

    return a.exec();
}


test_org.xml文件内容
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
    <persons name = "Qt">
        <person id = "101">
            <name>baidu</name>
            <age>1999</age>
            <website>https://www.baidu.com/</website>
        </person>
        <person id = "102">
            <name>sohu</name>
            <age>1998</age>
            <website>http://www.sohu.com/</website>
        </person>
        <person id = "103">
            <name>qq</name>
            <age>1998</age>
            <website>http://www.qq.com/</website>
        </person>
        <person id = "104">
            <name>youku</name>
            <age>2006</age>
            <website>http://www.youku.com/</website>
        </person>
    </persons>
    <index>
        <item>
            <id>10001</id>
            <title>number1</title>
            <text>this is number1</text>
        </item>
        <item>
            <id>10002</id>
            <title>number2</title>
            <text>this is number2</text>
        </item>
        <item>
            <id>10003</id>
            <title>number3</title>
            <text>this is number3</text>
        </item>
    </index>
</root>
原文链接:https://www.f2er.com/xml/293429.html

猜你在找的XML相关文章