0

这是我不确定是否有任何隐藏错误的代码,但是在运行时尝试导入 rsa 参数时会弹出该错误

Imports System.Security.Cryptography

Imports System.Security

Imports System.Text

Imports System.IO


Public Class RSA_Test_Form
Public FactorList As New List(Of Integer)

Public FindFactor As Long

Public PString, QString, ModulusString, ExponentString, DString, DPString,
    DQString, InverseQString As String

Function ModInverse(ByVal a As Long, ByVal b As Long) As Long

    Dim b0 As Long = b
    Dim t As Long
    Dim q As Long
    Dim x0 As Long = 0
    Dim x1 As Long = 1

    If b = 1 Then Return 1

    While a > 1
        q = a \ b
        t = b
        b = a Mod b
        a = t
        t = x0
        x0 = x1 - q * x0
        x1 = t
    End While

    If x1 < 0 Then x1 += b0
    Return x1

End Function

Function gcd(ByVal n1 As Long, ByVal n2 As Long) As Long
    Dim i As Integer
    Dim minimum As Integer
    If n1 < n2 Then
        minimum = n1
    Else
        minimum = n2
    End If

    For i = minimum To 1 Step -1
        If n1 Mod i = 0 And n2 Mod i = 0 Then
            Return i
        End If
    Next
    Return gcd
End Function

Sub FindFactorFunction()
    Dim x As Long
    For x = 2 To FindFactor - 1
        If FindFactor Mod x = 0 Then
            FactorList.Add(x)
        End If
    Next
End Sub

