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.
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
@Jyrh

Jyrh commented May 14, 2018

Copy link
Copy Markdown

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

Kenan7 commented Jul 4, 2018

Copy link
Copy Markdown

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

@letrckn

letrckn commented Jul 13, 2018

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

print ("my balls hurt")

@lewisn92

lewisn92 commented Jan 4, 2020

Copy link
Copy Markdown

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

Christian198231 commented Apr 27, 2021

Copy link
Copy Markdown

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

jameslyons commented Apr 27, 2021 via email

Copy link
Copy Markdown
Author

@Christian198231

Copy link
Copy Markdown

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
Copy Markdown

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

@alpair22

Copy link
Copy Markdown

thanks @Jyrh

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