古詩詞大全網 - 成語故事 - 請問怎麽給軟件加註冊驗證

請問怎麽給軟件加註冊驗證

要實現軟件註冊功能,首先需要知道實現註冊機制要涉及到的幾個問題:1、如何加入註冊檢測,判斷軟件是否註冊;2、如何生成註冊碼,如何保證壹個用戶名只生成與之唯壹對應的註冊碼;3、在軟件不註冊情況下,如何限制軟件功能的局限性;4、對已經負費使用用戶而言,不應造成使用不便。

首先,應該有壹個生成註冊碼的算法,以下是我簡單的壹個生成15位註冊碼的算法:

//該函數返回壹個CSTRING類型的15位註冊碼,入口參數為用戶名

CString GetRegPasswd(CString &DirName)

{

//將用戶名換算成15位註冊碼

long Num1,Num2,Num3;

char sn[16]={0};

CString p;

int i,len;

Num1=0;

Num2=0;

Num3=0;

len=int(strlen(DirName));

if(len!=0)

{

for( i=1;i<=len;i++)

{

//第壹步算法

Num1=(long(Num1+(int(DirName[i-1])*i*i)*(i*sqrt(DirName[i-1])+1)))%100000;

//第二步算法

Num2=(Num2*i+(long(pow((int)DirName[i-1],2)*i)))%100000;

//第三步算法

Num3=(Num2+(long)sqrt(Num1))%100000;

}

//以下把三個算法結果分別生成5個字符,***有15個

for(i=0;i<5;i++)

sn[i]=(int)(Num1+31+i*i*i)%128;

for(i=5;i<10;i++)

sn[i]=(int)(Num2+31+i*i*i)%128;

for(i=10;i<15;i++)

sn[i]=(int)(Num3+31+i*i*i)%128;

sn[15]=0;

//以下循環把所有生成的字符轉換為0---9,A---Z,a----z

for(i=0;i<15;i++)

{

while((sn[i]<'0' || sn[i]>'9') && (sn[i]<'A' || sn[i]>'Z') &&(sn[i]<'a' || sn[i]>'z') )

{

sn[i]=(sn[i]+31+7*i)%128;

}

}

//賦值給壹個CSTRING變量,用做函數返回值

p.Format("%s",sn);

}

return p;

}

//檢查軟件是否註冊的函數

BOOL GetRegFlag(void)

{

HKEY hKey = NULL;

BYTE i;

CString str;

str.LoadString(IDS_REG_KEY);// IDS_REG_KEY為在註冊表中的子目錄字符串

if (RegCreateKey(HKEY_CURRENT_USER, str, &hKey) != ERROR_SUCCESS) return false;

DWORD cbA;

cbA=sizeof(int);

if( RegQueryValueEx(hKey, "SzMima",NULL,NULL, &i,&cbA) != ERROR_SUCCESS)

return false;

BYTE j=i;

if(j==0)//0代表軟件已經註冊,可以正常使用

{

RegCloseKey(hKey);

return true;

}

else

{

RegCloseKey(hKey);

return false;

}

return false;

};

//設置軟件已經註冊標誌的函數

BOOL SetRegFlag(void)

{

HKEY hKey = NULL;

BYTE i;

CString str;

str.LoadString(IDS_REG_KEY);// IDS_REG_KEY為在註冊表中的子目錄字符串

if (RegCreateKey(HKEY_CURRENT_USER, str, &hKey) != ERROR_SUCCESS) return false;

BYTE j=0;//0代表已經註冊

if(RegSetValueEx(hKey, "SzMima", 0, REG_BINARY, &j,4) != ERROR_SUCCESS)

{

AfxMessageBox("設置註冊表數據失敗!");

return FALSE;

}

return false;

};

以上三個函數即可實現軟件註冊機制,只需要在程序初始化的時候加入以下幾句代碼即可

BOOL bReg= GetRegFlag ();

if(!bReg)

{

//在此加入限制功能或者拒絕是使用的代碼

}

如果用戶註冊只需要加入以下代碼即可

//這是我的程序中的壹個註冊界面,輸入用戶名和註冊碼提交後檢驗註冊碼是否正確的代碼

void CRegEdit::OnBnClickedOk()

