天天看点

QT使用异或加密算法进行加密解密_F_hawk189_新浪博客

Xor.h:

#include "xor.h"

Xor::Xor(QObject *parent) : QObject(parent)
{


}

Xor::~Xor()
{


}

QString Xor::XorEncryptDecrypt(const QString &str, const char &key)
{

 QString result;

 QByteArray bs = this->qstringToByte(str);

 int i;

 for(i=0; i i++)
    {

 bs[i] = bs[i] ^ key;

 }

    result = byteToQString(bs);

 return result;


}

QString Xor::byteToQString(const QByteArray &byte)
{

 QString result;

 if(byte.size() > 0)
    {

  QTextCodec *codec = QTextCodec::codecForName("utf-8");

   result = codec->toUnicode(byte);

 }

    return result;


}

QByteArray Xor::qstringToByte(const QString &strInfo)
{

 QByteArray result;

 if(strInfo.length() > 0)
    {

  QTextCodec *codec = QTextCodec::codecForName("utf-8"); 

  result = codec->fromUnicode(strInfo);

 }

    return result;


}

主函数:
 QString str = QString("Hello World!");
    

QString jiami = getXorEncryptDecrypt(str, 12);
    

qDebug() << "str:" << str;

    qDebug() << "Encrypto:" << jiami;

    QString jiemi = getXorEncryptDecrypt(jiami, 12);

    qDebug() << "Decrypto:" << jiemi;