none
Using RSA Keys to generate large Primes

    Question

  • Below is the code I am using to generate large Prime numbers. It regularly outputs even numbers as well as negative. Hopefully someone can have a look and tell me where my malfunction is. I'm also wondering how to properly convert a negative BigInteger into its unsigned equivalent. Requires a Reference to System.Numerics

    Option Strict On
    Imports System.Security.Cryptography
    Imports System.Xml
    Imports System.IO
    Imports System.Numerics
    Public Class Form1
        WithEvents CBKeySizes As New ComboBox With {.Location = New Point(10, 10), .Size = New Size(120, 20)}
        WithEvents BTNGo As New Button With {.Location = New Point(150, 10), .Size = New Size(80, 20), .Text = "Generate"}
        WithEvents TXTPrimes As New TextBox With {.Location = New Point(10, 40), .Size = New Size(500, 200), .Multiline = True, _
                            .Anchor = CType(AnchorStyles.Top + AnchorStyles.Bottom + AnchorStyles.Left + AnchorStyles.Right, AnchorStyles)}
        WithEvents CBreverse As New CheckBox With {.Checked = True, .Text = "Reverse Bytes ?", .Location = New Point(250, 10), .Size = New Size(120, 20)}
        Dim xmlKeys As String
        Dim PString As String
        Dim QString As String
        Private Function Base64_To_Decimal(InputString As String) As String
            Dim b As Byte() = Convert.FromBase64String(InputString)
            If CBreverse.Checked Then Array.Reverse(b)
            Dim BigInt As BigInteger = New BigInteger(b)
            Return BigInt.ToString
        End Function
        Private Function Base64_To_Hex(InputString As String) As String
            Dim OutputString As String = ""
            Dim Bytes() As Byte = Convert.FromBase64String(InputString)
            If CBreverse.Checked Then Array.Reverse(Bytes)
            For Each b As Byte In Bytes
                OutputString &= b.ToString("x2")
            Next
            Return OutputString
        End Function
        Private Sub DisplayXMLValues(inputstring As String)
            'Apparently XML wants newline chars after each section - RSA Keys lack this, so ....
            inputstring = inputstring.Replace("</Modulus>", "</Modulus>" & vbNewLine)
            inputstring = inputstring.Replace("</Exponent>", "</Exponent>" & vbNewLine)
            inputstring = inputstring.Replace("</P>", "</P>" & vbNewLine)
            inputstring = inputstring.Replace("</Q>", "</Q>" & vbNewLine)
            inputstring = inputstring.Replace("</DP>", "</DP>" & vbNewLine)
            inputstring = inputstring.Replace("</DQ>", "</DQ>" & vbNewLine)
            inputstring = inputstring.Replace("</InverseQ>", "</InverseQ>" & vbNewLine)
            inputstring = inputstring.Replace("</D>", "</D>" & vbNewLine)
            Dim xmlString As String = inputstring
    
            ' Create an XmlReader
            Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))
                Dim MyXMLString As String = ""
                'ignore Modulus, Exponent, DP, DQ, InverseQ and D
                reader.ReadToFollowing("P")
                MyXMLString = reader.ReadElementContentAsString()
                PString = MyXMLString
                TXTPrimes.AppendText("P (Base 64)   : " & PString & vbNewLine)
                TXTPrimes.AppendText("P (Prime-Dec) : " & Base64_To_Decimal(MyXMLString) & vbNewLine)
                TXTPrimes.AppendText("P (Prime-Hex) : " & Base64_To_Hex(MyXMLString) & vbNewLine & vbNewLine)
                reader.ReadToFollowing("Q")
                MyXMLString = reader.ReadElementContentAsString()
                QString = MyXMLString
                TXTPrimes.AppendText("Q (Base 64)   : " & QString & vbNewLine)
                TXTPrimes.AppendText("Q (Prime-Dec) : " & Base64_To_Decimal(MyXMLString) & vbNewLine)
                TXTPrimes.AppendText("Q (Prime-Hex) : " & Base64_To_Hex(MyXMLString) & vbNewLine)
            End Using
        End Sub
        Private Sub BTNGo_Click(sender As System.Object, e As System.EventArgs) Handles BTNGo.Click
            TXTPrimes.Clear()
            Using rsa As New RSACryptoServiceProvider(CInt(CBKeySizes.Text))
                xmlKeys = rsa.ToXmlString(True)
                DisplayXMLValues(xmlKeys.ToString)
                TXTPrimes.AppendText(vbNewLine & vbNewLine & xmlKeys)
            End Using
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.Size = New Size(550, 300)
            Me.Text = "Primes via RSA Key Generation"
            Me.Font = New Font("Consolas", 8)
            Me.Controls.Add(CBKeySizes)
            Me.Controls.Add(BTNGo)
            Me.Controls.Add(TXTPrimes)
            Me.Controls.Add(CBreverse)
            Using RSATest As New RSACryptoServiceProvider
                Dim legalKeySizes() As KeySizes = RSATest.LegalKeySizes
                If (legalKeySizes.Length > 0) Then
                    For i As Integer = 0 To legalKeySizes.Length - 1 Step 1
                        For K As Integer = legalKeySizes(i).MinSize To legalKeySizes(i).MaxSize Step legalKeySizes(i).SkipSize
                            CBKeySizes.Items.Add(K)
                        Next
                    Next
                    CBKeySizes.SelectedIndex = 0
                Else
                    MessageBox.Show("Fatal Error getting Legal Key Sizes", "Cannot Continue", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                    End
                End If
            End Using
        End Sub
    End Class
    

    
    

    Something is wrong with my base64 to BigInteger and Base64 to HEX and I cant figure it out.

    Here is an example

    P (Base 64)   : 8GQR8fa8Hm7Xe1pFHe6vVcDZsqQCf105
    P (Prime-Dec) : 1406591083805472782664110490276708020851235597056102982896
    P (Prime-Hex) : f06411f1f6bc1e6ed77b5a451deeaf55c0d9b2a4027f5d39

    Wednesday, May 25, 2016 8:47 PM

Answers

  • Devon

      -- a Big integer is SIGNED if the high byte of the array is <> 0

            then, if the high bit of the high byte is set the value is negative

    ----------------------------------------------

     -- a Big integer is UNSIGNED if the high byte of the array = 0

    ----------------------------------------------

    Therefore to get an unsigned big integer from an array, we need to add to the array an extra byte equal to zero

            Dim a As New BigInteger({255, 255})
            TextBox3.Text = a.ToString
            Dim b As New BigInteger({255, 255, 0})
            TextBox4.Text = b.ToString

    Textbox3 will contain -1  

    Textbox4 will contain 65535

    ------------------------------------------

    The value p  and q are unsigned number --- therefore your function should be like this(Adding a last byte equal to zero to the array)

        Private Function Base64_To_Decimal(InputString As String) As String
            Dim b As Byte() = Convert.FromBase64String(InputString)
            Array.Reverse(b)
            ReDim Preserve b(b.Length)
            Dim BigInt = New BigInteger(b)
            Return BigInt.ToString
        End Function


     


    Luc




    Friday, May 27, 2016 3:50 PM

All replies

  • Even though I suppose it is in Java perhaps (looks like C# to me) this link could shed light on converting BigInteger to unsigned equivalent. I've no idea though.

    interpret a negative number as unsigned with BigInteger java


    La vida loca

    Thursday, May 26, 2016 2:35 AM
  • Devon

      -- a Big integer is SIGNED if the high byte of the array is <> 0

            then, if the high bit of the high byte is set the value is negative

    ----------------------------------------------

     -- a Big integer is UNSIGNED if the high byte of the array = 0

    ----------------------------------------------

    Therefore to get an unsigned big integer from an array, we need to add to the array an extra byte equal to zero

            Dim a As New BigInteger({255, 255})
            TextBox3.Text = a.ToString
            Dim b As New BigInteger({255, 255, 0})
            TextBox4.Text = b.ToString

    Textbox3 will contain -1  

    Textbox4 will contain 65535

    ------------------------------------------

    The value p  and q are unsigned number --- therefore your function should be like this(Adding a last byte equal to zero to the array)

        Private Function Base64_To_Decimal(InputString As String) As String
            Dim b As Byte() = Convert.FromBase64String(InputString)
            Array.Reverse(b)
            ReDim Preserve b(b.Length)
            Dim BigInt = New BigInteger(b)
            Return BigInt.ToString
        End Function


     


    Luc




    Friday, May 27, 2016 3:50 PM
  • Thank you very much Crazypennie

    Friday, May 27, 2016 10:00 PM