# Vinegere Cipher... Can You Please Help me...??? • ### Question

• ```here is the code for i have done so far...'en.wikipedia.org/wiki/Vigenère_cipher
texts = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
key = "CBA"
For i =  1 To Text.GetLength(texts)
For j = 1 To Text.GetLength(key)
textscode = Text.GetCharacterCode(Text.GetSubText(texts,i,1))
keycode = Text.GetCharacterCode(Text.GetSubText(key,j,1))
'repeat the key if you are at end of it.
If j > Text.GetLength(key) Then
j = 1
EndIf
'actual logic -> character from texts + character from key % 26 is encrypted charater as described on wikipedia
TextWindow.Write(Text.GetCharacter(Math.Remainder(65+(textscode-65)+(keycode-65),26)))
EndFor
EndFor```

They say working hard is good but i say working smart is best...

Tuesday, March 6, 2012 5:41 PM

• There are some questions about the logic first:

```      For j = 1 To Text.GetLength(key)
textscode = Text.GetCharacterCode(Text.GetSubText(texts,i,1))
keycode = Text.GetCharacterCode(Text.GetSubText(key,j,1))
If j > Text.GetLength(key) Then
j = 1
EndIf
EndFor
```

Consider the code above:

textscode only depends on i, so why is it in the loop for j?

j can never be > Text.GetLength(key) since it is in a loop over this upper limit, so the test can never be hit.  Then, in fact keycode is always set to the last character and so the j loop is unnecessary and only the last character of the key is being used.

These don't answer the question, but lots of redundant complicating code make it harder.

Note the following cipher can be deciphered using your decipher:

```Sub eNcrypt
texts = Text.ConvertToUpperCase(Controls.GetTextBoxText(txtMain))
key =  Text.ConvertToUpperCase(Controls.GetTextBoxText(txtKey))
keycode = Text.GetCharacterCode(Text.GetSubText(key,Text.GetLength(key),1))
For i =  1 To Text.GetLength(texts)
textscode = Text.GetCharacterCode(Text.GetSubText(texts,i,1))
charscrypt = 65+Math.Remainder((textscode-65)+(keycode-65),26)
charfromcode = Text.GetCharacter(charscrypt)
crypt = crypt + charfromcode
EndFor
Controls.SetTextBoxText(txtMain,crypt)
cleaning()
EndSub
```

So to check for non ascii codes, the following ignores them, but the final solution is the work on the cipher and key methods more.

```Sub eNcrypt
texts = Text.ConvertToUpperCase(Controls.GetTextBoxText(txtMain))
key =  Text.ConvertToUpperCase(Controls.GetTextBoxText(txtKey))
keycode = Text.GetCharacterCode(Text.GetSubText(key,Text.GetLength(key),1))
For i =  1 To Text.GetLength(texts)
textscode = Text.GetCharacterCode(Text.GetSubText(texts,i,1))
If (textscode < 65 Or textscode > 90) Then
crypt = crypt + Text.GetCharacter(textscode)
Else
charscrypt = 65+Math.Remainder((textscode-65)+(keycode-65),26)
charfromcode = Text.GetCharacter(charscrypt)
crypt = crypt + charfromcode
EndIf
EndFor
Controls.SetTextBoxText(txtMain,crypt)
cleaning()
EndSub

Sub deCrypt
enctexts = Text.ConvertToUpperCase(Controls.GetTextBoxText(txtMain))
key =  Text.ConvertToUpperCase(Controls.GetTextBoxText(txtKey))
keycode = Text.GetCharacterCode(Text.GetSubText(key,Text.GetLength(key),1))
For i =  1 To Text.GetLength(enctexts)
textscode = Text.GetCharacterCode(Text.GetSubText(enctexts,i,1))
If (textscode < 65 Or textscode > 90) Then
decrypt = decrypt + Text.GetCharacter(textscode)
Else
charscrypt = 65+Math.Remainder((textscode-65)-(keycode-65),26)
If charscrypt < 65 Then
charscrypt = charscrypt + 26
EndIf
charfromcode = Text.GetCharacter(charscrypt)
decrypt = decrypt + charfromcode
EndIf
EndFor
Controls.SetTextBoxText(txtMain,decrypt)
cleaning()
EndSub```

