0

Okay, so I am trying to make my program take whatever your key is, and loop through each character of the key, then find the ascii code for each character code, and then loop through each character of the message, finding the ascii code of each of them, and adding the key code to the message code, doing this for each character in the key, to each character in the message. I ran into a little problem, it changes the message right after the first letter is added, and I can't figure out how to fix it, any help would be great!

Basically, all I want is to that the ascii code for the key characters and add them to the ascii code for the message characters, then convert that final code back to the new characters in the message text. Using this:

tbxMessage.Text = (AscW(Mid(tbxMessage.Text, xForMess, 1)) + AscW(vTemp))

Here is everything I've got so far:

Public Class Form1

Function fctEncryptDecrypt(pMess As String, pKey As String) As String
    If Len(tbxMessage.Text) > 0 Then
        Dim xForKey As Integer
        Dim xForMess As Integer
        Dim intKey As Integer
        Dim intMessage As Integer
        Dim strAsciiKeyChar As String
        Dim intAsciiKeyChar As Integer
        Dim strAsciiMesChar As String
        Dim intAsciiMesChar As Integer
        Dim vTemp As String
        Dim vTempMess As String

        intKey = Len(tbxKey.Text)
        intMessage = Len(tbxMessage.Text)
        For xForKey = 1 To intKey
            strAsciiKeyChar = Mid(tbxKey.Text, xForKey, 1)
            intAsciiKeyChar = AscW(strAsciiKeyChar)
            vTemp = intAsciiKeyChar
            For xForMess = 1 To intMessage
                strAsciiMesChar = Mid(tbxMessage.Text, xForMess, 1)
                intAsciiMesChar = AscW(strAsciiMesChar)
                vTempMess = vTemp + intAsciiMesChar
                tbxMessage.Text = (AscW(Mid(tbxMessage.Text, xForMess, 1)) + AscW(vTemp))
            Next xForMess
        Next xForKey
        Label1.Text = vTemp

    Else
        MessageBox.Show("No Message Found")
    End If
End Function

Private Sub btnEncrypt_Click(sender As System.Object, e As System.EventArgs) Handles btnEncrypt.Click
    fctEncryptDecrypt(tbxMessage.Text, tbxKey.Text)
End Sub
End Class
Nick Riggi
  • 49
  • 1
  • 5

1 Answers1

0
tbxMessage.Text = (AscW(Mid(tbxMessage.Text, xForMess, 1)) + AscW(vTemp))

in the inner For loop is setting the value of the text box to a number.

I'd expect the use of a temporary string variable that collects

Chr((intAsciiKeyChar + intAsciiMesChar) mod 256)

I'd also expect the key to be applied one letter at a time over the message. Something like:

Dim i as Integer
Dim s as String
Dim sKey as String
Dim sMesg as String
Dim intCharacter as Integer
s = ""
For i = 1 to len(tbxMessage.Text)
    sKey = Mid(tbxKey.Text, (i mod Len(tbxKey)) + 1, 1)
    sMesg = Mid(tbxMessage.Text, i, 1)
    intCharacter = (WAsc(sKey) + WAsc(sMesg)) mod 256
    s = s & Chr(intCharacter)
Next
tbxMessage.Text = s
dfrevert
  • 366
  • 5
  • 17