locked
Problem when encrypting a user input! RRS feed

  • Question

  • Hey everyone!
    At first, my english isn't well so please don't criticize me because of that...

    I'm new to this forum but i'm coding in Small Basic for some time now.
    I really enjoy this programming language because it is so easy to understand and you can learn it really fast!

    My problem now is that i wanted to code a text encrypter but it gives me some little mistake.
    At first, here's my code:

    TextWindow.Title = "Text Encrypter"
    TextWindow.ForegroundColor = "White"
    TextWindow.WriteLine("========Text Encrypter========")
    TextWindow.WriteLine("Please give in your text:")
    TextWindow.Write("-->")
    tocrypt = TextWindow.Read()
    TextWindow.WriteLine("")
    TextWindow.WriteLine("")
    CryptText()
    TextWindow.WriteLine("Encrypted text:")
    TextWindow.Write("-->")
    TextWindow.WriteLine(crypted)
    TextWindow.WriteLine("")
    TextWindow.WriteLine("")
    TextWindow.Write("See you!")
    TextWindow.PauseWithoutMessage()
    Program.End()
    
    Sub CryptText
      crypted = ""
      'fill array with each character in the given text
      For x = 0 To Text.GetLength(tocrypt)
        Array.SetValue("cryp1",x,Text.GetSubText(tocrypt,x,1))
      EndFor
      'crypt each character in the given text with +3 (Caesar)
      For y = 0 To Array.GetItemCount("cryp1")-1
        If (Array.GetValue("cryp1",y) = " ") Then
          Array.SetValue("cryp2",y," ")
        Else
          Array.SetValue("cryp2",y,Text.GetCharacterCode(Array.GetValue("cryp1",y))+3)
        EndIf
      EndFor
      'fill third array with crypted characters
      For z = 0 To Array.GetItemCount("cryp2")-1
        Array.SetValue("cryp3",z,Text.GetCharacter(Array.GetValue("cryp2",z)))
      EndFor
      'fill string "crypted" with all crypted characters from "cryp3"
      For p = 0 To Array.GetItemCount("cryp3")-1
        crypted = crypted + Array.GetValue("cryp3",p)
      EndFor
    EndSub


    When you run this program you are asked to give in some text and then the program encrypts the given text with the Caesar method, but it always gives me a "heart symbol" as the first character and after that the encrypted text! Besides, my program doesn't convert "x,y" and "z" into normal characters but into other symbols.

    My question is: How can i make my program not show the "heart symbol" at first but just the encrypted text?

    I hope anyone can help me with this =)
    See you!
    Monday, May 25, 2009 6:35 PM

Answers

  • Your problem arises because arrays in Small Basic are 1-based not 0-based.
    I have modified the code for you below.

    TextWindow.Title = "Text Encrypter"
    TextWindow.ForegroundColor = "White"
    TextWindow.WriteLine("========Text Encrypter========")
    TextWindow.WriteLine("Please give in your text:")
    TextWindow.Write("-->")
    tocrypt = TextWindow.Read()
    TextWindow.WriteLine("")
    TextWindow.WriteLine("")
    CryptText()
    TextWindow.WriteLine("Encrypted text:")
    TextWindow.Write("-->")
    TextWindow.WriteLine(crypted)
    TextWindow.WriteLine("")
    TextWindow.WriteLine("")
    TextWindow.Write("See you!")
    TextWindow.PauseWithoutMessage()
    Program.End()

    Sub CryptText
      crypted = ""
      'fill array with each character in the given text
      For x = 1 To Text.GetLength(tocrypt)
        Array.SetValue("cryp1",x,Text.GetSubText(tocrypt,x,1))
      EndFor
      'crypt each character in the given text with +3 (Caesar)
      For y = 1 To Array.GetItemCount("cryp1")
        If (Array.GetValue("cryp1",y) = " ") Then
          Array.SetValue("cryp2",y," ")
        Else
          Array.SetValue("cryp2",y,Text.GetCharacterCode(Array.GetValue("cryp1",y))+3)
        EndIf
      EndFor
      'fill third array with crypted characters
      For z = 1 To Array.GetItemCount("cryp2")
        Array.SetValue("cryp3",z,Text.GetCharacter(Array.GetValue("cryp2",z)))
      EndFor
      'fill string "crypted" with all crypted characters from "cryp3"
      For p = 1 To Array.GetItemCount("cryp3")
        crypted = crypted + Array.GetValue("cryp3",p)
      EndFor
    EndSub

    • Marked as answer by CrazyCoding Monday, May 25, 2009 8:07 PM
    Monday, May 25, 2009 7:57 PM

