最近需要為一個工具添加注冊碼,查閱了網上相關的資料,再結合自身的工程需求,簡單整理一下。
1、通過序列號生成注冊碼:
(1)試用版注冊碼----一定時間后關閉軟件,再次運行時需要重新注冊;
(2)正式版注冊碼----注冊之后,運行軟件不再需要注冊;
2、UI界面如下
使用時需要在序列號編輯框中輸入序列號,選擇密鑰類型,點擊按鈕,密鑰框內就會顯示生成的注冊碼。
3、代碼
(1)頭文件
1 #ifndef VSPOWER_H 2 #define VSPOWER_H 3 4 #include <QtWidgets/QWidget> 5 #include <Windows.h> 6 #include <QString> 7 #include <QCryptographicHash> 8 #include "ui_vspower.h" 9 10 class VSPower : public QWidget 11 { 12 Q_OBJECT 13 14 public: 15 VSPower(QWidget *parent = 0); 16 ~VSPower(); 17 18 private: 19 Ui::VSPowerClass ui; 20 QString m_Code; 21 QString m_codeMD4; 22 23 private: 24 25 //獲得序列號 26 QString GetSerialNumber(QString processID); 27 //格式化序列號 28 QString FormatSerialNumber(const QString serialNum); 29 //正式版加密 30 QString OfficialEncryption(const QString temp); 31 32 private slots: 33 //測試版加密 34 void TestSerialNum(); 35 //生成密鑰 36 void createCode(); 37 38 }; 39 40 #endif // VSPOWER_H
(2)源文件
1 #include "vspower.h" 2 3 VSPower::VSPower(QWidget *parent) 4 : QWidget(parent) 5 { 6 ui.setupUi(this); 7 connect(ui.m_creatCode, &QPushButton::clicked, this, &VSPower::createCode); 8 connect(ui.m_test, SIGNAL(clicked()), this, SLOT(TestSerialNum())); 9 } 10 11 VSPower::~VSPower() 12 { 13 14 } 15 16 QString VSPower::GetSerialNumber(QString processID) 17 { 18 //序列號 19 DWORD VolumeSerialNumber = 1111111; 20 m_Code = processID.mid(0, 4) + "U-" + processID.mid(4, 4) + "V-" + 21 processID.mid(8, 4) + "W-" + processID.mid(12, 4) + "X-" + 22 QString::number(VolumeSerialNumber, 10).mid(0, 4) + "6"; 23 24 return m_Code; 25 } 26 27 //使用MD5加密 28 QString VSPower::OfficialEncryption(const QString temp) 29 { 30 QByteArray byte; 31 byte.append(temp); 32 //QCryptographicHash類提供了生成密碼散列的方法。該類可以用於生成二進制或文本數據的加密散列值。 33 //目前支持MD4、MD5、SHA-1、SHA-224、SHA-256、SHA-384和SHA-512。 34 QByteArray MD5byte = QCryptographicHash::hash(byte, QCryptographicHash::Md5); 35 return MD5byte.toHex().toUpper(); 36 } 37 //生成測試版密鑰 38 void VSPower::TestSerialNum() 39 { 40 QString qstr = ui.m_serialNum->text(); 41 GetSerialNumber(qstr); 42 QByteArray byte; 43 byte.append(m_Code); 44 QByteArray MD4byte = QCryptographicHash::hash(byte, QCryptographicHash::Md4); 45 m_codeMD4 = MD4byte.toHex().toUpper(); 46 } 47 48 // 格式化生成的序列號 49 50 QString VSPower::FormatSerialNumber(const QString serialNum) 51 { 52 QString tempSerial = ""; 53 for (int i = 0; i < 7; i++) 54 { 55 tempSerial += serialNum.mid(4 * i, 4) + "-"; 56 } 57 tempSerial += serialNum.mid(28, 4); 58 return tempSerial; 59 } 60 61 // 生成密鑰槽函數 62 63 void VSPower::createCode() 64 { 65 if (ui.m_test->isChecked()) 66 { 67 QString key = FormatSerialNumber(m_codeMD4); 68 ui.m_powerNum->setText(key); 69 } 70 else 71 { 72 QString qstr = ui.m_serialNum->text(); 73 QString m_code = GetSerialNumber(qstr); 74 QString m_MD5Key = OfficialEncryption(m_code); 75 QString key = FormatSerialNumber(m_MD5Key); 76 ui.m_powerNum->setText(key); 77 } 78 }
這里只是簡單的完成注冊碼生成工具,使用時需要在序列號編輯框中輸入序列號,選擇密鑰類型,點擊按鈕,密鑰框內就會顯示生成的注冊碼。僅供參考。
參考:Qt-序列號生成器