赞
踩
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
这个功能主要是实现一机一码的功能,给用户做试用体验产品的,功能的实现主要为A,B机两套软件,产品A用来获取当前用户的CPU、磁盘序列号、主板,mac地址等唯一标识号,共同组成一个字符串,AES中利用秘钥对字符串加密,用户将产品生成的秘钥提交给公司,公司利用秘钥和B机对A机实现解密,将A机的设备信息利用Qt自带的加密方法生成加密字符串,再次回传给A机进行匹配,匹配成功则通过用户,进行产品登录试用。
#include "qaesencryption.h"主要借用了Qt的第三方QAes库实现加密解密功能。AES属于分组加密算法,每组长度相等,每次加密一组数据,直到加密完整个明文,明文长度固定为128位,密钥长度可以是128(10轮循环)、192(12轮循环)、256位(14轮循环)。
明文P需要分组,称为状态,状态用以字节为元素的矩阵阵列表示,16字节明文按照此顺序放入矩阵,规则为:从上到下,从左到右,依次进行。密钥K也需要分组,原理与明文P相同。
获取一堆信息,生成秘钥,Qt自带的秘钥无法解密,所以采用了外接第三库的加密解密算法,一层加密当时感觉不保守,做了两层加密,加密过程在解密中介绍,代码如下(示例):
// 获取CPU信息 QProcess process; process.start("wmic cpu get ProcessorId"); process.waitForFinished(); QString cpuId = process.readAllStandardOutput(); cpuId = cpuId.replace("ProcessorId", "").simplified(); qDebug()<<cpuId; // 获取硬盘序列号 process.start("wmic diskdrive get SerialNumber"); process.waitForFinished(); QString hddSerial = process.readAllStandardOutput(); hddSerial = hddSerial.replace("SerialNumber", "").simplified(); qDebug()<<hddSerial; //获取主板序列号 process.start("wmic baseboard get serialnumber"); process.waitForFinished(); QString Board_Serial = process.readAllStandardOutput(); Board_Serial = Board_Serial.replace("SerialNumber", "").simplified(); qDebug()<<Board_Serial; //获取MAC地址序列号 process.start("wmic nic where NetConnectionStatus=2 get macaddress"); process.waitForFinished(); QString MAC_Serial = process.readAllStandardOutput(); MAC_Serial = MAC_Serial.replace("MACAddress", "").simplified(); qDebug()<<MAC_Serial; //当前日期 QDateTime current_date_time = QDateTime::currentDateTime(); QString current_date = current_date_time.toString("yyyy-MM-dd"); qDebug()<<current_date; // 其他硬件信息可继续获取 // 组合这些信息 QString combinedInfo = QString("%1-%2-%3-%4-%5").arg(cpuId, hddSerial,Board_Serial,current_date,MAC_Serial); //信息加密 hashResult = QCryptographicHash::hash(combinedInfo.toLocal8Bit(), QCryptographicHash::Md5).toHex().toUpper(); QByteArray text = combinedInfo.toLocal8Bit(); QByteArray by = text.toBase64(); qDebug()<<"009----------------------------------"<<by; // 创建一个哈希值作为最终的序列号,生成一个MD5散列 //注册秘钥 ctext->setPlainText(by); qDebug()<<"0010----------------------------------"<<hashResult; QString key = "9876543"; //数据密钥 QString encoded = encodedText(combinedInfo, key); //加密数据 QString decoded = decodedText(encoded, key); //解密 qDebug()<<"******************************************"<<encoded; qDebug()<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<<decoded; // 创建一个哈希值作为最终的序列号,生成一个MD5散列 }
主要是我将整合成的字符串进行了一次本地字节转换,然后第二次利用Aes加密,对应的解密过程就是先进行Aes解密,在进行字符转换,最后用Qt加密生成秘钥,回传给A机里面的秘钥进行匹配。代码如下(示例):
QString combinedInfo=ctext->toPlainText(); qDebug()<<combinedInfo; //二次转字符解密 QByteArray text2 = combinedInfo.toLocal8Bit(); QByteArray by2 = text2.fromBase64(text2); QString str = QString::fromLocal8Bit(by2); //一次传输数据解密 QString key="7758521"; QString decoded = DecodedText(str, key); qDebug()<<"-----------------------"<<decoded; QString Data_get=decoded.right(10); QDateTime time; time = QDateTime::fromString(Data_get, "yyyy-MM-dd"); QByteArray hashResult = QCryptographicHash::hash(decoded.toLocal8Bit(), QCryptographicHash::Md5).toHex().toUpper(); codeEdit->setText(hashResult+DATE); qDebug()<<"-----------------------"<<hashResult+DATE;
这个实现的并不是很好,本来想添加一个试用日期的,比如说一天,一周,一个季度,后来通过写入注册表做好以后,发现只要把系统时间改了以后,试用期功能就会失效了。主要是对第三库#include "qaesencryption.h"的使用。
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。