loop order (again)

ARC

Well-known member
Joined
Sep 9, 2006
Messages
63
Location
Minnesota
Programming Experience
Beginner
I'm trying to get the 'space-less' messege, one letter at a time, and convert that letter into a number between 1 and 26. I'm doing so by turning the letter into ascii value, then minus 96. a = 97 so 97 - 96 = 1. a is the first letter of the alphabet. Then, i use that number (1) in cunjunction with the same process to the keyword letter, to pull a number out of an array i have created in the formload. I then take that pulled number and CHR() it to give me the codedChar, then display it in the uiOutput text box.

The problem im having is with my loop. It's not looping past the first letter for either the keyword, or the messege... but it does fire the correct number of times according to mLength.

I know that this isnt controling the To.Lower yet, but for now i'm just making sure i enter the keyword and messege in all lowercase. I know how to do that, so that's not what i'm worried about yet.

Any suggestions would be great.

EDIT:: the only thing I can think of, is it's some problem with the math being outside of the loop. So i'm gunna try that next, by bringing what i can inside.

VB.NET:
Dim messege As String = Me.uiInput.Text.Replace(" ", "")
        Dim keyWord As String = Me.uiKeyWord.Text

        Dim mLength As Integer = Len(messege)
        Dim kLength As Integer = Len(keyWord)

        Dim mCounter As Integer = 1
        Dim kCounter As Integer = 1

        Dim mTempLetter As String = Microsoft.VisualBasic.Left(messege, mCounter)
        Dim kTempLetter As String = Microsoft.VisualBasic.Left(keyWord, kCounter)

        Dim mASC As Integer = (Asc(mTempLetter) - (96))

        Dim kASC As Integer = (Asc(kTempLetter) - (96))

        Dim codedChar As String


        Me.uiOutput.Text = ""


        Do While mCounter <= mLength

            codedChar = Chr(aGrid(kASC, mASC))

            Me.uiOutput.AppendText(codedChar)

            mCounter += 1
            kCounter += 1


        Loop


This didnt work either::

VB.NET:
Dim mLength As Integer
        Dim kLength As Integer
        Dim mTempLetter As String
        Dim kTempLetter As String

        Dim mCounter As Integer
        Dim kCounter As Integer

        Dim ascK As Integer
        Dim ascM As Integer

        Dim kTemp As Integer
        Dim mTemp As Integer

        Dim codedInt As Integer
        Dim codedChar As String


        mLength = Len(Me.uiInput.Text)
        kLength = Len(Me.uiKeyWord.Text)

        mCounter = 1
        kCounter = 1


        For mCounter = 1 To mLength

            mTempLetter = Microsoft.VisualBasic.Left(Me.uiInput.Text, mCounter)
            kTempLetter = Microsoft.VisualBasic.Left(Me.uiKeyWord.Text, kCounter)

            ascM = Asc(mTempLetter)
            ascK = Asc(kTempLetter)

            mTemp = ((ascM) - (96))
            kTemp = ((ascK) - (96))

            codedInt = aGrid(mTemp, kTemp)
            codedChar = Chr(codedInt)

            Me.uiOutput.AppendText(codedChar)

            kCounter += 1

        Next mCounter
 
Last edited:
Why? Are you aware that vb.net supports encryption?
 
I have heard that it does, yes. But this is a specific method of cryptography that I'm trying to duplicate here. If you have any suggestions about why my loop isnt working, or at least that it's not this loop that's messing it up, that would be great!
 
Your code was going thru it but you set the item to encode outside the loop so it was converting the same thing every time thru. Try this:
VB.NET:
        Dim messege As String = Me.uiInput.Text.Replace(" ", "")
        Dim keyWord As String = Me.uiKeyWord.Text
        Dim mLength As Integer = Len(messege)
        Dim kLength As Integer = Len(keyWord)
        Dim mCounter As Integer = 0
        Dim kCounter As Integer = 0
        Dim mTempLetter As String = String.Empty
        Dim kTempLetter As String = String.Empty
        Dim mASC As Integer = 0
        Dim kASC As Integer = 0
        Dim codedChar As String = String.Empty

        Me.uiOutput.Text = ""
        Do While mCounter < mLength 
            mTempLetter = messege.Substring(mCounter, 1)
            kTempLetter = keyWord.Substring(kCounter, 1)
            mASC = (Asc(mTempLetter) - (96))
            kASC = (Asc(kTempLetter) - (96))
            codedChar = Chr(aGrid(kASC, mASC))
            Me.uiOutput.AppendText(codedChar)
            mCounter += 1
            kCounter += 1
        Loop
You will notice I did away with the Microsoft.VisualBaisc stuff and used the substring method of the string. I move the mTempLetter and kTempLetter inside the loop.
Also Net is zero based so I changed mCounter to start at 0 and while less than mLength.
 
Hey