Private Sub GenerateBTN_Click(sender As Object, e As EventArgs) Handles GenerateBTN.Click
    Dim Result As Long = 0
    Dim Result2 As Long = 0
    Dim Result3 As Long = 0
    Dim Random1, Random2 As New Random()
    Dim P, Q, Modulus As Long
    Dim Exponent, D, DP, DQ As New Nullable(Of Long)
    Dim InverseQ As New Nullable(Of ULong)
    Dim Modulus1 As Long = 0
    Dim LoopCount As Integer = 0
    Dim ls, ls2 As New List(Of Long)
    Dim PrimeString As String
    PrimeString = ""
    Using MyNewStreamReader As StreamReader = New StreamReader("AllPrimes.txt")
        PrimeString = MyNewStreamReader.ReadLine().ToString
        MessageBox.Show(PrimeString)
        While PrimeString <> "" And LoopCount <= 1249
            ls.Add(Long.Parse(PrimeString))
            LoopCount += 1
            PrimeString = ""
            PrimeString = MyNewStreamReader.ReadLine
        End While
    End Using
    Using MyNewStreamReader2 As StreamReader = New StreamReader("AllPrimes.txt")
        PrimeString = ""
        PrimeString = MyNewStreamReader2.ReadLine().ToString
        LoopCount = 0
        MessageBox.Show(PrimeString)
        While PrimeString <> ""
            If LoopCount >= 1250 And LoopCount <= 2499 Then
                ls2.Add(Long.Parse(PrimeString))
            End If
            LoopCount += 1
            PrimeString = ""
            PrimeString = MyNewStreamReader2.ReadLine
        End While
    End Using
    Dim rand = Random1.Next(0, ls.Count)
    Dim rand2 = Random2.Next(0, ls2.Count)
    P = ls(rand)
    Q = ls2(rand2)
    Result3 = gcd(P, Q)
    While Result3 <> 1
        rand = Random1.Next(0, ls.Count)
        rand2 = Random2.Next(0, ls2.Count)
        P = ls(rand)
        Q = ls2(rand2)
        Result3 = gcd(P, Q)
    End While
    MessageBox.Show("P= " & P & "Q= " & Q)
    Modulus = (P - 1) * (Q - 1)
    Modulus1 = Modulus + 1
    FindFactor = Modulus1
    FindFactorFunction()
    Dim Count As Integer = 0
    Dim Count2 As Integer = 0
    For A As Integer = 0 To FactorList.Count - 1
        Result = gcd(FactorList.ElementAt(A), Modulus)
        If Result = 1 Then
            Count += 1
        End If
    Next
    Dim PositionArray(Count) As Integer
    Count = 0
    For A As Integer = 0 To FactorList.Count - 1
        Result = gcd(FactorList.ElementAt(A), Modulus)
        If Result = 1 Then
            PositionArray(Count) = FactorList.ElementAt(A)
            Count += 1
        End If
    Next
    Dim Number1, Number2 As Long
    Dim GetResult As Boolean = False
    Count = 0
    If PositionArray.Count = 2 Then
        Exponent = PositionArray(0)
        D = PositionArray(1)
    Else
        While GetResult = False And Count <> PositionArray.Count
            For Count = 0 To PositionArray.Count - 1
                For Count2 = Count + 1 To PositionArray.Count - 1
                    Number1 = PositionArray(Count)
                    Number2 = PositionArray(Count2)
                    Result2 = Number1 * Number2 Mod Modulus
                    If Result2 = 1 Then
                        GetResult = True
                    End If
                    If GetResult = True Then
                        Exit While
                    End If
                Next
            Next
        End While
    End If
    Dim Selection As Integer = MessageBox.Show(Number1 & "=E  And  D= " & Number2, "Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
    If Selection = DialogResult.OK Then
        Exponent = Number1
        D = Number2
        DP = D * P
        DQ = D * Q
        InverseQ = ModInverse(Q, P)
        MessageBox.Show("DP= " & DP)
        MessageBox.Show("DQ= " & DQ)
        MessageBox.Show("InverseQ= " & InverseQ)

在这里生成 RSA 数字时,我不能总是得到正确的数字,所以我使用这个网站来检查我是否得到了正确的 RSA 数字,并且 D 和指数不 = 0

指数=e

每次我得到 Exponent 和 D 时,我都会使用这个网站检查 P、Q、Exponent 和 D 以确保它是正确的

https://www.cs.drexel.edu/~jpopyack/IntroCS/HW/RSAWorksheet.html

    End If
    If Exponent.HasValue And D.HasValue Then
        PString = P.ToString
        QString = Q.ToString
        ModulusString = Modulus.ToString
        ExponentString = Exponent.ToString
        DString = D.ToString
        DPString = DP.ToString
        DQString = DQ.ToString
        InverseQString = InverseQ.ToString

在这里所有的 D,P,Q,DP,DQ,Exponent,Modulus,InverseQ 值都已计算和检查

    End If
End Sub

Private Sub Number2ByteConverterBTN_Click(sender As Object, e As EventArgs) Handles Number2ByteConverterBTN.Click

如果可以尝试检查这里的编码,也许这个地方是我做错的地方

我最初想到使用 BitCoverter.GetBytes() 来转换 long 和 Ulong 数据类型值

但最后,bitconverter.getbytes() 对我不起作用,所以我必须首先将所有这些 long 和 ulong 数据类型值转换为字符串,然后使用 BYTE.PARSE() 将字符串转换为字节值

我就是这样做的

如果让我们这里的任何专家确认上面的编码有效并且所有值都是正确的,那么尝试检查这里是否有任何潜在的错误

    Dim PByte, QByte, ModulusByte, ExponentByte, DByte, DPByte, DQByte, InverseQByte As Byte()
    ReDim PByte(PString.Count)
    ReDim QByte(QString.Count)
    ReDim ModulusByte(ModulusString.Count)
    ReDim ExponentByte(ExponentString.Count)
    ReDim DByte(DString.Count)
    ReDim DPByte(DPString.Count)
    ReDim DQByte(DQString.Count)
    ReDim InverseQByte(InverseQString.Count)
    Dim TempPByte, TempQByte, TempModulusByte, TempExponentByte, TempDByte As New Byte
    Dim StringBuilder As New StringBuilder
    Dim StringBuilder2 As New StringBuilder
    Dim StringBuilder3 As New StringBuilder
    Dim StringBuilder4 As New StringBuilder
    Dim StringBuilder5 As New StringBuilder
    Dim StringBuilder6 As New StringBuilder
    Dim StringBuilder7 As New StringBuilder
    Dim StringBuilder8 As New StringBuilder
    For i As Integer = 0 To PString.Length - 1
        PByte(i) = Byte.Parse(PString.ElementAt(i))
        StringBuilder.Append(PByte(i).ToString)
    Next
    For i As Integer = 0 To QString.Length - 1
        QByte(i) = Byte.Parse(QString.ElementAt(i))
        StringBuilder2.Append(QByte(i).ToString)
    Next
    For i As Integer = 0 To ModulusString.Length - 1
        ModulusByte(i) = Byte.Parse(ModulusString.ElementAt(i))
        StringBuilder3.Append(ModulusByte(i).ToString)
    Next
    For i As Integer = 0 To ExponentString.Length - 1
        ExponentByte(i) = Byte.Parse(ExponentString.ElementAt(i))
        StringBuilder4.Append(ExponentByte(i).ToString)
    Next
    For i As Integer = 0 To DString.Length - 1
        DByte(i) = Byte.Parse(DString.ElementAt(i))
        StringBuilder5.Append(DByte(i).ToString)
    Next
    For i As Integer = 0 To DPString.Length - 1
        DPByte(i) = Byte.Parse(DPString.ElementAt(i))
        StringBuilder6.Append(DPByte(i).ToString)
    Next
    For i As Integer = 0 To DQString.Length - 1
        DQByte(i) = Byte.Parse(DQString.ElementAt(i))
        StringBuilder7.Append(DQByte(i).ToString)
    Next
    For i As Integer = 0 To InverseQString.Length - 1
        InverseQByte(i) = Byte.Parse(InverseQString.ElementAt(i))
        StringBuilder8.Append(InverseQByte(i).ToString)
    Next

    Dim MyRSAParams As New RSAParameters
    MyRSAParams.P = PByte
    MyRSAParams.Q = QByte
    MyRSAParams.Exponent = ExponentByte
    MyRSAParams.Modulus = ModulusByte
    MyRSAParams.D = DByte
    MyRSAParams.DP = DPByte
    MyRSAParams.DQ = DQByte
    MyRSAParams.InverseQ = InverseQByte
    Dim MyRSA As RSA
    MyRSA = RSA.Create()
    MyRSA.ImportParameters(MyRSAParams)
End Sub

Private Sub RSA_Test_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim temp, temp2 As Integer
    temp = gcd(1, 2)
    temp2 = gcd(2, 3)
    FindFactor = 0
End Sub
End Class

我在生成按钮中尝试做的是为 RSA 创建合适的参数。

我实际上能够生成它们的参数,但可以是 ULong 或 Long 数据类型。

当我第一次将它们转换为字节数组时,我考虑过使用 BitConverter,但至少在我的情况下它不起作用。

我只能将它们变成字节数组的唯一方法是将 ULong 或 Long 数据类型变量变成字符串。

然后将这些字符串转换为 Byte,然后将它们放入 ByteArray 我希望它是正确的,但现在我无法做到。

关于如何使参数数据被接受为 rsa 参数的任何想法?

4

0 回答 0