Monday, September 3, 2012 9:58 PM
• finally here is the final codes.

```'Vigenere Cipher By Amir CPS
'Version 1.0
'Date 3-09-2012

GraphicsWindow.Width = 600
GraphicsWindow.Height = 400
GraphicsWindow.Title = "Computer Implementation of Vigenere Cipher"
GraphicsWindow.Show()
Controls.SetSize(txtMain,580,310)
GraphicsWindow.DrawText(10,330,"Secret Key (shoud be less than the message)")
Controls.SetSize(txtKey,150,30)
Controls.ButtonClicked = do_Click

Sub do_Click
If Controls.LastClickedButton = "Button1" Then
If Controls.GetTextBoxText(txtKey) = "" Then
GraphicsWindow.ShowMessage("Enter the key first","Error")
Else
eNcrypt()
EndIf
ElseIf Controls.LastClickedButton = "Button2" Then
If Controls.GetTextBoxText(txtKey) = "" Then
GraphicsWindow.ShowMessage("Enter the key first","Error")
Else
deCrypt()
EndIf
EndIf
EndSub

Sub eNcrypt
repeatKey()
getStringCharCode()
getKeyCharCode()
For i = 1 To Text.GetLength(string)
If charscode[i] < 0 Or charscode[i] > 26 Then
encryptestring = Text.Append(encryptestring,Text.GetCharacter(charscode[i] + 65))
Else
encryptedcharcode[i] = 65 + Math.Remainder(keycharscode[i] + charscode[i],26)
encryptedchar[i] = Text.GetCharacter(encryptedcharcode[i])
encryptestring = Text.Append(encryptestring,encryptedchar[i])
EndIf
EndFor
Controls.SetTextBoxText(txtMain,"")
Controls.SetTextBoxText(txtMain,encryptestring)
encryptestring = ""
key = ""
Controls.SetTextBoxText(txtKey,"")
finalkey = ""
EndSub

Sub deCrypt
repeatKey()
getStringCharCode()
getKeyCharCode()
For i = 1 To Text.GetLength(string)
If charscode[i] < 0 Or charscode[i] > 26 Then
decryptestring = Text.Append(decryptestring,Text.GetCharacter(charscode[i] + 65))
Else
encryptedcharcode[i] = Math.Remainder(charscode[i] - keycharscode[i],26) + 65
If encryptedcharcode[i] < 65 Then
encryptedcharcode[i] = encryptedcharcode[i] + 26
EndIf
encryptedchar[i] = Text.GetCharacter(encryptedcharcode[i])
decryptestring = Text.Append(decryptestring,encryptedchar[i])
EndIf
EndFor
Controls.SetTextBoxText(txtMain,"")
Controls.SetTextBoxText(txtMain,decryptestring)
decryptestring = ""
key = ""
Controls.SetTextBoxText(txtKey,"")
finalkey = ""
EndSub

Sub repeatKey
string = Text.ConvertToUpperCase(Controls.GetTextBoxText(txtMain))
key = Text.ConvertToUpperCase(Controls.GetTextBoxText(txtKey))
i = 1
While Text.GetLength(finalkey)  < Text.GetLength(string)
If i > Text.GetLength(key) Then
i = 1
EndIf
keyl = Text.GetSubText(key,i,1)
finalkey = finalkey + keyl
i = i + 1
EndWhile
EndSub

Sub getStringCharCode
For i= 1 To Text.GetLength(string)
chars[i] = Text.GetSubText(string,i,1)
charscode[i] = Text.GetCharacterCode(chars[i]) - 65
EndFor
EndSub

Sub getKeyCharCode
For i = 1 To Text.GetLength(finalkey)
keychars[i] = Text.GetSubText(finalkey,i,1)
keycharscode[i] = Text.GetCharacterCode(keychars[i]) - 65
EndFor
EndSub```

They say working hard is good but i say working smart is best...

Tuesday, September 4, 2012 4:33 PM

### All replies

