-
-
Save jameslyons/8701593 to your computer and use it in GitHub Desktop.
# 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 |
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.
print ("my balls hurt")
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
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?
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 .
I needed to edit a littell bit to run on python3 but I like it good job pro : ).
thanks @Jyrh
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