Skip to content

Instantly share code, notes, and snippets.

@jameslyons
Last active February 10, 2022 19:53
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jameslyons/8701593 to your computer and use it in GitHub Desktop.
Save jameslyons/8701593 to your computer and use it in GitHub Desktop.
Caesar Cipher Python
# 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.upper():
if c.isalpha(): ciphertext += I2L[ (L2I[c] + key)%26 ]
else: ciphertext += c
# decipher
plaintext2 = ""
for c in ciphertext.upper():
if c.isalpha(): plaintext2 += I2L[ (L2I[c] - key)%26 ]
else: plaintext2 += c
print plaintext
print ciphertext
print plaintext2
@linuxshivam
Copy link

how can i run in cent os 7

@Gustav-N
Copy link

lol

@Gustav-N
Copy link

:D

@arkan-leki
Copy link

lol
arr = "abcdefghijklmnopqrstuwxyz"

key = 3
plain = "hello world 2day"
cyper = "";
for c in plain:
if c.isalpha() : cyper += arr[(arr.index(c)+3)%26]
else: cyper += c

print(cyper)

@silverNitrateIon
Copy link

silverNitrateIon commented Mar 25, 2018

To reverse the cipher negate the value of the key. This will reverse the cipher and give back your initial text. Just adding this. I also stripped out the decipher part. It did really nothing.

@Jyrh
Copy link

Jyrh commented May 14, 2018

##This is my code, very proud definitely not copy and pasted.##
L2I = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ",range(26)))
I2L = dict(zip(range(26),"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))

key = 3
plaintext = (input("Enter text you would like to cipher:"))

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

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

print(plaintext)
print(ciphertext)
print(plaintext2)

@Kenan7
Copy link

Kenan7 commented Jul 4, 2018

you can use instead;
import string
L2I = dict(zip(string.ascii_uppercase,range(26)))

@letrckn
Copy link

letrckn commented Jul 13, 2018

a = list(input())
b = int(input())
s = ''
for i in range(0, len(a)):
c = ord(a[i])
c -= b
if c < ord('A'):
c += 26
s += chr(c)
print(s)
decoding the Caesar cipher

@HEELcaT666
Copy link

You cant use chr() and ord() because they use the whole ASCII library, which also includes lowercase and non alphanumerical characters, which may result in the wrong decryption.

@bak3rb0y
Copy link

print ("my balls hurt")

@lewisn92
Copy link

lewisn92 commented Jan 4, 2020

import time
plain = input("Enter your encryption: ")
time.sleep(0.5)
print("Decrypting...")
dcypher = ""

for c in plain:
if c.isalpha():
dcypher += arr[(arr.index(c) + -3) % 26]
else:
dcypher += c
time.sleep(1)
print(dcypher)

simple decipher code that you could use

@Christian198231
Copy link

Christian198231 commented Apr 27, 2021

I'm using this code for homework, and the sentence that I'm using has an exclamation mark at the end, but I have to remove it. what code would I need to add?

@jameslyons
Copy link
Author

jameslyons commented Apr 27, 2021 via email

@Christian198231
Copy link

Could you please give me an example of what that code would look like? I'm new to Python and this stuff is kinda out of my league. Thank you.

You could use a for loop to build a new string which is a copy of the old string, but only includes characters that are a-z. There are lots of ways of achieving it. Regards, James

On Tue, 27 Apr 2021, 3:50 pm TeacherChristian, @.***> wrote: @Christian198231 commented on this gist. ------------------------------ I'm using this code for homework, but the sentence that I'm using has an exclamation mark at the end, but I have to remove it. what code would I need to add? — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://gist.github.com/8701593#gistcomment-3721131, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAM7KQK6USLVAZB75UZRAG3TKZGBTANCNFSM4J3N64AQ .

@ZiadTariq506
Copy link

I needed to edit a littell bit to run on python3 but I like it good job pro : ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment