String Encryption Problems

Hello all!

I am writing a script that “encrypts” a string so that a person without the script can’t read it. Here is the script, where t equals the string “Hello” :


on jassic(t)
	set q to {}
	considering case
		set AppleScript's text item delimiters to {""} 
		set q to every text item of t 		
		repeat with x from 1 to (count q) 
			
			if item x of q is "a" then set item x of q to "w"
			if item x of q is "b" then set item x of q to "v"
			if item x of q is "c" then set item x of q to "u"
			if item x of q is "d" then set item x of q to "t"
			if item x of q is "e" then set item x of q to "s"
			if item x of q is "f" then set item x of q to "r"
			if item x of q is "g" then set item x of q to "q"
			if item x of q is "h" then set item x of q to "p"
			if item x of q is "i" then set item x of q to "o"
			if item x of q is "j" then set item x of q to "n"
			if item x of q is "k" then set item x of q to "m"
			if item x of q is "l" then set item x of q to "l"
			if item x of q is "m" then set item x of q to "k"
			if item x of q is "n" then set item x of q to "j"
			if item x of q is "o" then set item x of q to "i"
			if item x of q is "p" then set item x of q to "h"
			if item x of q is "q" then set item x of q to "g"
			if item x of q is "r" then set item x of q to "f"
			if item x of q is "s" then set item x of q to "e"
			if item x of q is "t" then set item x of q to "d"
			if item x of q is "u" then set item x of q to "c"
			if item x of q is "v" then set item x of q to "b"
			if item x of q is "a" then set item x of q to "a"
			if item x of q is "x" then set item x of q to "z"
			if item x of q is "y" then set item x of q to "y"
			if item x of q is "z" then set item x of q to "x"
			
			
			--uppercase
			
			if item x of q is "A" then set item x of q to "W"
			if item x of q is "B" then set item x of q to "V"
			if item x of q is "C" then set item x of q to "U"
			if item x of q is "D" then set item x of q to "T"
			if item x of q is "E" then set item x of q to "S"
			if item x of q is "F" then set item x of q to "R"
			if item x of q is "G" then set item x of q to "Q"
			if item x of q is "H" then set item x of q to "P"
			if item x of q is "I" then set item x of q to "O"
			if item x of q is "J" then set item x of q to "N"
			if item x of q is "K" then set item x of q to "M"
			if item x of q is "L" then set item x of q to "L"
			if item x of q is "M" then set item x of q to "K"
			if item x of q is "N" then set item x of q to "J"
			if item x of q is "O" then set item x of q to "I"
			if item x of q is "P" then set item x of q to "H"
			if item x of q is "Q" then set item x of q to "G"
			if item x of q is "R" then set item x of q to "F"
			if item x of q is "S" then set item x of q to "E"
			if item x of q is "T" then set item x of q to "D"
			if item x of q is "U" then set item x of q to "C"
			if item x of q is "V" then set item x of q to "B"
			if item x of q is "W" then set item x of q to "A"
			if item x of q is "X" then set item x of q to "Z"
			if item x of q is "Y" then set item x of q to "Y"
			if item x of q is "Z" then set item x of q to "X"

		end repeat
		
	end considering
	set returnString to q as string --return it as a string, not a list
	return returnString
end jassic

The result is “Helli”

Only the last character, “o”, was changed.

No errors are thrown, and I have done a script similar to this before and it worked.

Could anyone point out what I am doing wrong? I think its a human error. Thanks for your help!

Hi,

as you have only if lines instead of if - else if the first letter (H) is set to P and later reset to H in the “is P” line

Duh!

Thank you, StefanK!

Knew it was something stupid. :slight_smile:

What you’re doing is very similar to Rot13 which has the advantage that the same code both encodes and decodes a string.

to Rot13(aString)
	set N to {}
	repeat with aChar in characters of aString
		set C to (ASCII number of aChar)
		if (C ≥ 65 and C ≤ 77) or (C ≥ 97 and C ≤ 109) then
			set end of N to ASCII character (C + 13)
		else if (C ≥ 110 and C ≤ 122) or (C ≥ 78 and C ≤ 90) then
			set end of N to ASCII character (C - 13)
		else
			set end of N to ASCII character C
		end if
	end repeat
	return N as string
end Rot13

set X to Rot13("Adam Bell") --> "Nqnz Oryy"

set y to Rot13(X) --> "Adam Bell"

Interesting!

I had thought of using ASCII, but that idea never went anywhere.

Thanks!

As from Leopard (Mac OS 10.5), you’d use ‘id’ rather than the now deprecated ‘ASCII number’ and ‘ASCII character’:

to Rot13(aString)
	set N to id of aString
	repeat with C in N
		if (C ≥ 65 and C ≤ 77) or (C ≥ 97 and C ≤ 109) then
			set C's contents to (C + 13)
		else if (C ≥ 110 and C ≤ 122) or (C ≥ 78 and C ≤ 90) then
			set C's contents to (C - 13)
		end if
	end repeat
	
	return string id N
end Rot13

set X to Rot13("Adam Bell") --> "Nqnz Oryy"

set y to Rot13(X) --> "Adam Bell"

Hmm. Apple never updated their help files…

http://developer.apple.com/library/mac/#releasenotes/AppleScript/RN-AppleScript/Introduction/Introduction.html

I meant the AppleScript help files that ARE PART OF THE COMPUTER. When I upgraded to 10.7 it reloaded only half of the help files. The one in question was still there. Oh well…

I so rarely do it that I forgot. Thanks, Nigel.