QT uses XOR to encrypt or decrypt

Source: Internet
Author: User


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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.