Now it's giving me errors whenever the mLength is longer then the kLength. The error actually says it's something on line 58, which is kTempLetter = keyWord.Substring(kCounter, 1):

VB.NET:
mTempLetter = messege.Substring(mCounter, 1)
kTempLetter = keyWord.Substring(kCounter, 1)

as long as my messege is shorter than, or equal to the kLength, it doesnt error. Which suggested to me that it had something to do with the kCounter going past kLength... It needs to reset once it hits kLength anyways, so I added the following ifthen, and it works now :) Thanks for the help man!


VB.NET:
Me.uiOutput.Text = ""
        Do While mCounter < mLength 
            mTempLetter = messege.Substring(mCounter, 1)
            kTempLetter = keyWord.Substring(kCounter, 1)
            mASC = (Asc(mTempLetter) - (96))
            kASC = (Asc(kTempLetter) - (96))
            codedChar = Chr(aGrid(kASC, mASC))
            Me.uiOutput.AppendText(codedChar)
            mCounter += 1
            kCounter += 1
            
            If kCounter >= kLength Then
                kCounter = 1
            End If

        Loop
 
Last edited:
hehe

And then to bring her on back... No debug errors... but it only decodes the first two code letters then does Z's for the remaining.
in:
VB.NET:
Dim code As String = Me.uiOutput.Text.Replace(" ", "")
        Dim keyWord As String = Me.uiKeyWord.Text.Replace(" ", "")
        Dim cLength As Integer = Len(code)
        Dim kLength As Integer = Len(keyWord)
        Dim cCounter As Integer = 0
        Dim kCounter As Integer = 0
        Dim cTempLetter As String = String.Empty
        Dim kTempLetter As String = String.Empty
        Dim cASC As Integer = 0
        Dim kASC As Integer = 0
        Dim x As Integer = 0
        Dim stackCounter As Integer = 0
        Dim decodedChar As String = String.Empty
        
        Me.uiInput.Text = ""
        Do While cCounter < cLength
            If kCounter > kLength Then
                kCounter = 0
            End If
            cTempLetter = code.Substring(cCounter, 1)
            kTempLetter = keyWord.Substring(kCounter, 1)
            cASC = Asc(cTempLetter)
            kASC = (Asc(kTempLetter) - (97))
           
            Do While stackCounter < 26
                If stackCounter > 25 Then
                    stackCounter = 0
                End if
                x = aGrid(kASC, stackCounter)
                decodedChar = Chr(((stackCounter) + (97)))
                If x = cASC Then Exit Do
                stackCounter += 1
kCounter += 1
            Loop

            Me.uiInput.AppendText(decodedChar)
            cCounter += 1
        Loop
 
Last edited:
After this code:
x = aGrid(kASC, stackCounter)
add:
Stop
When the program stops put your mouse over the x and get the value, then run then get value of x and so on.
It might be you are returning z from the aGrid.
 
here

I feel this is closer... I think my problem is with the inside loop, and my variables. the only way I could thnk of to reverse the process, was to get the Ascii value of stackCounter when aGrid(kCounter, stackCounter) = Asc(cTempLetter)

Seeing as asc(cTempLetter) is the coded character, aGrid( 'kCounter COLUMN, 'stackCounter ROW') should be the correct approach.

VB.NET:
Do While cCounter < cLength

            If kCounter > kLength Then
                kCounter = 0
            End If

            cTempLetter = code.Substring(cCounter, 1)
            kTempLetter = keyWord.Substring(kCounter, 1)

            cASC = Asc(cTempLetter)
            kASC = (Asc(kTempLetter) - (97))

            Do While stackCounter < 26

                decodedChar = Chr(((stackCounter) + (97)))

                x = aGrid(kCounter, stackCounter)

                If x = cASC Then Exit Do

                stackCounter += 1

            Loop

            Me.uiInput.AppendText(decodedChar)
            cCounter += 1
            kCounter += 1
        Loop
 
I'm trying to get the 'space-less' messege, one letter at a time, and convert that letter into a number between 1 and 26. I'm doing so by turning the letter into ascii value, then minus 96. a = 97 so 97 - 96 = 1. a is the first letter of the alphabet. Then, i use that number (1) in cunjunction with the same process to the keyword letter, to pull a number out of an array i have created in the formload. I then take that pulled number and CHR() it to give me the codedChar, then display it in the uiOutput text box.

OMG, why make this so hard for yourself:

VB.NET:
Public Class Form1

    Private tran_fwd As New Dictionary(Of Char, Char)
    Private tran_bak As New Dictionary(Of Char, Char)


    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        TextBox2.Text = ""
        For Each ch As Char In TextBox1.Text
            If tran_fwd.ContainsKey(ch) Then
                TextBox2.AppendText(tran_fwd(ch))
            Else
                TextBox2.AppendText("?"c)
            End If
        Next
    End Sub

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        Dim fwdStr As String = "abcdefghijklmnopqrstuvwxyz"
        Dim revStr As String = "qwertyuiopasdfghjklzxcvbnm"

        For i As Integer = 0 To fwdStr.Length - 1
            tran_fwd.Add(fwdStr(i), revStr(i))
            tran_bak.Add(revStr(i), fwdStr(i))
        Next

    End Sub
End Class

tran_bak isnt used, but it shouldnt be rocket science to figure out its purpose :)
 
