At this point we have established a working method for converting a plaintext character to a ciphertext character and we have done so with only 12 lines of code. Really only 8 if you use the same subroutine to encipher and decipher the text. To use this routine on a string of characters we need only use a simple repeat loop. Let’s do that now.
Let’s encrypt a message!
set charSet to "abcdefghijklmnopqrstuvwxyz"
-- ==| Note |=========
-- ==| Since our character set does not include spaces, for the time being
-- ==| we will remove the spaces from our plaintext message.
-- ==| We will, very shortly update our routines to deal with all
-- ==| 256 ascii characters.
set pString to "theeaglehaslanded"
set cString to ""
repeat with i from 1 to (count characters of pString)
set p to character i of pString
set p to offset of p in charSet
set c to getInverse(p, 5, 26)
set c to character c of charSet
set cString to (cString & c) as string
end repeat
return cString
on encipherText(p, m, n)
set c to (p * m) mod n
return c
end encipherText
Our resulting ciphertext is “vnyyeihyneqhertyt”. We can just as easily decipher this meaningless string of characters by doing the same thing we did above with the single letters.
Let’s decipher the message
set charSet to "abcdefghijklmnopqrstuvwxyz"
set cString to "vnyyeihyneqhertyt"
set pString to ""
repeat with i from 1 to (count characters of cString)
set c to character i of cString
set c to offset of c in charSet
set p to decipherText(p, 5, 26)
set p to character p of charSet
set p to (pString & p) as string
end repeat
return pString
on decipherText(c, m, n)
set p to (c * m) mod n
return p
end decipherText
This brings us back to our original message of “theeaglehaslanded”.
At this point you have an understanding of the basics of encryption. What we have done so far is very simple and not very secure. We are ready to start adding some meat to our encryption routines. I’m going to add some stuff that, if you do not understand, I will ask you to accept on trust.
We are going to not only multiply the numerical value of our plaintext characters my a value m but we are going to then shift that value forward by another number s. Additionally, to make the message more difficult to unscramble, we will not be using single characters, but two-character blocks to encipher our message. In much more robust and secure methods, characters would be broken into blocks of 64, 128 or 256 characters, thus increasing the difficulty of cracking them.
The addition of each character to the length of the blocks increases the possible number of letter combinations by an exponent. In our first example there are only 26 possible substitutions for each letter. By increasing our letter blocks to 2, we have 26^2 or 676 possible substitutions. You can see, as letters are added to the block size the number of possible combinations can grow very quickly. Thus a block length of 6 gives us 308,915,776 or (26^6) possible combinations. In the next part of this series I will show you how to do all of this using AppleScript. I will also introduce you to the RSA method of encryption. Yes, this method can be performed on a very small scale with AppleScript, however, using AppleScript to work with numbers of 400 digits is not feasible, but you will at least have an understanding of this very elegant method.