.Net自带的类库里面有个算法。
这个算法的原理是不对称加密的原理。不对称加密原理大家基本上都了解。加密的密码(密钥)分为两个部分,公钥和私钥。通过私钥加密的密文只能通过公钥解密。根据这个特性,我们可以发现只要开发者保存好私钥,即使算法代码被客户端破解,因客户端不知道保存在开发者处的私钥,也无法生成注册码。
以下是代码:
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Security.Cryptography
Public Class Form1
Private prikey As String, pubkey As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' getKeys();
pubkey = "<RSAKeyValue><Modulus>xe3teTUwLgmbiwFJwWEQnshhKxgcasglGsfNVFTk0hdqKc9i7wb+gG7HOdPZLh65QyBcFfzdlrawwVkiPEL5kNTX1q3JW5J49mTVZqWd3w49reaLd8StHRYJdyGAL4ZovBhSTThETi+zYvgQ5SvCGkM6/xXOz+lkMaEgeFcjQQs=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"
prikey = "<RSAKeyValue><Modulus>xe3teTUwLgmbiwFJwWEQnshhKxgcasglGsfNVFTk0hdqKc9i7wb+gG7HOdPZLh65QyBcFfzdlrawwVkiPEL5kNTX1q3JW5J49mTVZqWd3w49reaLd8StHRYJdyGAL4ZovBhSTThETi+zYvgQ5SvCGkM6/xXOz+lkMaEgeFcjQQs=</Modulus><Exponent>AQAB</Exponent><P>5flMAd7IrUTx92yomBdJBPDzp1Kclpaw4uXB1Ht+YXqwLW/9icI6mcv7d2O0kuVLSWj8DPZJol9V8AtvHkC3oQ==</P><Q>3FRA9UWcFrVPvGR5bewcL7YqkCMZlybV/t6nCH+gyMfbEvgk+p04F+j8WiHDykWj+BahjScjwyF5SGADbrfJKw==</Q><DP>b4WOU1XbERNfF3JM67xW/5ttPNX185zN2Ko8bbMZXWImr1IgrD5RNqXRo1rphVbGRKoxmIOSv7flr8uLrisKIQ==</DP><DQ>otSZlSq2qomgvgg7PaOLSS+F0TQ/i1emO0/tffhkqT4ah7BgE97xP6puJWZivjAteAGxrxHH+kPY0EY1AzRMNQ==</DQ><InverseQ>Sxyz0fEf5m7GrzAngLDRP/i+QDikJFfM6qPyr3Ub6Y5RRsFbeOWY1tX3jmV31zv4cgJ6donH7W2dSBPi67sSsw==</InverseQ><D>nVqofsIgSZltxTcC8fA/DFz1kxMaFHKFvSK3RKIxQC1JQ3ASkUEYN/baAElB0f6u/oTNcNWVPOqE31IDe7ErQelVc4D26RgFd5V7dSsF3nVz00s4mq1qUBnCBLPIrdb0rcQZ8FUQTsd96qW8Foave4tm8vspbM65iVUBBVdSYYE=</D></RSAKeyValue>"
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Using rsa As New RSACryptoServiceProvider()
rsa.FromXmlString(prikey)
' 加密对象
Dim f As New RSAPKCS1SignatureFormatter(rsa)
f.SetHashAlgorithm("SHA1")
Dim source As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(textBox1.Text)
Dim sha As New SHA1Managed()
Dim result As Byte() = sha.ComputeHash(source)
Dim s As String = Convert.ToBase64String(result)
MessageBox.Show("s=" & s.Length)
Dim b As Byte() = f.CreateSignature(result)
textBox2.Text = ""
'string s = System.Text.Encoding.Default.GetString(b, 0, 9);
textBox2.Text = Convert.ToBase64String(b)
End Using
textBox3.Text = pubkey
textBox4.Text = prikey
Dim str As String = textBox2.Text
' string aa=MD5.Create(str).ToString();
End Sub
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button2.Click
Using rsa As New RSACryptoServiceProvider()
rsa.FromXmlString(pubkey)
Dim f As New RSAPKCS1SignatureDeformatter(rsa)
f.SetHashAlgorithm("SHA1")
Dim key As Byte() = Convert.FromBase64String(textBox2.Text)
Dim sha As New SHA1Managed()
Dim name As Byte() = sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(textBox1.Text))
Dim s As String = Convert.ToBase64String(name)
If f.VerifySignature(name, key) Then
MessageBox.Show("Succese!")
Else
MessageBox.Show("Falied!")
End If
End Using
End Sub
Private Sub getKeys()
Using ras As New RSACryptoServiceProvider()
'公匙
pubkey = ras.ToXmlString(False)
'私匙
prikey = ras.ToXmlString(True)
End Using
End Sub
End Class
这个算法就是 System.Security.Cryptography 名称空间的 RSAPKCS1SignatureFormatter 类(用来生成注册码)和 RSAPKCS1SignatureDeformatter 类(用来在客户端验证注册码)。验证过程如下:
首先,需要生成一个公钥和私钥对,当然,依靠人是无法生成的,我们可以通过 System.Security.Cryptography 名称空间的RSACryptoServiceProvider 类来生成公钥/私钥对。
可以使用getKeys()方法获得公匙和私匙,但是如果要验证还是要在得到公匙和私匙后保持不变,因为这个方法生成的公匙和私匙都是一直在变化的,所以要有一个确定的公匙和私匙。
button1的Click事件中是生成注册码的过程,用 RSAPKCS1SignatureFormatter 类来生成注册码。
其中又用了sha1加密算法加密你的userId,这样可能起到的更好的保密效果。注册码验证是在button2的click事件中。
总的来说,就是用私匙生成注册码再用公匙去验证。可以自己做个有私匙的注册机用于生成注册码,在程序中使用公匙去验证,这样整个软件的安全性就比较高了。客户端代码是没有私钥的,即使有人把程序集的代码反编译了也没有用。
如果觉得172位的验证码太长了,不好用,可以用了其中的sha1算法作了一个28位的。
这个算法的原理是不对称加密的原理。不对称加密原理大家基本上都了解。加密的密码(密钥)分为两个部分,公钥和私钥。通过私钥加密的密文只能通过公钥解密。根据这个特性,我们可以发现只要开发者保存好私钥,即使算法代码被客户端破解,因客户端不知道保存在开发者处的私钥,也无法生成注册码。
以下是代码:
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Security.Cryptography
Public Class Form1
Private prikey As String, pubkey As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' getKeys();
pubkey = "<RSAKeyValue><Modulus>xe3teTUwLgmbiwFJwWEQnshhKxgcasglGsfNVFTk0hdqKc9i7wb+gG7HOdPZLh65QyBcFfzdlrawwVkiPEL5kNTX1q3JW5J49mTVZqWd3w49reaLd8StHRYJdyGAL4ZovBhSTThETi+zYvgQ5SvCGkM6/xXOz+lkMaEgeFcjQQs=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"
prikey = "<RSAKeyValue><Modulus>xe3teTUwLgmbiwFJwWEQnshhKxgcasglGsfNVFTk0hdqKc9i7wb+gG7HOdPZLh65QyBcFfzdlrawwVkiPEL5kNTX1q3JW5J49mTVZqWd3w49reaLd8StHRYJdyGAL4ZovBhSTThETi+zYvgQ5SvCGkM6/xXOz+lkMaEgeFcjQQs=</Modulus><Exponent>AQAB</Exponent><P>5flMAd7IrUTx92yomBdJBPDzp1Kclpaw4uXB1Ht+YXqwLW/9icI6mcv7d2O0kuVLSWj8DPZJol9V8AtvHkC3oQ==</P><Q>3FRA9UWcFrVPvGR5bewcL7YqkCMZlybV/t6nCH+gyMfbEvgk+p04F+j8WiHDykWj+BahjScjwyF5SGADbrfJKw==</Q><DP>b4WOU1XbERNfF3JM67xW/5ttPNX185zN2Ko8bbMZXWImr1IgrD5RNqXRo1rphVbGRKoxmIOSv7flr8uLrisKIQ==</DP><DQ>otSZlSq2qomgvgg7PaOLSS+F0TQ/i1emO0/tffhkqT4ah7BgE97xP6puJWZivjAteAGxrxHH+kPY0EY1AzRMNQ==</DQ><InverseQ>Sxyz0fEf5m7GrzAngLDRP/i+QDikJFfM6qPyr3Ub6Y5RRsFbeOWY1tX3jmV31zv4cgJ6donH7W2dSBPi67sSsw==</InverseQ><D>nVqofsIgSZltxTcC8fA/DFz1kxMaFHKFvSK3RKIxQC1JQ3ASkUEYN/baAElB0f6u/oTNcNWVPOqE31IDe7ErQelVc4D26RgFd5V7dSsF3nVz00s4mq1qUBnCBLPIrdb0rcQZ8FUQTsd96qW8Foave4tm8vspbM65iVUBBVdSYYE=</D></RSAKeyValue>"
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Using rsa As New RSACryptoServiceProvider()
rsa.FromXmlString(prikey)
' 加密对象
Dim f As New RSAPKCS1SignatureFormatter(rsa)
f.SetHashAlgorithm("SHA1")
Dim source As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(textBox1.Text)
Dim sha As New SHA1Managed()
Dim result As Byte() = sha.ComputeHash(source)
Dim s As String = Convert.ToBase64String(result)
MessageBox.Show("s=" & s.Length)
Dim b As Byte() = f.CreateSignature(result)
textBox2.Text = ""
'string s = System.Text.Encoding.Default.GetString(b, 0, 9);
textBox2.Text = Convert.ToBase64String(b)
End Using
textBox3.Text = pubkey
textBox4.Text = prikey
Dim str As String = textBox2.Text
' string aa=MD5.Create(str).ToString();
End Sub
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button2.Click
Using rsa As New RSACryptoServiceProvider()
rsa.FromXmlString(pubkey)
Dim f As New RSAPKCS1SignatureDeformatter(rsa)
f.SetHashAlgorithm("SHA1")
Dim key As Byte() = Convert.FromBase64String(textBox2.Text)
Dim sha As New SHA1Managed()
Dim name As Byte() = sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(textBox1.Text))
Dim s As String = Convert.ToBase64String(name)
If f.VerifySignature(name, key) Then
MessageBox.Show("Succese!")
Else
MessageBox.Show("Falied!")
End If
End Using
End Sub
Private Sub getKeys()
Using ras As New RSACryptoServiceProvider()
'公匙
pubkey = ras.ToXmlString(False)
'私匙
prikey = ras.ToXmlString(True)
End Using
End Sub
End Class
这个算法就是 System.Security.Cryptography 名称空间的 RSAPKCS1SignatureFormatter 类(用来生成注册码)和 RSAPKCS1SignatureDeformatter 类(用来在客户端验证注册码)。验证过程如下:
首先,需要生成一个公钥和私钥对,当然,依靠人是无法生成的,我们可以通过 System.Security.Cryptography 名称空间的RSACryptoServiceProvider 类来生成公钥/私钥对。
可以使用getKeys()方法获得公匙和私匙,但是如果要验证还是要在得到公匙和私匙后保持不变,因为这个方法生成的公匙和私匙都是一直在变化的,所以要有一个确定的公匙和私匙。
button1的Click事件中是生成注册码的过程,用 RSAPKCS1SignatureFormatter 类来生成注册码。
其中又用了sha1加密算法加密你的userId,这样可能起到的更好的保密效果。注册码验证是在button2的click事件中。
总的来说,就是用私匙生成注册码再用公匙去验证。可以自己做个有私匙的注册机用于生成注册码,在程序中使用公匙去验证,这样整个软件的安全性就比较高了。客户端代码是没有私钥的,即使有人把程序集的代码反编译了也没有用。
如果觉得172位的验证码太长了,不好用,可以用了其中的sha1算法作了一个28位的。
[本日志由 tiancao1001 于 2017-05-08 06:06 PM 编辑]
|
tiancao1001 于 2017-05-08 06:06 PM 发表评论:
Public Class Form1
Dim pubkey As String = "<RSAKeyValue><Modulus>xe3teTUwLgmbiwFJwWEQnshhKxgcasglGsfNVFTk0hdqKc9i7wb+gG7HOdPZLh65QyBcFfzdlrawwVkiPEL5kNTX1q3JW5J49mTVZqWd3w49reaLd8StHRYJdyGAL4ZovBhSTThETi+zYvgQ5SvCGkM6/xXOz+lkMaEgeFcjQQs=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"
Dim prikey As String = "<RSAKeyValue><Modulus>xe3teTUwLgmbiwFJwWEQnshhKxgcasglGsfNVFTk0hdqKc9i7wb+gG7HOdPZLh65QyBcFfzdlrawwVkiPEL5kNTX1q3JW5J49mTVZqWd3w49reaLd8StHRYJdyGAL4ZovBhSTThETi+zYvgQ5SvCGkM6/xXOz+lkMaEgeFcjQQs=</Modulus><Exponent>AQAB</Exponent><P>5flMAd7IrUTx92yomBdJBPDzp1Kclpaw4uXB1Ht+YXqwLW/9icI6mcv7d2O0kuVLSWj8DPZJol9V8AtvHkC3oQ==</P><Q>3FRA9UWcFrVPvGR5bewcL7YqkCMZlybV/t6nCH+gyMfbEvgk+p04F+j8WiHDykWj+BahjScjwyF5SGADbrfJKw==</Q><DP>b4WOU1XbERNfF3JM67xW/5ttPNX185zN2Ko8bbMZXWImr1IgrD5RNqXRo1rphVbGRKoxmIOSv7flr8uLrisKIQ==</DP><DQ>otSZlSq2qomgvgg7PaOLSS+F0TQ/i1emO0/tffhkqT4ah7BgE97xP6puJWZivjAteAGxrxHH+kPY0EY1AzRMNQ==</DQ><InverseQ>Sxyz0fEf5m7GrzAngLDRP/i+QDikJFfM6qPyr3Ub6Y5RRsFbeOWY1tX3jmV31zv4cgJ6donH7W2dSBPi67sSsw==</InverseQ><D>nVqofsIgSZltxTcC8fA/DFz1kxMaFHKFvSK3RKIxQC1JQ3ASkUEYN/baAElB0f6u/oTNcNWVPOqE31IDe7ErQelVc4D26RgFd5V7dSsF3nVz00s4mq1qUBnCBLPIrdb0rcQZ8FUQTsd96qW8Foave4tm8vspbM65iVUBBVdSYYE=</D></RSAKeyValue>"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using rsa As New RSACryptoServiceProvider()
rsa.FromXmlString(prikey)
' 加密对象
Dim f As New RSAPKCS1SignatureFormatter(rsa)
f.SetHashAlgorithm("SHA1")
Dim source As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(TextBox1.Text)
Dim sha As New SHA1Managed()
Dim result As Byte() = sha.ComputeHash(source)
Dim s As String = Convert.ToBase64String(result)
MsgBox(s)
Dim b As Byte() = f.CreateSignature(result)
TextBox2.Text = ""
TextBox2.Text = Convert.ToBase64String(b)
End Using
TextBox3.Text = pubkey
TextBox4.Text = prikey
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Using rsa As New RSACryptoServiceProvider()
rsa.FromXmlString(pubkey)
Dim f As New RSAPKCS1SignatureDeformatter(rsa)
f.SetHashAlgorithm("SHA1")
Dim key As Byte() = Convert.FromBase64String(TextBox2.Text)
Dim sha As New SHA1Managed()
Dim name As Byte() = sha.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(TextBox1.Text))
Dim s As String = Convert.ToBase64String(name)
MsgBox(s)
If f.VerifySignature(name, key) Then
MessageBox.Show("Succese!")
Else
MessageBox.Show("Falied!")
End If
End Using
End Sub
End Class
发表评论 - 不要忘了输入验证码哦! |