【发布时间】:2019-02-15 17:29:11
【问题描述】:
我正在尝试在我制作的软件上设置许可系统。这个想法是客户端收到我的软件的副本,软件检查它的硬件 ID 或特定计算机的其他一些唯一标识符,然后使用带有公钥的非对称加密从该标识符生成激活码。然后客户端将激活码发送给我,我使用我的私钥创建一个签名,然后我将其发回给他,软件会确认它。它还会定期检查唯一标识符是否仍然正确。我希望我理解正确的部分。
从this example 复制,我在VB 中编写了一个基本代码,用于创建签名(应该在我的计算机上完成)和检查签名(在客户端的计算机上完成):
Function RSAoperations(hardwareID As String)
Dim hashValue() As Byte = Encoding.UTF8.GetBytes(hardwareID)
Dim sha256 As SHA256 = SHA256Managed.Create()
Dim hash() As Byte = sha256.ComputeHash(hashValue)
Dim RSA As New RSACryptoServiceProvider()
'create signature
Dim RSAFormatter As New RSAPKCS1SignatureFormatter(RSA)
RSAFormatter.SetHashAlgorithm("SHA256")
Dim SignedHash() As Byte
SignedHash = RSAFormatter.CreateSignature(hash)
'check signature
Dim RSADeformatter = New RSAPKCS1SignatureDeformatter(RSA)
RSADeformatter.SetHashAlgorithm("SHA256")
If RSADeformatter.VerifySignature(hash, SignedHash) Then
Console.WriteLine("The signature was verified.")
Else
Console.WriteLine("The signature was not verified.")
End If
Dim Parameters As New RSAParameters
Parameters = RSA.ExportParameters(True)
'display the private key. Base64 encode the text to make display easier
Debug.Print("private key: " & Convert.ToBase64String(Parameters.D))
'display the public key. Base64 encode the text to make display easier
Debug.Print("public key: " & Convert.ToBase64String(Parameters.Modulus))
End Function
在每次运行此函数时,Dim RSA As New RSACryptoServiceProvider() 使用一组新的公钥和私钥创建一个新的 RSA 对象。但由于我想将公钥嵌入客户端将收到的软件副本中,我需要以某种方式创建一对密钥,将它们写下来,并让 RSA 接受客户端计算机上预先制作的公钥。正如我想象的那样,我必须在两台机器上创建那个 RSA 对象,但是告诉其中一台使用预先制作的公钥,同时告诉另一台使用预先制作的私钥。我多次使用 RSA API,但找不到与此类功能相关的任何方法。
我感觉我在某种程度上误解了整个过程以及我需要做些什么来实现我的目标。有人可以帮我解决这个问题吗?我在互联网上找到了许多使用 RSA 的示例,但没有一个解释如何在两台机器之间共享 RSA 密钥。
【问题讨论】:
标签: security encryption rsa