QT之加解密
// 加解密都用此方法
QString toXOREncryptUncrypt(QString src, const QChar key)
{
for (int i = 0; i < src.count(); i++) {
src[i] = src.at(i).toLatin1() ^ key.toLatin1();
}
return src;
}
//加密
QString DES_data(QString src, QString key)
{
QChar deskey = *key.data();
QByteArray text = src.toLocal8Bit();
QByteArray by = text.toBase64();
QString str = QString(by);
// 异或加密(钥匙加密) --- 注意:(这里的key是可以指定为任意字符的,相应的,解密也要同意字符才行)
str = toXOREncryptUncrypt(str, deskey);
return str;
}
//解密
QString DES_decrypt_data(QString src, QString key)
{
QChar deskey = *key.data();
QString str = toXOREncryptUncrypt(src, deskey); //异或解密(钥匙解密)
QByteArray text = str.toLocal8Bit();
QByteArray by = text.fromBase64(text);
QString outstr = QString::fromLocal8Bit(by); // 注意:这里是处理中文用的
return outstr;
}
测试调用:
QString logininfo = "cookie:S6BB6A2E727A450; is_authed=1 userid:1101025456 organid : C00006780 username : wpzhangzechi";
QString desdata = DES_data(logininfo,"123465798");//加密
QString deseco = DES_decrypt_data(desdata, "123465798");//解密