Missing Document Title
theme: Next, 1
Algorithms: Day Three
Real world examples: Transforming data, codes, and ciphers
These examples will use:
- Looping
- Transforming
- Conditionals
First Example: Substitution Cipher
- Transforms one input into another
- Attempts to make it difficult to decode the "ciphertext" back into "plaintext"
Simple substitution
- Consider only the uppercase letters A through Z
- For each letter, we have another unique letter to transform into
S D G L I F E A B C H J K M N O P Q R T U V W X Y ZZ D R T S E P O G A V K C H W F Q X N B L Y U I M J
Take an example. Encode the phrase "CODES"
S D G L I F E A B C H J K M N O P Q R T U V W X Y Z|vZ D R T S E P O G A V K C H W F Q X N B L Y U I M JCODESA
S D G L I F E A B C H J K M N O P Q R T U V W X Y Z|vZ D R T S E P O G A V K C H W F Q X N B L Y U I M JCODESAF
S D G L I F E A B C H J K M N O P Q R T U V W X Y Z|vZ D R T S E P O G A V K C H W F Q X N B L Y U I M JCODESAFD
S D G L I F E A B C H J K M N O P Q R T U V W X Y Z|vZ D R T S E P O G A V K C H W F Q X N B L Y U I M JCODESAFDP
S D G L I F E A B C H J K M N O P Q R T U V W X Y Z|vZ D R T S E P O G A V K C H W F Q X N B L Y U I M JCODESAFDPZ
What is our algorithm?
[.autoscale: true][.build-lists: true]
- Start at the first letter of our plaintext
- If the letter is "S" put a "Z" in the ciphertext
- If the letter is "D" put a "D" in the ciphertext
- If the letter is "G" put a "R" in the ciphertext
- If the letter is "L" put a "T" in the ciphertext
- ...
- If the letter is "X" put a "I" in the ciphertext
- If the letter is "Y" put a "M" in the ciphertext
- If the letter is "Z" put a "J" in the ciphertext
- If that was the last letter in the plaintext: STOP
- Go to the next letter of our plaintext
- Go back to step 2
We'll learn more efficient ways of doing things other than a bunch of "if"s
What do you think the algorithm is for decoding?
Caesar cipher
This is a "shift" cipher
Takes each letter and "shifts" it down the alphabet, circling back to the start if we go off the end.
A .. Z, with a shift number
Take the example of a shift of 3.
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 ZA 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 ZA 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 ZB 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
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 ZB 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
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 ZC 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
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 ZC 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
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 ZD 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
Encoding the phrase "CODES"
It works just like when using a substitution cipher.
What is the ciphertext?
Is there another way?
Can we use the position information?
What if there was a shift of 0?
1. Start with the first letter in the plain text2. For the given letter, figure out the index in the alphabet (e.g., A is 0, B is 1, ..., Y is 24, Z is 25)3. Go to that index in the shifted alphabet and copy that letter to the ciphertext4. If this is the last letter in the plain text: STOP5. Consider the next letter in the plain text6. Go to step 2
[fit] Let's encode SDGDAY
Walk through each step of the algorithm to get the encoded text (... which is the not very secure text of SDGDAY
)
What if the shift was more than 0?
How do we handle the "index in the shifted alphabet"?
First attempt, consider a shift of 3
1. Start with the first letter in the plain text2. For the given letter, figure out the index in the alphabet (e.g., A is 0, B is 1, ..., Y is 24, Z is 25)3. Add 3 to that index4. If the index is more than 25 (the index of Z), subtract 265. Go to that index in the alphabet and copy that letter to the ciphertext6. If this is the last letter in the plain text: STOP7. Consider the next letter in the plain text8. Go to step 2
[fit] Let's encode SDGDAY
Walk through each step of the algorithm to get the encoded text (what do you get?)
Only needs ONE alphabet! There is no need to keep that second alphabet order.
[fit] Another way to deal with "... went off the end of the alphabet."
[fit] Modulus
[fit] Division
Consider integer division.
19 divided by 8
is 2 with a remainder of 3
or
19 / 3
is 2 with a remainder of 3
[fit] That remainder is considered the "modulus."
We would say:
19 modulus 8
is 3
The symbol used in most programming languages is the %
19 % 8
is 3
[fit] How does this help us?
[.column]
Consider the index divided by 26, but only the remainder.
[.column]
0 % 26 = 01 % 26 = 12 % 26 = 23 % 26 = 34 % 26 = 45 % 26 = 56 % 26 = 67 % 26 = 78 % 26 = 89 % 26 = 910 % 26 = 1011 % 26 = 1112 % 26 = 1213 % 26 = 1314 % 26 = 1415 % 26 = 1516 % 26 = 1617 % 26 = 1718 % 26 = 1819 % 26 = 1920 % 26 = 2021 % 26 = 2122 % 26 = 2223 % 26 = 2324 % 26 = 2425 % 26 = 25
[.column]
... and when we run off the end ...
[.column]
20 % 26 = 2021 % 26 = 2122 % 26 = 2223 % 26 = 2324 % 26 = 2425 % 26 = 2526 % 26 = 0 <== Aha, we "wrap" around27 % 26 = 128 % 26 = 229 % 26 = 330 % 26 = 431 % 26 = 532 % 26 = 633 % 26 = 734 % 26 = 835 % 26 = 9
So another way to write our algorithm
1. Start with the first letter in the plain text2. For the given letter, figure out the index in the alphabet(e.g., A is 0, B is 1, ..., Y is 24, Z is 25)3. Go to the index in the alphabet given by the formula(current letter index + shift number) % 26and copy that letter to the ciphertext4. If this is the last letter in the plain text: STOP5. Consider the next letter in the plain text6. Go to step 2
- One alphabet
- Works for any shift number
How would you work with DECODING ?
[.autoscale: true]
Example: ROT13
- Simple Caesar Cipher with offset 13 (half the alphabet)
- No need to use a negative code to decipher (since
26 - 13
is13
) - Try here
- It was used for a while during the early days of the internet to avoid spoilers. Everything was plaintext.
- Example: "Wow, I just saw Empire Strikes Back! I can't believe
Qnegu Inqre vf Yhxr'f sngure!