Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Goal: put in "ATTACKATDAWN" with the key "LEMON" and get the string "LXFOPVEFRNHR" as output.
- Here's our vigenere square:
- ABCDEFGHIJKLMNOPQRSTUVWXYZ
- LMNOPQRSTUVWXYZABCDEFGHIJK
- EFGHIJKLMNOPQRSTUVWXYZABCD
- MNOPQRSTUVWXYZABCDEFGHIJKL
- OPQRSTUVWXYZABCDEFGHIJKLMN
- NOPQRSTUVWXYZABCDEFGHIJKLM
- The idea is that we'll go through the key (wrapping) for each letter of the message, and then use the rows to encipher/decipher
- To encode, look up key letter row, and find plaintext letter in top row. Trace column to matching row to get ciphertext letter
- To decode, Look at key letter row, find ciphertext letter, trace to top row.
- ATTACKATDAWN
- LEMONLEMONLE
- A
- [A]BCDEFGHIJKLMNOPQRSTUVWXYZ
- [L]MNOPQRSTUVWXYZABCDEFGHIJK
- T
- ABCDEFGHIJKLMNOPQRS[T]UVWXYZ
- EFGHIJKLMNOPQRSTUVW[X]YZABCD
- ...
- ATTACKATDAWN
- LEMONLEMONLE
- LXFOPVEFRNHR
- Alright, so I understand it.
- Now to do this programmatically.
- First list the letters' offsets from A:
- ATTACKATDAWN = 0, 19, 19, 0, 2, 10, 0, 19, 3, 0, 22, 13
- LEMONLEMONLE
- And then add the offsets mod 26, standard caesar cipher rules.
- L + 0 = L
- E + 19 = X
- M + 19 = F
- O + 0 = O
- N + 2 = P
- L + 10 = V
- E + 0 = E
- M + 19 = F
- O + 3 = R
- N + 0 = N
- L + 22 = H
- E + 13 = R
- Okay, now to decode...
- 1AM, can't think straight. Gonna guess.
- LXFOPVEFRNHR = 11 23 5 14 15 21 4 5 17 13 7 17
- L - 11 = 0 = A
- E - 23 = -19 = H
- M - 5 = 7 = H
- O - 14 = 0 = A
- N - 15 = -2 = Y
- L - 21 = -10 = Q
- E - 4 = 0 = A
- M - 5 = 7 = H
- O - 17 = -3 = ?
- N - 13 = 0 = ?
- L - 7 = 4 = ?
- E - 17 = -13 = ?
- Ugh, that's not even close to right.
- Oh. Duh. If A + B = C then C - B = A, not B - C = A. I got out from adding in + key, so need to be subtracting out - key = in.
- LEMON = 11, 4, 12, 14, 13
- LEMONLEMONLE = 11, 4, 12, 14, 13, 11, 4, 12, 14, 13, 11, 4
- L - 11 = A
- X - 4 = T
- F - 12 = T
- O - 14 = A
- P - 13 = C
- V - 11 = K
- E - 4 = A
- F - 12 = T
- R - 14 = D
- N - 13 = A
- H - 11 = W
- R - 4 = N
- ATTACK AT DAWN
- So when implementing, I'll just force the letters to be uppercase and treat everything as offsets (i.e. T -> toupper('T') - 'A' = 19 / offset 19 to chr -> 'A' + 19 = T). The offset logic will make it simpler to rotate about the alphabet (mod 26 to prevent over flow, while x < 0: x += 26 to prevent underflow. So keep it signed. While loop might not even be necessary)
- The only really tricky part will be dealing with non-alpha characters while keeping the code from turning into spaghetti.
Add Comment
Please, Sign In to add comment