0

Here's the code I am not sure if there's any hidden error but on runtime when trying to import the rsa parameters it pops up that error

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)

In here when generating RSA numbers, I can not always get the correct numbers so i am using this website to check that i have get the correct RSA numbers and the D and Exponent was not = 0

Exponent=e

Everytime I get Exponent and D, I will always use this website to check P,Q,Exponent and D to make sure it's correct

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

In here all D,P,Q,DP,DQ,Exponent,Modulus,InverseQ value has been calculated and checked

    End If
End Sub

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

If it's possible try check the coding here, perhaps this place was the place that i do it wrongly

I initially thought of using BitCoverter.GetBytes() to convert the long and Ulong datatype value

But in the end, the bitconverter.getbytes() doesn't work for me so i have to first convert all those long and ulong data type values into string then use BYTE.PARSE() to convert the string into byte value

That's how i do it

If let's any experts here confirmed that the coding above worked and all the values are correct then try to check is there any potential error here

    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

What I am trying to do in the generate button was to create the suitable parameters for RSA.

The parameters I was actually able to generate them but in either ULong or Long data type.

When i first convert them into array of Bytes I have considered to use BitConverter but it doesn't work at least in my case.

The only way i am only able to make them into array of Bytes was by making the ULong or Long data type variable into string.

Then convert those string into Byte then put them into ByteArray I hope it was correct but it is out of my reach for now.

Any ideas on how can i make the parameter data to be accepted as rsa parameter?

Hern
  • 3
  • 3
  • The shortest key any of the built-in types can load is RSA-384. The biggest you can produce with longs is RSA-64. If you’re not using BigInteger you’re too small for the platform. – bartonjs Nov 27 '19 at 17:35
  • @bartonjs u mean all parameter for rsa must be more than 64 bit or just a few of them?? instead of biginteger can i use Decimal?? – Hern Nov 28 '19 at 03:42
  • @bartonjs is the conversion from long to byte in the codes has no error? based on your opinion? – Hern Nov 28 '19 at 03:45
  • @bartonjs is the code to calculate the numbers was also correct? – Hern Nov 28 '19 at 03:55
  • your array building looks quite suspect-like you’re doing a base 10 operation instead of base-256. Decimal is also too small. You need significantly bigger numbers, and then just use BigInteger’s ToArray (but you have to reverse it to get back to Big Endian) – bartonjs Nov 28 '19 at 04:44
  • Big Endian ? Why do i need to do like that? – Hern Nov 28 '19 at 08:28
  • erm i can't use base 10 to do the operation? given the coding i changed to biginteger?? – Hern Nov 28 '19 at 08:29
  • If it's possible mind switching to this link? I have asked another similar questions again but this time I used BigInteger and the number was 384 bit https://stackoverflow.com/questions/60548866/runtime-error-system-security-cryptography-cryptographicexception-bad-data – Hern Mar 05 '20 at 15:37

0 Answers0