This page is a work in progress.You can help improve it. →

theme: Next, 1

• 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]

1. Start at the first letter of our plaintext
2. If the letter is "S" put a "Z" in the ciphertext
3. If the letter is "D" put a "D" in the ciphertext
4. If the letter is "G" put a "R" in the ciphertext
5. If the letter is "L" put a "T" in the ciphertext
6. ...
7. If the letter is "X" put a "I" in the ciphertext
8. If the letter is "Y" put a "M" in the ciphertext
9. If the letter is "Z" put a "J" in the ciphertext
10. If that was the last letter in the plaintext: STOP
11. Go to the next letter of our plaintext
12. Go back to step 2

# 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 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``

``  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 Z  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``

``  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 Z  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``

``  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 Z  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``

# 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] 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 = 0 1 % 26 = 1 2 % 26 = 2 3 % 26 = 3 4 % 26 = 4 5 % 26 = 5 6 % 26 = 6 7 % 26 = 7 8 % 26 = 8 9 % 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) % 26   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``
• One alphabet
• Works for any shift number

# How would you work with DECODING ?

[.autoscale: true]

# Example: ROT13

• Simple Caeser Cipher with offset 13 (half the alphabet)
• No need to use a negative code to decipher (since `26 - 13` is `13`)
• 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!`

© 2017 - 2021; Built with ♥ in St. Petersburg, Florida.