Case-sensitive 'offset'?
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 tokey_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 todecexit 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 tokey_
display dialogkey_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 tokey_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)