It's still returning an error on the

kTempLetter = keyWord.Substring(kCounter, 1)

"Index and length must refer to a location within the string.
Parameter name: length."

Last time (way above) it was a problem with kCounter extending past kLength... but i added the same ifthen statement to fix it again... and i've tried putting it inside or outside of both the inside and outside loop, nothing worked.
VB.NET:
if kCounter > kLength then
   kCounter = 0
End if
VB.NET:
Dim code As String = Me.uiOutput.Text.Replace(" ", "")
        Dim keyWord As String = Me.uiKeyWord.Text.Replace(" ", "")
        Dim cLength As Integer = Len(code)
        Dim kLength As Integer = Len(keyWord)
        Dim cCounter As Integer = 0
        Dim kCounter As Integer = 0
        Dim cTempLetter As String = String.Empty
        Dim kTempLetter As String = String.Empty
        Dim cASC As Integer = 0
        Dim kASC As Integer = 0
        Dim x As Integer = 0
        Dim stackCounter As Integer = 0
        Dim decodedChar As String = String.Empty

        Me.uiInput.Text = ""

        Do While cCounter < cLength
            cTempLetter = code.Substring(cCounter, 1)
            kTempLetter = keyWord.Substring(kCounter, 1)
            cASC = Asc(cTempLetter)
            kASC = (Asc(kTempLetter) - (97))

            Do While stackCounter < 27
                If stackCounter = 26 Then
                    stackCounter = 0
                End If
                x = aGrid(kCounter, stackCounter)
                decodedChar = Chr(((stackCounter) + (97)))
                If x = cASC Then Exit Do
                stackCounter += 1
            Loop
            
            If kCounter > kLength Then
                kCounter = 0
            End If

            Me.uiInput.AppendText(decodedChar)
            cCounter += 1
            kCounter += 1
        Loop



    End Sub
 
Last edited:
nope, it's following the Vigenére cipher actually.

What I said:
...Then, i use that number (1) in cunjunction with the same process to get the keyword letter, to pull a number out of an array i have created in the formload. I then take that pulled number and CHR() it to give me the codedChar, then display it in the uiOutput text box.

I'm taking the first letter of the messege, AND the first letter of the Keyword, and then using those two letters as coordinants in aGrid to get me the coded letter.

i'll upload the whole project when im done, but for now, im just trying to revers the process of encrypting the messege text.

the only way I could thnk of to reverse the process, was to get the Ascii value of stackCounter when aGrid(kCounter, stackCounter) = Asc(cTempLetter)

is what i'm trying to do.
 
again though, youre making it too difficult. Reading http://en.wikipedia.org/wiki/Vigenère_cipher we see that vignere is quite a simple math:

VB.NET:
Imports System.Text

Public Class Form1

    Private numOf As New Dictionary(Of Char, Int32)
    Private chrOf As New Dictionary(Of Int32, Char)
    Private keyStr As String




    Private Sub encTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles encTextBox.TextChanged
        MakeKey(encTextBox.Text)

        resTextBox.Clear()

        Dim enc As String = encTextBox.Text.ToLower().Replace(" ", "")
        For i As Integer = 0 To enc.Length - 1
            resTextBox.AppendText(chrOf((numOf(enc(i)) + numOf(keyStr(i))) Mod 26))
        Next

    End Sub

    Private Sub decTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles decTextBox.TextChanged
        MakeKey(decTextBox.Text)

        resTextBox.Clear()

        Dim dec As String = decTextBox.Text.ToLower().Replace(" ", "")
        For i As Integer = 0 To dec.Length - 1
            resTextBox.AppendText(chrOf((26 + (numOf(dec(i)) - numOf(keyStr(i)))) Mod 26))
        Next
    End Sub

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        Dim alphabet As String = "abcdefghijklmnopqrstuvwxyz"

        For i As Integer = 0 To alphabet.Length - 1
            numOf.Add(alphabet(i), i)
            chrOf.Add(i, alphabet(i))
        Next
    End Sub


    Private Sub MakeKey(ByVal forText As String)
        If keyTextBox.Text.Length = 0 Then Exit Sub

        Dim keySB As New StringBuilder(keyTextBox.Text)
        While keySB.Length < forText.Length
            keySB.Append(keyTextBox.Text)
        End While
        keySB.Length = forText.Length

        keyStr = keySB.ToString().ToLower()
    End Sub
End Class
 
Back
Top