{

if(!UpdateData()) return;//取得編輯框的內容,並賦值到類變量中

m_RegUser.TrimLeft();//m_RegUser是用戶名

m_RegUser.TrimRight();

if(m_RegUser.IsEmpty())

{

AfxMessageBox("用戶名不能為空,請重新輸入。");

GetDlgItem(IDC_REGUSER)->SetFocus();

return;

}

m_RegPasswd.TrimLeft();m_RegPasswd是註冊碼

m_RegPasswd.TrimRight();

if(m_RegPasswd.IsEmpty())

{

AfxMessageBox("註冊碼不能為空,請重新輸入。");

GetDlgItem(IDC_REGPASSWD)->SetFocus();

return;

}

CString Passwd;

Passwd=GetRegPasswd(m_RegUser);//調用算法取得該用戶名的註冊碼

if(Passwd==m_RegPasswd)//與用戶輸入的註冊進行比較

{

SetRegFlag();//設置註冊標誌

OnOK();

}

else

AfxMessageBox("註冊碼錯誤,請重新輸入。");

UpdateData(false);

}

RSA做軟件的註冊功能,如果密鑰設置的比較大位數的話,基本別人是無法寫出註冊機的,但是防止別人暴破妳的程序又是另壹回事了。

Imports System.Security.Cryptography

Imports System.Text

Imports System.IO

Imports System.Xml

Public Class FrmReg

Inherits System.Windows.Forms.Form

'Dim xmlKeys As String 'A combination of both the public and 'private keys

'Dim xmlPublicKey As String 'The public key only

''The plaintext message in a byte array

'Dim PlainTextBArray As Byte()

''The cyphertext message in a byte array

'Dim CypherTextBArray As Byte()

#Region " Windows 窗體設計器生成的代碼 "

Public Sub New()

MyBase.New()

'該調用是 Windows 窗體設計器所必需的。

InitializeComponent()

'在 InitializeComponent() 調用之後添加任何初始化

End Sub

'窗體重寫 dispose 以清理組件列表。

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Windows 窗體設計器所必需的

Private components As System.ComponentModel.IContainer

'註意: 以下過程是 Windows 窗體設計器所必需的

'可以使用 Windows 窗體設計器修改此過程。

'不要使用代碼編輯器修改它。

Friend WithEvents btnReg As System.Windows.Forms.Button

Friend WithEvents btnexit As System.Windows.Forms.Button

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents Label2 As System.Windows.Forms.Label

Friend WithEvents txtRegCode As System.Windows.Forms.TextBox

Friend WithEvents txtRegName As System.Windows.Forms.TextBox

Friend WithEvents LinkLabel1 As System.Windows.Forms.LinkLabel

Private Sub InitializeComponent()

Me.btnReg = New System.Windows.Forms.Button

Me.btnexit = New System.Windows.Forms.Button

Me.txtRegName = New System.Windows.Forms.TextBox

Me.Label1 = New System.Windows.Forms.Label

Me.Label2 = New System.Windows.Forms.Label

Me.txtRegCode = New System.Windows.Forms.TextBox

Me.LinkLabel1 = New System.Windows.Forms.LinkLabel

Me.SuspendLayout()

'

'btnReg

'

Me.btnReg.FlatStyle = System.Windows.Forms.FlatStyle.Flat

Me.btnReg.Location = New System.Drawing.Point(64, 192)

Me.btnReg.Name = "btnReg"

Me.btnReg.Size = New System.Drawing.Size(75, 24)

Me.btnReg.TabIndex = 0

Me.btnReg.Text = "註冊(&R)"

'

'btnexit

'

Me.btnexit.FlatStyle = System.Windows.Forms.FlatStyle.Flat

Me.btnexit.Location = New System.Drawing.Point(168, 192)

Me.btnexit.Name = "btnexit"

Me.btnexit.Size = New System.Drawing.Size(75, 24)

Me.btnexit.TabIndex = 1

Me.btnexit.Text = "取消(&C)"

'

'txtRegName

'

Me.txtRegName.Location = New System.Drawing.Point(8, 24)

Me.txtRegName.Name = "txtRegName"

Me.txtRegName.Size = New System.Drawing.Size(296, 21)

