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