• I find this very insteresting. This should be in one of the Challenge of the Month (if it's not in it already).

What exactly the kind of help do you need?

I try your code and it returns a bunch of garbage because you are using Text.GetCharacterCode Method.

Tuesday, March 6, 2012 8:38 PM
• Maybe is this what you want to do?

TextWindow.Write(Text.GetCharacter(64+Math.Remainder((textscode-64)+(keycode-64),26)))

Tuesday, March 6, 2012 11:27 PM
• letter = Text.GetCharacterCode("C")
key = Text.GetCharacterCode("M")
'Encrypter
cipherchar = 65+(Math.Remainder((letter-65)+(key-65),26))
TextWindow.WriteLine("Enciphered Char: "+Text.GetCharacter(cipherchar))

'Decrypter
value = (cipherchar-64)-(key-64)
If value < 0 Then
value = 26 + value
EndIf
decipherchar = 65 + Math.Remainder(value,26)
TextWindow.WriteLine("Deciphered Char: "+Text.GetCharacter(decipherchar))

@NaoChanON,Khumara

This is what i actually want to do but when i do the same for a long string instead of single character with nested for loop it can't work...The above code works just fine...

They say working hard is good but i say working smart is best...

Wednesday, March 7, 2012 5:44 PM
• Try this. In this case decipher is too easy.

To complicate , you'd better use For J=.... code before

For M=....  .

'Cipher
A="TodayIsThursday"
TextWindow.WriteLine("Original is ...."+A+text.GetCharacter(10))
A=text.ConvertToUpperCase(A)
L=text.GetLength(A)
KeyLetter="CBA"
L2=text.GetLength(KeyLetter)
C=""
For M=1 to L
For j= 1 To L2
Key=text.GetCharacterCode(text.GetSubText(KeyLetter,j,1))-64
P = text.GetSubText(A,M,1)
PNumber=text.GetCharacterCode(P)-65
CNumber=Math.Remainder((PNumber + key) , 26)
C=C+text.GetCharacter(CNumber+65)
EndFor
EndFor
textwindow.WriteLine("Ciphered to ... "+C+text.GetCharacter(10))

'  Decipher
A=C
L=text.GetLength(A)
Key=text.GetCharacterCode(text.GetSubText(KeyLetter,1,1))-64
D=""
For M=1 to L Step 3
P = text.GetSubText(A,M,1)
DNumber=text.GetCharacterCode(P)-65
PNumber=Math.Remainder((DNumber - key+26) , 26)
D=D+text.GetCharacter(PNumber+65)
EndFor
textwindow.WriteLine("Returned to .... "+D+text.GetCharacter(10))

Thursday, March 8, 2012 1:23 AM
• Import : JBK158 'Graphical Interface

@NaochanON

It still can't seems to work... key length is limited to 3 character if i enter more than 3 character it can't decipher back to original text...

They say working hard is good but i say working smart is best...

Thursday, March 8, 2012 5:56 AM
• Sorry my coding was not general.

Please chage Step "3"  to   L2    (L2= Text.getlength(Keyletter))

For M=1 to L Step 3 ---> For M=1 to L Step L2

But something wrong in JBK158 yet. ( I'm trying ...)

Thursday, March 8, 2012 6:26 AM
• see   JBK158-0.  No limit.

UpperCase is the point.

Thursday, March 8, 2012 6:43 AM
•  my sister wrote a C++ program for me she was in hurry for her office and didn't described anything how it works.

she slaps me twice or thrice (don't wanna remember) :P and advice me to learn  the math behind it and then try to code.

the mathematical formula for vigenere cipher shown on wikipedia can't seem to work... in my case. :(

They say working hard is good but i say working smart is best...

Thursday, March 8, 2012 8:32 AM
• Try this.  JBK158-1

Used KM=math.Remainder(M-1,L2)+1   to cipher,decipher

Rotates cipher character. "L" -->"E"-->"M"-->"O"-->"N"-->"L"-------

attackdown ----->  MYGPQWIBLB ----->ATTACKDOWN

Thursday, March 8, 2012 9:17 AM
• Try this.  JBK158-1

Used KM=math.Remainder(M-1,L2)+1   to cipher,decipher

Rotates cipher character. "L" -->"E"-->"M"-->"O"-->"N"-->"L"-------

attackdown ----->  MYGPQWIBLB ----->ATTACKDOWN

what..? you can decipher encrypted text using any key...

They say working hard is good but i say working smart is best...

Thursday, March 8, 2012 4:46 PM
• Hi Amir,

Can I ask the reason you started the thread? Do you need a tool to do the ecryption and decryption for other project, or do you want to learn what is wrong with your code so you can finish your project on your own or do you want to learn how other people write the code to solve the same problem?

I just want to make sure we are directing you to the right direction. If you're trying to use the problem to learn SmallBasic, we want to give you pointers so you can continue to figure it out. If you're trying to impress your sister by solving the problem through SmallBasic, then NaochanON is on the right track :)

Thursday, March 8, 2012 10:01 PM
• i want to learn the concept of nested for loop...i don't wanna impress anyone. :)

They say working hard is good but i say working smart is best...

Monday, March 12, 2012 5:17 AM
• ```GraphicsWindow.Width = 600
GraphicsWindow.Height = 400
GraphicsWindow.Title = "Computer Implementation of Vigenere Cipher"
GraphicsWindow.Show()
Controls.SetSize(txtMain,580,310)
GraphicsWindow.DrawText(10,330,"Cecret Key (Case Sensitive)")
Controls.SetSize(txtKey,150,30)
Controls.ButtonClicked = do_Click

Sub cleaning
texts = ""
key = ""
i = ""
j = ""
textscode = ""
charfromcode = ""
crypt = ""
charscrypt = ""
decrypt = ""
enctexts = ""
keycode = ""
EndSub

Sub do_Click
If Controls.LastClickedButton = "Button1" Then
eNcrypt()
ElseIf Controls.LastClickedButton = "Button2" Then
deCrypt()
EndIf
EndSub

Sub eNcrypt
texts = Text.ConvertToUpperCase(Controls.GetTextBoxText(txtMain))
key =  Text.ConvertToUpperCase(Controls.GetTextBoxText(txtKey))
For i =  1 To Text.GetLength(texts)
For j = 1 To Text.GetLength(key)
textscode = Text.GetCharacterCode(Text.GetSubText(texts,i,1))
keycode = Text.GetCharacterCode(Text.GetSubText(key,j,1))
If j > Text.GetLength(key) Then
j = 1
EndIf
EndFor
charscrypt = 65+Math.Remainder((textscode-65)+(keycode-65),26)
charfromcode = Text.GetCharacter(charscrypt)
crypt = crypt + charfromcode
EndFor
Controls.SetTextBoxText(txtMain,crypt)
cleaning()
EndSub

Sub deCrypt
enctexts = Text.ConvertToUpperCase(Controls.GetTextBoxText(txtMain))
key =  Text.ConvertToUpperCase(Controls.GetTextBoxText(txtKey))
For i =  1 To Text.GetLength(enctexts)
For j = 1 To Text.GetLength(key)
textscode = Text.GetCharacterCode(Text.GetSubText(enctexts,i,1))
keycode = Text.GetCharacterCode(Text.GetSubText(key,j,1))
If j > Text.GetLength(key) Then
j = 1
EndIf
EndFor
charscrypt = 65+Math.Remainder((textscode-65)-(keycode-65),26)
If charscrypt < 65 Then
charscrypt = charscrypt + 26
EndIf
charfromcode = Text.GetCharacter(charscrypt)
decrypt = decrypt + charfromcode
EndFor
Controls.SetTextBoxText(txtMain,decrypt)
cleaning()
EndSub```

Today i visit this forum after a long time and decided to complete this Vigenere Cipher codes

i need some more help to skip chars other than A-Z and a-z from the encryption and decryption operation...

:)

They say working hard is good but i say working smart is best...

Monday, September 3, 2012 8:57 AM
• There are some questions about the logic first:

```      For j = 1 To Text.GetLength(key)
textscode = Text.GetCharacterCode(Text.GetSubText(texts,i,1))
keycode = Text.GetCharacterCode(Text.GetSubText(key,j,1))
If j > Text.GetLength(key) Then
j = 1
EndIf
EndFor
```

Consider the code above:

textscode only depends on i, so why is it in the loop for j?

j can never be > Text.GetLength(key) since it is in a loop over this upper limit, so the test can never be hit.  Then, in fact keycode is always set to the last character and so the j loop is unnecessary and only the last character of the key is being used.

These don't answer the question, but lots of redundant complicating code make it harder.

Note the following cipher can be deciphered using your decipher:

```Sub eNcrypt
texts = Text.ConvertToUpperCase(Controls.GetTextBoxText(txtMain))
key =  Text.ConvertToUpperCase(Controls.GetTextBoxText(txtKey))
keycode = Text.GetCharacterCode(Text.GetSubText(key,Text.GetLength(key),1))
For i =  1 To Text.GetLength(texts)
textscode = Text.GetCharacterCode(Text.GetSubText(texts,i,1))
charscrypt = 65+Math.Remainder((textscode-65)+(keycode-65),26)
charfromcode = Text.GetCharacter(charscrypt)
crypt = crypt + charfromcode
EndFor
Controls.SetTextBoxText(txtMain,crypt)
cleaning()
EndSub
```

So to check for non ascii codes, the following ignores them, but the final solution is the work on the cipher and key methods more.

```Sub eNcrypt
texts = Text.ConvertToUpperCase(Controls.GetTextBoxText(txtMain))
key =  Text.ConvertToUpperCase(Controls.GetTextBoxText(txtKey))
keycode = Text.GetCharacterCode(Text.GetSubText(key,Text.GetLength(key),1))
For i =  1 To Text.GetLength(texts)
textscode = Text.GetCharacterCode(Text.GetSubText(texts,i,1))
If (textscode < 65 Or textscode > 90) Then
crypt = crypt + Text.GetCharacter(textscode)
Else
charscrypt = 65+Math.Remainder((textscode-65)+(keycode-65),26)
charfromcode = Text.GetCharacter(charscrypt)
crypt = crypt + charfromcode
EndIf
EndFor
Controls.SetTextBoxText(txtMain,crypt)
cleaning()
EndSub

Sub deCrypt
enctexts = Text.ConvertToUpperCase(Controls.GetTextBoxText(txtMain))
key =  Text.ConvertToUpperCase(Controls.GetTextBoxText(txtKey))
keycode = Text.GetCharacterCode(Text.GetSubText(key,Text.GetLength(key),1))
For i =  1 To Text.GetLength(enctexts)
textscode = Text.GetCharacterCode(Text.GetSubText(enctexts,i,1))
If (textscode < 65 Or textscode > 90) Then
decrypt = decrypt + Text.GetCharacter(textscode)
Else
charscrypt = 65+Math.Remainder((textscode-65)-(keycode-65),26)
If charscrypt < 65 Then
charscrypt = charscrypt + 26
EndIf
charfromcode = Text.GetCharacter(charscrypt)
decrypt = decrypt + charfromcode
EndIf
EndFor
Controls.SetTextBoxText(txtMain,decrypt)
cleaning()
EndSub```

Monday, September 3, 2012 9:58 PM
• text = "text"

key = "key"

the logic is that get the first letter from text and then encrypt it by letters from key

t encrypt by k >> d

then d encrypt by e >> x

and then x encrypt by y >> r

now second letter from text and so on.

They say working hard is good but i say working smart is best...

Tuesday, September 4, 2012 5:48 AM
• ```string = "CRYPTOISSHORTFORCRYPTOGRAPHY"
key = "ABCDABCDABCDABCDABCDABCDABCD"
'get the string char code
For i= 1 To Text.GetLength(string)
chars[i] = Text.GetSubText(string,i,1)
charscode[i] = Text.GetCharacterCode(chars[i]) - 65
EndFor
'get the key char code
For i = 1 To Text.GetLength(key)
keychars[i] = Text.GetSubText(key,i,1)
keycharscode[i] = Text.GetCharacterCode(keychars[i]) - 65
EndFor
'add the string char in key chars then mod by 26 to get encrypted char
For i = 1 To Text.GetLength(string)
combinedchars[i] = keycharscode[i] + charscode[i]
encryptedcharcode[i] = Math.Remainder(combinedchars[i],26)
encryptedchar[i] = Text.GetCharacter(encryptedcharcode[i] + 65)
TextWindow.Write(encryptedchar[i])
EndFor
'CSASTPKVSIQUTGQUCSASTPIUAQJB```

i need help to repeat the key automatically just show me the method not the working code...

e.g : text = "text" ,key = "key"

KEYK ???

i need to repeat the key itself until key length and text length both become equal.

They say working hard is good but i say working smart is best...

Tuesday, September 4, 2012 7:00 AM
• ```i = 1
'repeat the key
While Text.GetLength(finalkey)  < Text.GetLength(string)
If i > Text.GetLength(key) Then
i = 1
EndIf
keyl = Text.GetSubText(key,i,1)
finalkey = finalkey + keyl
i = i + 1
EndWhile```

i think i have solved the problem...

is this method right??? any suggestion?

They say working hard is good but i say working smart is best...

Tuesday, September 4, 2012 7:45 AM
• finally here is the final codes.

```'Vigenere Cipher By Amir CPS
'Version 1.0
'Date 3-09-2012

GraphicsWindow.Width = 600
GraphicsWindow.Height = 400
GraphicsWindow.Title = "Computer Implementation of Vigenere Cipher"
GraphicsWindow.Show()
Controls.SetSize(txtMain,580,310)
GraphicsWindow.DrawText(10,330,"Secret Key (shoud be less than the message)")
Controls.SetSize(txtKey,150,30)
Controls.ButtonClicked = do_Click

Sub do_Click
If Controls.LastClickedButton = "Button1" Then
If Controls.GetTextBoxText(txtKey) = "" Then
GraphicsWindow.ShowMessage("Enter the key first","Error")
Else
eNcrypt()
EndIf
ElseIf Controls.LastClickedButton = "Button2" Then
If Controls.GetTextBoxText(txtKey) = "" Then
GraphicsWindow.ShowMessage("Enter the key first","Error")
Else
deCrypt()
EndIf
EndIf
EndSub

Sub eNcrypt
repeatKey()
getStringCharCode()
getKeyCharCode()
For i = 1 To Text.GetLength(string)
If charscode[i] < 0 Or charscode[i] > 26 Then
encryptestring = Text.Append(encryptestring,Text.GetCharacter(charscode[i] + 65))
Else
encryptedcharcode[i] = 65 + Math.Remainder(keycharscode[i] + charscode[i],26)
encryptedchar[i] = Text.GetCharacter(encryptedcharcode[i])
encryptestring = Text.Append(encryptestring,encryptedchar[i])
EndIf
EndFor
Controls.SetTextBoxText(txtMain,"")
Controls.SetTextBoxText(txtMain,encryptestring)
encryptestring = ""
key = ""
Controls.SetTextBoxText(txtKey,"")
finalkey = ""
EndSub

Sub deCrypt
repeatKey()
getStringCharCode()
getKeyCharCode()
For i = 1 To Text.GetLength(string)
If charscode[i] < 0 Or charscode[i] > 26 Then
decryptestring = Text.Append(decryptestring,Text.GetCharacter(charscode[i] + 65))
Else
encryptedcharcode[i] = Math.Remainder(charscode[i] - keycharscode[i],26) + 65
If encryptedcharcode[i] < 65 Then
encryptedcharcode[i] = encryptedcharcode[i] + 26
EndIf
encryptedchar[i] = Text.GetCharacter(encryptedcharcode[i])
decryptestring = Text.Append(decryptestring,encryptedchar[i])
EndIf
EndFor
Controls.SetTextBoxText(txtMain,"")
Controls.SetTextBoxText(txtMain,decryptestring)
decryptestring = ""
key = ""
Controls.SetTextBoxText(txtKey,"")
finalkey = ""
EndSub

Sub repeatKey
string = Text.ConvertToUpperCase(Controls.GetTextBoxText(txtMain))
key = Text.ConvertToUpperCase(Controls.GetTextBoxText(txtKey))
i = 1
While Text.GetLength(finalkey)  < Text.GetLength(string)
If i > Text.GetLength(key) Then
i = 1
EndIf
keyl = Text.GetSubText(key,i,1)
finalkey = finalkey + keyl
i = i + 1
EndWhile
EndSub

Sub getStringCharCode
For i= 1 To Text.GetLength(string)
chars[i] = Text.GetSubText(string,i,1)
charscode[i] = Text.GetCharacterCode(chars[i]) - 65
EndFor
EndSub

Sub getKeyCharCode
For i = 1 To Text.GetLength(finalkey)
keychars[i] = Text.GetSubText(finalkey,i,1)
keycharscode[i] = Text.GetCharacterCode(keychars[i]) - 65
EndFor
EndSub```

They say working hard is good but i say working smart is best...

Tuesday, September 4, 2012 4:33 PM