Hi,
This is my first time posting. I need some help with an assignment I'm working on. The goal is to encrypt a string of text using RSA. The user enters in two prime numbers under 1000. The program then figures out the public and private keys and the R value (P*Q). The problem I'm encountering is that the math does not always come out right. With some prime numbers, it encrypts the ascii value properly. With other numbers, it does not. I'm sure I did not explain this well enough, but if anyone could even think of some reasons why this isn't working, I would appreciate it so much. Thank you
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
'Clears textboxes
txtEnc.Clear()
txtSep.Clear()
'Declarations for variables follow
'For storage from two prime number input boxes
Dim prime1 As Int64
Dim prime2 As Int64
'Array for storing 1,2,3 etc for table to determine K
Dim K(0 To 20) As Int64
'Array for storing results of (U-1)*(V-1)
Dim otherRtimesK(0 To 20) As Int64
'Storage for private key
Dim privatekey As Int64
'Storage for public key
Dim publickey As Int64
'Boolean to signal if factor has been found
Dim factorfound As Boolean
'Storage of R
Dim R As Int64
'Storage of that other R thing
Dim otherR As Int64
'Stores ascii values for text to be encrypted
Dim plaintext(0 To 60) As Int64
'String inputed in the text box
Dim txtboxInput As String
'Stores individual characters inputed into text box
Dim sepInput(0 To 60) As String
'Used for temp storage during character seperation
Dim temp As String
'array for storing encrypted ascii values
Dim sepenc(0 To 60) As Int64
'Initializes sepInput array for use later
For w = 1 To 18 Step 1
sepInput(w) = 0
Next
'initializes factorfound
factorfound = False
'Create K column for determining Keys
For x = 1 To 20
K(x) = x
Next
'Get prime numbers from text boxes
prime1 = txtPrime1.Text
prime2 = txtPrime2.Text
'Compute R
R = (prime1 * prime2)
'Computer otherR
otherR = ((prime1 - 1) * (prime2 - 1))
'Display R and otherR for debugging purposes
txtDR.Text = otherR
'Do first step of first table dR * K
For y = 1 To 20
otherRtimesK(y) = (otherR * K(y)) + 1
Next
'This will be the test to determine the public key and private key
For z = 1 To 20
For t = 2 To 20
If ((otherRtimesK(z) Mod t) = 0 And (t <> (otherRtimesK(z) / t)) And (factorfound = False) And (t <> otherRtimesK(z))) Then
'this should be private
privatekey = otherRtimesK(z) / t
'this should be public
publickey = t
factorfound = True
End If
Next
Next
'Outputs the data retrieved to the form in labels
txtR.Text = R
txtPub.Text = publickey
txtPriv.Text = privatekey
'Puts textbox contents into variable
txtboxInput = txtPlain.Text
'This loop seperates the inputed text into individual characters
For h = 1 To txtboxInput.Length Step 1
'set temp to a piece of string txtboxInput with a starting position of h and a length of 1
temp = Mid(txtboxInput, h, 1)
'set the array sepInput position h to temp
sepInput(h) = temp
'Output the seperated text to text box for diasgnoses
txtSep.Text = txtSep.Text & sepInput(h) & " "
'set plaintext, position h to the asci value of the corresponding position in sepInput (the seperated characters)
plaintext(h) = Asc(sepInput(h))
Next
'Loop to encrypt the text using the standard formula. byte^public key mod R
For i = 1 To txtboxInput.Length Step 1
'Perform calculation and put in array at position i
sepenc(i) = (plaintext(i) ^ publickey) Mod R
'Display results in text box
txtEnc.Text = txtEnc.Text & sepenc(i) & " "
Next
End Sub