Applescript hates "r"s?

I have a script that tests writing a list of encrypted information onto a file and reading the file to decrypt the information. It appears that when I encrypt “r” (that returns “\”) and when it comes back, I see “”. I looked into it and the encryption methods are causing the problems. I don’t know what is happening. Here is the script:

set thefile to ((path to home folder as string) & "list.txt")
set thedata to {encrypt("r"), encrypt("r")}
try
	set writefile to open for access file thefile with write permission
	set eof of file thefile to 0
	write thedata to writefile starting at eof as list
	close access writefile
on error
	try
		close access writefile
	end try
end try

set thelist to {}
read file thefile as list
repeat with i in result
	set thelist to thelist & decrypt(i)
end repeat
on encrypt(words_to_encrypt)
	set multiplier to 5
	set charList to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", " ", "~", "!", "@", "#", "$", "%", "^", "&", "*", "\\(", "\\)", "_", "+", "{", "}", "|", ":", "\\\"", "<", ">", "?", "`", "-", "=", "[", "]", "\\\\", ";", "'", ",", ".", "/", "a"}
	
	
	considering case
		--get a list of numbers for the words corresponding to the item numbers of the characters in charList
		set p_letter_list to text items of words_to_encrypt
		set p_num_list to {}
		repeat with i from 1 to (count of p_letter_list)
			set this_letter to item i of p_letter_list
			repeat with j from 1 to count of charList
				set this_char to item j of charList
				if this_letter is this_char then
					set end of p_num_list to j
					exit repeat
				end if
			end repeat
		end repeat
		--encrypt the numbers
		set modulus to count of charList
		set c_num_list to {}
		repeat with i from 1 to (count of p_num_list)
			set p_num to item i of p_num_list
			set c_num to ((p_num * multiplier) mod modulus)
			set end of c_num_list to c_num
		end repeat
		--get the characters for the encrypted numbers corresponding to the characters in charList
		set c_letter_list to {}
		repeat with i from 1 to (count of c_num_list)
			set this_item to item i of c_num_list
			set end of c_letter_list to (item this_item of charList)
		end repeat
		--coerce the encrypted characters into a string
		set c_string to c_letter_list as string
	end considering
	return c_string
end encrypt
on decrypt(words_to_decrypt)
	set multiplier to 5
	set charList to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", " ", "~", "!", "@", "#", "$", "%", "^", "&", "*", "\\(", "\\)", "_", "+", "{", "}", "|", ":", "\\\"", "<", ">", "?", "`", "-", "=", "[", "]", "\\\\", ";", "'", ",", ".", "/", "a"}
	
	considering case
		-- get a list of numbers for the encrypted word corresponding to the item numbers of the characters in charList
		set c_letter_list to text items of words_to_decrypt
		set c_num_list to {}
		repeat with i from 1 to (count of c_letter_list)
			set this_letter to item i of c_letter_list
			repeat with j from 1 to count of charList
				set this_char to item j of charList
				if this_letter is this_char then
					set end of c_num_list to j
					exit repeat
				end if
			end repeat
		end repeat
		-- calculate m2 which is the multiplicative inverse of the multiplier, and is needed to decrypt the encrypted word
		set n to count of charList
		set multiplier to multiplier as integer
		set m2 to 0
		repeat until ((multiplier * m2) mod n) = 1
			set m2 to m2 + 1
		end repeat
		-- decrypt the numbers
		set p_num_list to {}
		repeat with i from 1 to (count of c_num_list)
			set c_num to item i of c_num_list
			set p_num to ((c_num * m2) mod n)
			set end of p_num_list to p_num
		end repeat
		-- get the characters for the decrypted numbers corresponding to the character in charList
		set p_letter_list to {}
		repeat with i from 1 to (count of p_num_list)
			set this_item to item i of p_num_list
			set end of p_letter_list to (item this_item of charList)
		end repeat
		-- coerce the decrypted characters into a string
		set p_string to p_letter_list as string
	end considering
	return p_string
end decrypt

Hi, Dylan.

  1. Both handlers extract text items from their input text without mentioning text item delimiters, so it seems that text items is being used as a synonym for characters, which is wrong.

  2. The encryption the script produces for “r” is “\\” ” ie. two escaped backslashes. These backslashes, being two characters, are decrypted individually by the decryption handler. There’s no single escaped backslash in the “character” (!) list, so c_num_list remains empty, which leads to p_num_list remaining empty, which leads to p_letter_list remaining empty. When this is coerced to string, the result’s an empty text.

PS. If you pass the read list directly to the decryption handler, rather than feeding the items individually, it returns “rr”.

decrypt(read file thefile as list)

So text items is really being used as a synonym for items, which is also wrong.

I have just a little note to add.

The encryption method for that particular script is fine, but it can be a bit slow and it’s somewhat prone to problems like that.

Thanks to jobu for this shell script method:

echo 'encrypt me!' | openssl enc -bf -e -pass pass:'password' -salt -a

Change “-e” to “-d” to decrypt the string, of course.

Also, blowfish encryption is great, but if you want super-secure encryption, you can use “-aes256” instead.

I recommend this method because it’s quicker, easier, and more compact.

For the full information, see this thread: http://macscripter.net/viewtopic.php?id=19520

Hope this helps somehow,

SuperScripter

Thanks for the help. I think I will make my own encryption/decryption methods. SuperScripter, I look at that forum post before to see other alternatives. Nigel, I saw your post and tried to correct things, nothing helped one bit. Now on to scripting!

I got it! Here is it! It is a little weird:


encrypt("http://www.macscripter.net/index.php")

decrypt(result)

on encrypt(words_to_encrypt)
	set m to 5
	set charList to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", " ", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "{", "}", "|", ":", "\"", "<", ">", "?", "`", "-", "=", "[", "]", "\\", ";", "'", ",", ".", "/"}
	--beginning to encrypt
	considering case
		set k to characters of words_to_encrypt
		set r to {}
		set x to ""
		repeat with i in k
			repeat with l from 1 to count charList
				set t to item l of charList as string
				log (i & " and " & t & " = " & ((i as string) is t as string))
				if (i as string) = t then
					set x to l * m - 16
				end if
			end repeat
			set r to r & x
		end repeat
	end considering
	return r
end encrypt
on decrypt(words_to_decrypt)
	set charList to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", " ", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "{", "}", "|", ":", "\"", "<", ">", "?", "`", "-", "=", "[", "]", "\\", ";", "'", ",", ".", "/"}
	set r to ""
	set m to 5
	repeat with i in words_to_decrypt
		set r to r & item (i / m + 3) of charList
	end repeat
	return r
end decrypt