0

I'm trying to write a program that encrypts and decrypts short messages in RSA, so I've been using the BigInteger object to store the large numbers. The encryption works properly, but when I run my decryption part of the code, it gives me an overflow exception error, Value was either too large or too small for an Int32 at the line that uses the BigInteger.DivRem() method. This is weird because I don't even use any Integer objects in this block of code. Can anybody tell me what I'm doing wrong here?

    Private Sub DecryptButton_Click(ByVal sender As System.Object, ByVal e As    System.EventArgs) Handles DecryptButton.Click
    Dim cipherText As BigInteger
    Dim cipherTextString As String
    Dim n, d, counter As BigInteger
    Dim f As BigInteger = 0
    Dim asciiArray() As Byte = Nothing
    Dim splits As String = ""
    Dim decryptedText As String
    counter = 0

    n = BigInteger.Parse("really large number")
    d = BigInteger.Parse("really large number")

    BigInteger.DivRem(BigInteger.Pow(BigInteger.Parse(CipherBox.Text), d), n, f)

    cipherText = f

    cipherTextString = cipherText.ToString

    For i As Integer = 0 To cipherTextString.Length - 1
        Dim c As Char = cipherTextString(i)
        If splits.Length < 2 Then
            splits = splits + c.ToString
        Else
            asciiArray(counter) = Byte.Parse(splits)
            splits = Nothing
            counter = counter + 1
        End If
    Next

    decryptedText = System.Text.ASCIIEncoding.ASCII.GetString(asciiArray)
    MessageText.Text = decryptedText

End Sub

1 Answers1

1

Yes you are using Integers in your code. You are just not aware of it.

Switch Option Strict On and your code will fail to compile because with d and counter declared as BigInteger it may result in data loss or overflow which is something Option Strict does not allow.

You should always use Option Strict On and just turn it off for the special cases (Late binding, etc) and then only in a partial class.

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143