3 Replies Latest reply: Feb 7, 2013 6:51 PM by Espeon
Espeon Level 2 Level 2 (155 points)

I'm working on making a kind of Vigenere cipher, and it was working smoothly until I decided to add in support for lowercase. Then I got issues.

Here's my code so far:

 

set res1 to display dialog "Set key for this runtime..." buttons {"From Clipboard", "Generate Random", "Quit"}

if button returned of res1 = "From Clipboard" then set key_ to the clipboard

if button returned of res1 = "Generate Random" then

          set list_ to {}

          set key_ to "*"

          repeat 55 times

                    set q to 0

                    repeat until q is 1

                              set rand to (random number from 1 to 55)

                              if rand is not in list_ then set q to 1

                    end repeat

                    set end of list_ to rand

                    set temp to character (rand) of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,"

                    set key_ to key_ & temp

          end repeat

          set key_ to text 2 thru 55 of key_

          set res1_1 to the display dialog "Key: \"" & key_ & "\"" buttons {"Copy Key to clipboard"}

  set the clipboard to key_

end if

if button returned of res1 = "Quit" then quit

 

 

repeat

 

          set res2 to display dialog "Encrypt or Decrpyt?" buttons {"Encrypt", "Decrypt", "Change key/Quit"}

 

          if button returned of res2 = "Encrypt" then

 

                    set res3 to (display dialog "Message?" default answer "" buttons {"Encrypt from Clipboard", "Encrypt message", "Quit"} default button 2)

                    if button returned of res3 = "Quit" then quit

                    if button returned of res3 = "Encrypt from Clipboard" then

                              set message to the clipboard

                              exit repeat

                    end if

                    if button returned of res3 = "Encrypt message" then set message to text returned of res3

 

                    set q to 0

                    set enc to "*"

                    repeat (length of message) times

                              set q to q + 1

                              try

                                        set temp to offset of (character q of message) in key_

                              on error

                                        set temp to 0

                              end try

                              set code_t to character (temp + 1) of "?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,"

                              set enc to enc & code_t

                    end repeat

 

                    set enc to text 2 thru (length of enc) of enc

                    repeat

                              set res3 to the display dialog "Encrypted message: \"" & enc & "\"" buttons {"Copy Message to Clipboard", "Return to Menu"}

                              if button returned of res3 = "Copy Message to clipboard" then set the clipboard to enc

                              if button returned of res3 = "Return to Menu" then exit repeat

                    end repeat

          end if

 

          if button returned of res2 = "Decrypt" then

                    set res3 to (display dialog "Message?" default answer "" buttons {"Decrypt from Clipboard", "Decrypt message", "Quit"} default button 2)

                    if button returned of res3 = "Quit" then quit

                    if button returned of res3 = "Decrypt from Clipboard" then set message to the clipboard

                    if button returned of res3 = "Decrypt message" then set message to text returned of res3

                    set q to 0

                    set dec to "*"

                    repeat (length of message) times

                              set q to q + 1

                              set dec_t to offset of (character q of message) in "?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,"

                              set dec_t2 to character (dec_t - 1) in key_

                              set dec to dec & dec_t2

 

 

 

                    end repeat

                    set dec to text 2 thru (length of dec) of dec

                    repeat

                              set res3 to display dialog "Decrypted message: \"" & dec & "\"" buttons {"Copy Message to Clipboard", "Return to menu"}

                              if button returned of res3 = "Copy Message to clipboard" then

  set the clipboard to dec

                                        exit repeat

                              end if

                              if button returned of res3 = "Return to menu" then exit repeat

 

                    end repeat

 

 

 

 

 

 

          end if

 

          if button returned of res2 = "Change key/Quit" then

                    set res3 to display dialog "Options…" buttons {"Copy Current key to Clipboard", "Set new key", "Quit"}

                    if button returned of res3 = "Quit" then quit

                    if button returned of res3 = "Copy Current key to Clipboard" then

  set the clipboard to key_

  display dialog key_

                    end if

                    if button returned of res3 = "Set new key" then

 

                              set res1 to display dialog "Set new key" buttons {"From Clipboard", "Generate Random"}

                              if button returned of res1 = "From Clipboard" then set key_ to the clipboard

                              if button returned of res1 = "Generate Random" then

                                        set list_ to {}

                                        set key_ to "*"

                                        repeat 55 times

                                                  set q to 0

                                                  repeat until q is 1

                                                            set rand to (random number from 1 to 55)

                                                            if rand is not in list_ then set q to 1

                                                  end repeat

                                                  set end of list_ to rand

                                                  set temp to character (rand) of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,"

                                                  set key_ to key_ & temp

 

 

                                        end repeat

                                        set key_ to text 2 thru 55 of key_

                                        set res1_1 to the display dialog "Key: \"" & key_ & "\"" buttons {"Copy new key to clipboard"}

  set the clipboard to key_

                              end if

                    end if

 

          end if

