In encryption, decryption, the XOR operation should be relatively simple. The following code, which is encrypted with an XOR operation, decrypts:
Click (here) to collapse or open
#include <QtCore/QCoreApplication>
#include <QTextCodec>
#include <QString>
#include <QDebug>
QString getxorencryptdecrypt (const QString &, const char &);
QString bytetoqstring (const qbytearray &byte);
Qbytearray qstringtobyte (const QString &strinfo);
int main (int argc, char *argv[])
{
Qcoreapplication A (argc, argv);
QString str = QString ("http://www.baidu.com?id=1");
QString Jiami = Getxorencryptdecrypt (str, 11);
Qdebug () << "str:" << str;
Qdebug () << "Miwen:" << Jiami;
QString Jiemi = Getxorencryptdecrypt (Jiami, 11);
Qdebug () << "Jimi:" << Jiemi;
return A.exec ();
}
QString getxorencryptdecrypt (const QString &STR, const char &key)
{
QString result;
Qbytearray bs = qstringtobyte (str);
for (int i=0; i<bs.size (); i++) {
Bs[i] = bs[i] ^ key;
}
result = Bytetoqstring (BS);
return result;
}
QString bytetoqstring (const Qbytearray &byte)
{
QString result;
if (byte.size () > 0) {
Qtextcodec *codec = Qtextcodec::codecforname ("Utf-8");
result = Codec->tounicode (byte);
}
return result;
}
Qbytearray qstringtobyte (const QString &strinfo)
{
Qbytearray result;
if (strinfo.length () > 0) {
Qtextcodec *codec = Qtextcodec::codecforname ("Utf-8");
result = Codec->fromunicode (strinfo);
}
return result;
}
testxorjiami$./testxorjiami
STR: "Http://www.baidu.com?id=1"
Miwen: "c^?^?" {1$$| | | %ijbo~%hdf4bo6: "
Jimi: "Http://www.baidu.com?id=1"
Xorencryptdecrypt.rar
QT uses XOR to encrypt or decrypt