All replies

  • Your problem arises because arrays in Small Basic are 1-based not 0-based.
    I have modified the code for you below.

    TextWindow.Title = "Text Encrypter"
    TextWindow.ForegroundColor = "White"
    TextWindow.WriteLine("========Text Encrypter========")
    TextWindow.WriteLine("Please give in your text:")
    TextWindow.Write("-->")
    tocrypt = TextWindow.Read()
    TextWindow.WriteLine("")
    TextWindow.WriteLine("")
    CryptText()
    TextWindow.WriteLine("Encrypted text:")
    TextWindow.Write("-->")
    TextWindow.WriteLine(crypted)
    TextWindow.WriteLine("")
    TextWindow.WriteLine("")
    TextWindow.Write("See you!")
    TextWindow.PauseWithoutMessage()
    Program.End()

    Sub CryptText
      crypted = ""
      'fill array with each character in the given text
      For x = 1 To Text.GetLength(tocrypt)
        Array.SetValue("cryp1",x,Text.GetSubText(tocrypt,x,1))
      EndFor
      'crypt each character in the given text with +3 (Caesar)
      For y = 1 To Array.GetItemCount("cryp1")
        If (Array.GetValue("cryp1",y) = " ") Then
          Array.SetValue("cryp2",y," ")
        Else
          Array.SetValue("cryp2",y,Text.GetCharacterCode(Array.GetValue("cryp1",y))+3)
        EndIf
      EndFor
      'fill third array with crypted characters
      For z = 1 To Array.GetItemCount("cryp2")
        Array.SetValue("cryp3",z,Text.GetCharacter(Array.GetValue("cryp2",z)))
      EndFor
      'fill string "crypted" with all crypted characters from "cryp3"
      For p = 1 To Array.GetItemCount("cryp3")
        crypted = crypted + Array.GetValue("cryp3",p)
      EndFor
    EndSub

    • Marked as answer by CrazyCoding Monday, May 25, 2009 8:07 PM
    Monday, May 25, 2009 7:57 PM
  • Yes man, i really thank you Stendec!
    That's the solution i was looking for =)
    Monday, May 25, 2009 8:07 PM
  • Actually, what I said about 1-based arrays was nonsense! Small Basic arrays are not 0 or 1 based. (See the thread "Does it an array type in Small Basic?"). I haven't yet worked out why my changes made a difference, but if I do I will get back to you.
    Tuesday, May 26, 2009 5:01 PM
  • OK,
        I've realised the problem was not with the arrays. The
    Text.GetSubText(tocrypt,x,1)) needs x+1 instead of x when starting your array indices from zero as Text.GetSubText() starts counting from one.
    I have published the new code as TND083.

    "Besides, my program doesn't convert "x,y" and "z" into normal characters but into other symbols."

    In order to convert these properly you need to wrap around back to a, b, c. I have incorporated this modification into the published code. Note, this code will still fail for upper case letters X, Y, Z. I leave that for you to consider.
    Tuesday, May 26, 2009 5:54 PM
  • Oh, it's good that u tell me that.
    But it works for me very well and your wrapping-snippet is very useful for me!

    For my program i made four modifications:
    -Now the program asks whether the user wants to run the program again.
    -I made it work for upper case letters as well (but the code is a bit confusing :-) )
    -The program now asks for a key from the user and doesn't provide the Caesar method anymore.
    -I also made a new array containing all characters the user doesn't want to be encrypted (user can modify the array in the code).

    My only problem for now is that when the user gives the program a long sentence and runs the program again the arrays are still filled with the old and long sentence! So when the user gives the program a shorter sentence the textwindow shows the other characters from the old sentence, too.

    Do you maybe have a solution for this problem?
    The full code is avaible under: CWV720.

    (I forgot to say that this program is a school work i had to do so i had to comment as much as i could...)

    See you!
    Wednesday, May 27, 2009 9:32 PM
  • Instead of using GetItemCount and RemoveValue, try this

    For me = 1 To Text.GetLength(tocrypt)
      Array.SetValue("cryp1", me, " ")
      Array.SetValue("cryp2", me, " ")
      Array.SetValue("cryp3", me, " ")
    EndFor

    When using loops, the value of GetItemCount changes every time you use RemoveValue.
    Here's an example of how it affects your loop

    http://smallbasic.com/program/?SLV157-2
    Wednesday, May 27, 2009 10:18 PM
  • Instead of using GetItemCount and RemoveValue, try this

    For me = 1 To Text.GetLength(tocrypt)
      Array.SetValue("cryp1", me, " ")
      Array.SetValue("cryp2", me, " ")
      Array.SetValue("cryp3", me, " ")
    EndFor

    When using loops, the value of GetItemCount changes every time you use RemoveValue.
    Here's an example of how it affects your loop

    http://smallbasic.com/program/?SLV157-2
    Yeah thank you, that helped me a lot!
    Now just some little problem again (i know i have too much problems):
    When i want to encrypt, for instance, the symbol "H" (upper case) with "T" (upper case,too) i get "<" ("smaller than"-smybol).

    Right now i don't have any solution for this...
    Friday, May 29, 2009 1:33 PM
  • Your algorithm adds character values and then subtracts 96.

    'H' = 72
    'T'= 84
    'H' + 'T' - 96 = 72 + 84 - 96 = 60 = '<'

    Just run the small piece of code below to see the characters. There are several blanks, but I believe 32 is space if I remember correctly.

    For character = 0 To 127
      TextWindow.WriteLine(character + " " + Text.GetCharacter(character))
    EndFor
    ' A = 65 Z = 90
    'a = 97 z = 122

    You check for chcode3 > 122 and for chcode3 > 90 and chcode3 < 97, but you don't check if chcode3 < 65.
    Friday, May 29, 2009 4:31 PM
  • Man Stendec u are the best =)

    Sorry for being long time not here to answer to your post but i had some free time . . .
    Now my project is finished and it can encrypt now "H" + "T" to "B" and many other combinations of Texts and Keys!

    If somebody finds a mistake plase write it in this thread here =)

    Now here u can get my program: KDN612.

    I really thank you for helping me out with this and getting it finished!!!
    Friday, June 5, 2009 9:31 PM