Python 3.4 - Caesar Cipher

Caesar ciphers take a list of values and rotates through them to give them a different value. The program below takes from the alphabet and creates a cipher of the message. It returns many variations of the cipher then returns the actual message.

import string
import collections


def caesar(rotate_string, number_to_rotate_by):

    upper = collections.deque(string.ascii_uppercase)
    lower = collections.deque(string.ascii_lowercase)

    upper.rotate(number_to_rotate_by)
    lower.rotate(number_to_rotate_by)

    upper = ''.join(list(upper))
    lower = ''.join(list(lower))

    return rotate_string.translate(str.maketrans(string.ascii_uppercase, upper)).translate(str.maketrans(string.ascii_lowercase, lower))


my_string = 'This is the message'my_string2 = caesar(my_string, 1)

for i in range(len(string.ascii_uppercase)):
    print(i, "|", caesar(my_string2, i))


/Users/mattfassnacht/PycharmProjects/untitled12/venv/bin/python /Users/mattfassnacht/PycharmProjects/untitled12/week9.py
0 | Sghr hr sgd ldrrzfd
1 | Rfgq gq rfc kcqqyec
2 | Qefp fp qeb jbppxdb
3 | Pdeo eo pda iaoowca
4 | Ocdn dn ocz hznnvbz
5 | Nbcm cm nby gymmuay
6 | Mabl bl max fxlltzx
7 | Lzak ak lzw ewkksyw
8 | Kyzj zj kyv dvjjrxv
9 | Jxyi yi jxu cuiiqwu
10 | Iwxh xh iwt bthhpvt
11 | Hvwg wg hvs asggous
12 | Guvf vf gur zrffntr
13 | Ftue ue ftq yqeemsq
14 | Estd td esp xpddlrp
15 | Drsc sc dro wocckqo
16 | Cqrb rb cqn vnbbjpn
17 | Bpqa qa bpm umaaiom
18 | Aopz pz aol tlzzhnl
19 | Znoy oy znk skyygmk
20 | Ymnx nx ymj rjxxflj
21 | Xlmw mw xli qiwweki
22 | Wklv lv wkh phvvdjh
23 | Vjku ku vjg oguucig
24 | Uijt jt uif nfttbhf
25 | This is the message

Process finished with exit code 0

https://inventwithpython.com/invent4thed/chapter14.html

Comments

Popular posts from this blog

UML - Use Case Diagram