Me.txtRegName.TabIndex = 2

Me.txtRegName.Text = ""

'

'Label1

'

Me.Label1.Location = New System.Drawing.Point(8, 0)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(48, 16)

Me.Label1.TabIndex = 3

Me.Label1.Text = "註冊名:"

'

'Label2

'

Me.Label2.Location = New System.Drawing.Point(8, 56)

Me.Label2.Name = "Label2"

Me.Label2.Size = New System.Drawing.Size(56, 16)

Me.Label2.TabIndex = 4

Me.Label2.Text = "註冊碼:"

'

'txtRegCode

'

Me.txtRegCode.Location = New System.Drawing.Point(8, 80)

Me.txtRegCode.Multiline = True

Me.txtRegCode.Name = "txtRegCode"

Me.txtRegCode.Size = New System.Drawing.Size(296, 104)

Me.txtRegCode.TabIndex = 5

Me.txtRegCode.Text = ""

'

'LinkLabel1

'

Me.LinkLabel1.Location = New System.Drawing.Point(128, 56)

Me.LinkLabel1.Name = "LinkLabel1"

Me.LinkLabel1.Size = New System.Drawing.Size(128, 16)

Me.LinkLabel1.TabIndex = 6

Me.LinkLabel1.TabStop = True

Me.LinkLabel1.Text = ""

Me.TopMost = True

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click

Application.Exit()

End Sub

Private Sub btnReg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReg.Click

If txtRegName.Text = "" Then

MessageBox.Show("註冊名不能為空", "提示")

Return

Else

If txtRegCode.Text = "" Then

MessageBox.Show("註冊碼不能為空", "提示")

Return

Else

If txtRegCode.Text.Length <> 172 Then

MessageBox.Show("註冊碼長度不對", "提示")

Return

End If

End If

End If

Try

Dim RSA As System.Security.Cryptography.RSACryptoServiceProvider = New System.Security.Cryptography.RSACryptoServiceProvider

'公鑰

RSA.FromXmlString("xzALwAOqEcj8jWrKXyaecAhyz9W+wH26K8ZcMv9ZFYDJeR5jMhK7AhNbwn+q1mAH7VV146v6S2SlJeErEvTXS3dJR0pjaED0R2JSf0FwrWP6CDsKREQC803E+Faaw/Kou134K6mejPg16dxYfcIlJgoWRqn58qnjo6g87XlUMUs=AQAB")

Dim RSADeformatter As System.Security.Cryptography.RSAPKCS1SignatureDeformatter = New System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA)

'指定解密的時候HASH算法為MD5

RSADeformatter.SetHashAlgorithm("MD5")

Dim Regcode As Byte() = Convert.FromBase64String(txtRegCode.Text)

Dim RegName As Byte() = (New System.Text.ASCIIEncoding).GetBytes(txtRegName.Text)

Dim dataToHash As Byte() = (New System.Text.ASCIIEncoding).GetBytes(txtRegName.Text)

Dim hash As Byte() = CType(System.Security.Cryptography.CryptoConfig.CreateFromName("MD5"), System.Security.Cryptography.HashAlgorithm).ComputeHash(dataToHash)

'驗證簽名

If RSADeformatter.VerifySignature(hash, Regcode) Then

'註冊通過的話就寫入註冊文件

Dim textWriter As XmlTextWriter = New XmlTextWriter("reg.xml", Nothing)

textWriter.WriteStartElement("RegInfo")

textWriter.WriteStartElement("RegName")

textWriter.WriteString(txtRegName.Text)

textWriter.WriteEndElement()

textWriter.WriteStartElement("RegCode")

textWriter.WriteString(txtRegCode.Text)

textWriter.WriteEndElement()

textWriter.Close()

MessageBox.Show("註冊正確,謝謝註冊本軟件,請重新啟動程序完成註冊", "提示")

Application.Exit()

Else

MessageBox.Show("錯誤的註冊碼", "提示")

End If

Catch ex As Exception

'MessageBox.Show(ex.Message())

MessageBox.Show("錯誤的註冊碼", "提示")

End Try

End Sub

Private Sub FrmReg_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed

Application.Exit()

End Sub

Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked

System.Diagnostics.Process.Start("")

End Sub

End Class