如何制作一个"密钥生成器"知道公式

时间:2014-10-30 19:32:44

标签: vb.net vb.net-2010

我有公式来检查9个整数,

第一位数字(d1)必须为:1,2,5,6,8或9 最后一位(d9)必须为:0或9 9xd1 + 8xd2 + 7xd3 + 6xd4 + 5xd5 + 4xd6 + 3xd7 + 2xd8 + d9 mod 11 = 0

我可以“验证”密钥,但是我怎样才能生成更多这样的信息,知道它的条件是正确的?

如何从0到9生成9个不同的整数并在此公式下检查它们?

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

  1. 随机生成前7位数字,计算这些数字的公式。
  2. 将第9位数字的值设置为9,并将其添加到公式中。
  3. 根据导致公式结果为mod 11 = 0的公式结果的mod计算第8位的值。
  4. 对于尝试执行此操作导致mod 11 = 9的异常情况,请将第9位设置为0.
  5. 实现:

    Private randGen As New Random()
    
    Function GenNum() As Integer
       Dim digits(0 To 8) As Integer
       GenNum = 0
       Dim checkSum As Integer
       digits(0) = randGen.Next(6) + 1
       If digits(0) >= 3 Then digits(0) += 2
       If digits(0) >= 7 Then digits(0) += 1
       checkSum += digits(0) * 9
       For d As Integer = 1 To 6
          digits(d) = randGen.Next(10)
          checkSum += digits(d) * (9 - d)
       Next
       digits(8) = 9
       checkSum += digits(8)
       If (checkSum Mod 11) Mod 2 = 1 Then
          digits(7) = (11 - (checkSum Mod 11)) \ 2
       Else
          digits(7) = ((12 - (checkSum Mod 11)) \ 2 + 4) Mod 10
       End If
       checkSum += digits(7) * 2
       If checkSum Mod 11 = 9 Then digits(8) = 0
       Dim pow10 As Integer = 1
       For d As Integer = 8 To 0 Step -1
          GenNum += pow10 * digits(d)
          pow10 *= 10
       Next
    End Function
    

答案 1 :(得分:0)

我可以帮助您生成0到9之间的整数。

以下是您的表单应如何显示: enter image description here

这是代码:

Public Class Form1
Dim NumRandom As Random = New Random
Dim X, Y, Z As Integer

Private Sub GenerateBUT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateBUT.Click
    Dim a(9), i, j, RN As Integer
    Dim flag As Boolean
    flag = False
    i = 1
    a(j) = 1
    Do While i <= 9
        Randomize()
        RN = CInt(Int(9 * Rnd()) + 1)
        For j = 1 To i
            If (a(j)) = RN Then
                flag = True
                Exit For
            End If
        Next
        If flag = True Then
            flag = False
        Else
            a(i) = RN
            i = i + 1S
        End If
    Loop
    Label1.Text = a(1)
    Label2.Text = a(2)
    Label3.Text = a(3)
    Label4.Text = a(4)
    Label5.Text = a(5)
    Label6.Text = a(6)
    Label7.Text = a(7)
    Label8.Text = a(8)
    Label9.Text = a(9)
    Z = Label4.Text
    Y = Label5.Text
    X = Z + Y
    X = X - Label3.Text
    If X > 1 And X < 10 Then
        X = NumRandom.Next(1, 7)
        If X = 1 Then
            Label1.Text = "0"
        ElseIf X = 2 Then
            Label2.Text = "0"
        ElseIf X = 3 Then
            Label3.Text = "0"
        ElseIf X = 4 Then
            Label4.Text = "0"
        ElseIf X = 5 Then
            Label5.Text = "0"
        ElseIf X = 6 Then
            Label6.Text = "0"
        ElseIf X = 7 Then
            Label7.Text = "0"
        End If
    End If
End Sub

结束班