Skip to content

Instantly share code, notes, and snippets.

@jameslyons
Last active May 27, 2024 20:52
Show Gist options
  • Select an option

  • Save jameslyons/8701593 to your computer and use it in GitHub Desktop.

Select an option

Save jameslyons/8701593 to your computer and use it in GitHub Desktop.

Revisions

  1. jameslyons revised this gist Jan 30, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions caesarCipher.py
    Original file line number Diff line number Diff line change
    @@ -7,13 +7,13 @@

    # encipher
    ciphertext = ""
    for c in plaintext:
    for c in plaintext.upper():
    if c.isalpha(): ciphertext += I2L[ (L2I[c] + key)%26 ]
    else: ciphertext += c

    # decipher
    plaintext2 = ""
    for c in ciphertext:
    for c in ciphertext.upper():
    if c.isalpha(): plaintext2 += I2L[ (L2I[c] - key)%26 ]
    else: plaintext2 += c

  2. jameslyons created this gist Jan 30, 2014.
    22 changes: 22 additions & 0 deletions caesarCipher.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    # we need 2 helper mappings, from letters to ints and the inverse
    L2I = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ",range(26)))
    I2L = dict(zip(range(26),"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))

    key = 3
    plaintext = "DEFEND THE EAST WALL OF THE CASTLE"

    # encipher
    ciphertext = ""
    for c in plaintext:
    if c.isalpha(): ciphertext += I2L[ (L2I[c] + key)%26 ]
    else: ciphertext += c

    # decipher
    plaintext2 = ""
    for c in ciphertext:
    if c.isalpha(): plaintext2 += I2L[ (L2I[c] - key)%26 ]
    else: plaintext2 += c

    print plaintext
    print ciphertext
    print plaintext2