end repeat

However, this is where the problem is:

set q to 0

  set enc to "*"

  repeat (length of message) times

  set q to q + 1

  try

  set temp to offset of (character q of message) in key_

  on error

  set temp to 0

  end try

  set code_t to character (temp + 1) of "?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,"

  set enc to enc & code_t

  end repeat

 

I've found out that if I do the follwing, I get 1:

 

set str to "aaaAAA"

offset of "A" in str

---> 1

So, I'm trying to ensure that the offset command is case-sensitive, which I haven't been able to as of yet.


MacBook Air, Mac OS X (10.7.5)
  • Niel Level 10 Level 10 (268,320 points)

    Here:

     

    considering case

    offset of "A" in str

    end considering

     

    (76295)

  • Espeon Level 2 Level 2 (155 points)

    Ah, thanks. I'd assumed it would be something like 'Offset of "A" in str with case sensitive. Then again, I guess that case sensitive applies to more than just the offset command, so it makes sense, in retrospect.

  • Espeon Level 2 Level 2 (155 points)

    Fixed and updated code:

    (The code in my previous posts wasn't actually Vigenére but a simple replacement cipher; that was just the prep work.)

    set res3 to 0

    set res2 to 0

     

    considering case

              repeat

                        set res1 to display dialog "Set key seed for this runtime..." buttons {"Set seed", "More...", "Quit"} default button 1 default answer "" with title "Vigenére 2.0 by Espeon"

                        if button returned of res1 = "Quit" then

      quit

      quit

                        end if

                        if button returned of res1 = "More..." then

                                  repeat

                                            set res2 to button returned of (display dialog "Set key from clipboard or custom?" buttons {"Clipboard", "Custom", "Back"})

                                            if res2 = "Custom" then

                                                      set key_l to "q"

                                                      set q to 0

                                                      repeat

                                                                set key_l to text returned of (display dialog "Key length? " default answer "")

                                                                try

                                                                          set key_l to key_l as number

                                                                end try

                                                                if ((class of key_l) is number) or ((class of key_l) is integer) then exit repeat

                                                      end repeat

     

                                                      set key_s to (display dialog "Set key seed for this runtime..." buttons {"Set seed", "Quit"} default button 1 default answer "")

                                                      if button returned of key_s = "Quit" then

                                                                quit

                                                                quit

                                                      end if

                                                      set key_s to text returned of key_s

                                                      set temp to 0

                                                      set q to 0

                                                      repeat (length of key_s) times

                                                                set q to q + 1

                                                                set temp to temp + 10 ^ q * (offset of (character q of key_s) in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,0123456789!'()[]{};:<>=+-_@#$%^&*`~")

                                                      end repeat

     

      random number with seed temp

                                                      set key_ to "*"

                                                      repeat key_l times

                                                                set rand to (random number from 1 to 90)

                                                                set temp to character (rand) of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,0123456789!'()[]{};:<>=+-_@#$%^&*`|~/?"

                                                                set key_ to key_ & temp

                                                      end repeat

                                                      set key_ to text 2 thru -1 of key_

                                                      exit repeat

                                            end if

     

                                            if res2 = "Clipboard" then

                                                      set res3 to button returned of (display dialog "Get seed or key from clipboard?" buttons {"Key", "Seed", "Back"})

                                                      if res3 = "Key" then

                                                                set key_ to the clipboard

                                                                exit repeat

                                                      end if

                                                      if res3 = "Seed" then

                                                                set key_s to the clipboard

                                                                exit repeat

                                                      end if

                                            end if

                                            if res2 = "Back" then

                                                      exit repeat

                                            end if

                                  end repeat

                        end if

                        if res3 = "Key" then exit repeat

                        if res3 = "Seed" then exit repeat

                        if res2 = "Custom" then exit repeat

                        if button returned of res1 = "Set seed" then exit repeat

              end repeat

              if (button returned of res1 = "Set seed") or (res3 = "Seed") then

                        set key_s to text returned of res1

                        set temp to 0

                        set q to 0

     

                        repeat (length of key_s) times

                                  set q to q + 1

                                  set temp to temp + 10 ^ q * (offset of (character q of key_s) in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,0123456789!'()[]{};:<>=+-_@#$%^&*`~")

                        end repeat

     

      random number with seed temp

                        set list_ to {}

                        set key_ to "*"

                        repeat 90 times

                                  set q to 0

                                  repeat until q is 1

                                            set rand to (random number from 1 to 90)

                                            if rand is not in list_ then set q to 1

                                  end repeat

                                  set end of list_ to rand

                                  set temp to character (rand) of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,0123456789!'()[]{};:<>=+-_@#$%^&*`~"

                                  set key_ to key_ & temp

                        end repeat

                        set key_ to text 2 thru 90 of key_

     

              end if

     

              repeat

     

                        set res2 to (display dialog "Encrypt or Decrpyt?" buttons {"Encrypt", "Decrypt", "Change key/Quit"})

     

                        if button returned of res2 = "Encrypt" then

                                  set res3 to ""

                                  repeat until res3 is not ""

                                            set res3 to (display dialog "Message?" default answer "" buttons {"Encrypt message", "Encrypt from clipboard", "Quit"} default button 1)

                                            if button returned of res3 = "Quit" then quit

                                            if button returned of res3 = "Encrypt from clipboard" then set str to the clipboard

                                            if button returned of res3 = "Encrypt message" then set str to text returned of res3

                                  end repeat

                                  set q to 0

                                  set k to 0

                                  set enc to "*"

                                  repeat (length of str) times

                                            set q to q + 1

                                            set k to k + 1

                                            if k > (length of key_) then set k to 1

                                            set temp to offset of (character k of key_) in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,0123456789!'()[]{};:<>=+-_@#$%^&*`~"

                                            set temp2 to character ((offset of (character q of str) in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,0123456789!'()[]{};:<>=+-_@#$%^&*`~") + temp) of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,0123456789!'()[]{};:<>=+-_@#$%^&*`~ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop qrstuvwxyz .,0123456789!'()[]{};:<>=+-_@#$%^&*`~"

                                            set enc to enc & temp2

                                  end repeat

                                  set enc to text 2 thru -1 of enc

     

     

                                  repeat

                                            set res3 to the display dialog "Encrypted message: \"" & enc & "\"" buttons {"Copy message to clipboard", "Return to menu"}

                                            if button returned of res3 = "Copy message to clipboard" then

      set the clipboard to enc

                                                      exit repeat

                                            end if

                                            if button returned of res3 = "Return to menu" then exit repeat

                                  end repeat

                        end if

     

                        if button returned of res2 = "Decrypt" then

                                  set res3 to ""

                                  repeat until res3 is not ""

                                            set res3 to (display dialog "Message?" default answer "" buttons {"Decrypt message", "Decrypt from clipboard", "Quit"} default button 1)

                                            if button returned of res3 = "Quit" then quit

                                            if button returned of res3 = "Decrypt from clipboard" then set str to the clipboard

                                            if button returned of res3 = "Decrypt message" then set str to text returned of res3

                                  end repeat

                                  set q to 0

                                  set k to 0

                                  set dec to "*"

                                  repeat (length of str) times

                                            set q to q + 1

                                            set k to k + 1

                                            if k > (length of key_) then set k to 1

                                            set temp to 182 - (offset of (character k of key_) in "~`*&^%$#@_-+=><:;}{][)('!9876543210,. zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA~`*&^%$#@_-+=><:;}{][)('!98 76543210,. zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA")

                                            set temp2 to character ((offset of (character q of str) in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,0123456789!'()[]{};:<>=+-_@#$%^&*`~") - temp) of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,0123456789!'()[]{};:<>=+-_@#$%^&*`~ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop qrstuvwxyz .,0123456789!'()[]{};:<>=+-_@#$%^&*`~"

                                            set dec to dec & temp2

                                  end repeat

     

                                  set dec to text 2 thru -1 of dec

     

                                  repeat

                                            set res3 to display dialog "Decrypted message: \"" & dec & "\"" buttons {"Copy message to clipboard", "Return to menu"}

                                            if button returned of res3 = "Copy message to clipboard" then

      set the clipboard to dec

                                                      exit repeat

                                            end if

                                            if button returned of res3 = "Return to menu" then exit repeat

     

                                  end repeat

     

                        end if

     

                        if button returned of res2 = "Change key/Quit" then

                                  set res3 to display dialog "Options…" buttons {"Copy current key seed to clipboard", "Set new key", "Quit"} with title "Vigenere 2.0 by Chris Guillott"

                                  if button returned of res3 = "Quit" then

                                            quit

                                            quit

                                  end if

                                  if button returned of res3 = "Copy current key seed to clipboard" then

      set the clipboard to key_s

      display dialog key_s buttons {"Copied!"}

                                  end if

                                  if button returned of res3 = "Set new key" then

     

                                            repeat

                                                      set res1 to display dialog "Set new key seed..." buttons {"Set seed", "More...", "Quit"} default button 1 default answer ""

                                                      if button returned of res1 = "Quit" then

                                                                quit

                                                                quit

                                                      end if

                                                      if button returned of res1 = "More..." then

                                                                repeat

                                                                          set res2 to button returned of (display dialog "Set key from clipboard or custom?" buttons {"Clipboard", "Custom", "Back"})

                                                                          if res2 = "Custom" then

                                                                                    set key_l to "q"

                                                                                    set q to 0

                                                                                    repeat

                                                                                              set key_l to text returned of (display dialog "Key length? " default answer "")

                                                                                              try

                                                                                                        set key_l to key_l as number

                                                                                              end try

                                                                                              if ((class of key_l) is number) or ((class of key_l) is integer) then exit repeat

                                                                                    end repeat

     

                                                                                    set key_s to (display dialog "Set key seed for this runtime..." buttons {"Set seed", "Quit"} default button 1 default answer "")

                                                                                    if button returned of key_s = "Quit" then quit

                                                                                    set key_s to text returned of key_s

                                                                                    set temp to 0

                                                                                    set q to 0

                                                                                    repeat (length of key_s) times

                                                                                              set q to q + 1

                                                                                              set temp to temp + 10 ^ q * (offset of (character q of key_s) in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,0123456789!'()[]{};:<>=+-_@#$%^&*`~")

                                                                                    end repeat

     

      random number with seed temp

                                                                                    set key_ to "*"

                                                                                    repeat key_l times

                                                                                              set rand to (random number from 1 to 90)

                                                                                              set temp to character (rand) of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,0123456789!'()[]{};:<>=+-_@#$%^&*`|~/?"

                                                                                              set key_ to key_ & temp

                                                                                    end repeat

                                                                                    set key_ to text 2 thru -1 of key_

                                                                                    exit repeat

                                                                          end if

     

                                                                          if res2 = "Clipboard" then

                                                                                    set res3 to button returned of (display dialog "Get seed or key from clipboard?" buttons {"Key", "Seed", "Back"})

                                                                                    if res3 = "Key" then

                                                                                              set key_ to the clipboard

                                                                                              exit repeat

                                                                                    end if

                                                                                    if res3 = "Seed" then

                                                                                              set key_s to the clipboard

                                                                                              exit repeat

                                                                                    end if

                                                                          end if

                                                                          if res2 = "Back" then

                                                                                    exit repeat

                                                                          end if

                                                                end repeat

                                                      end if

                                                      if res3 = "Key" then exit repeat

                                                      if res3 = "Seed" then exit repeat

                                                      if res2 = "Custom" then exit repeat

                                                      if button returned of res1 = "Set seed" then exit repeat

                                            end repeat

                                            if (button returned of res1 = "Set seed") or (res3 = "Seed") then

                                                      set key_s to text returned of res1

                                                      set temp to 0

                                                      set q to 0

     

                                                      repeat (length of key_s) times

                                                                set q to q + 1

                                                                set temp to temp + 10 ^ q * (offset of (character q of key_s) in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,0123456789!'()[]{};:<>=+-_@#$%^&*`~")

                                                      end repeat

     

      random number with seed temp

                                                      set list_ to {}

                                                      set key_ to "*"

                                                      repeat 90 times

                                                                set q to 0

                                                                repeat until q is 1

                                                                          set rand to (random number from 1 to 90)

                                                                          if rand is not in list_ then set q to 1

                                                                end repeat

                                                                set end of list_ to rand

                                                                set temp to character (rand) of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .,0123456789!'()[]{};:<>=+-_@#$%^&*`~"

                                                                set key_ to key_ & temp

                                                      end repeat

                                                      set key_ to text 2 thru 90 of key_

     

                                            end if

                                  end if

     

                        end if

              end repeat

     

    end considering

     

     

    I added in a few different keygen options.