python automatic caesar cipher decrypt
def caesar_encrypt():
word = input('Enter the plain text: ')
c = ''
for i in word:
if (i == ' '):
c += ' '
else:
c += (chr(ord(i) + 3))
return c
def caesar_decrypt():
word = input('Enter the cipher text: ')
c = ''
for i in word:
if (i == ' '):
c += ' '
else:
c += (chr(ord(i) - 3))
return c
plain = 'hello'
cipher = caesar_encrypt(plain)
decipher = caesar_decrypt(cipher)
3.78
9
x = input()
NUM_LETTERS = 26
def SpyCoder(S, N):
y = ""
for i in S:
if(i.isupper()):
x = ord(i)
x += N
if x > ord('Z'):
x -= NUM_LETTERS
elif x < ord('A'):
x += NUM_LETTERS
y += chr(x)
else:
y += " "
return y
def GoodnessFinder(S):
y = 0
for i in S:
if i.isupper():
x = ord(i)
x -= ord('A')
y += letterGoodness[x]
else:
y += 1
return y
def GoodnessComparer(S):
goodnesstocompare = GoodnessFinder(S)
goodness = 0
v = ''
best_v = S
for i in range(0, 26):
v = SpyCoder(S, i)
goodness = GoodnessFinder(v)
if goodness > goodnesstocompare:
best_v = v
goodnesstocompare = goodness
return best_v
print(GoodnessComparer(x))
Thank you!
9
0
0
8
plaintext = input("Please enter your plaintext: ")
shift = input("Please enter your key: ")
alphabet = "abcdefghijklmnopqrstuvwxyz"
ciphertext = ""
# shift value can only be an integer
while isinstance(int(shift), int) == False:
# asking the user to reenter the shift value
shift = input("Please enter your key (integers only!): ")
shift = int(shift)
new_ind = 0 # this value will be changed later
for i in plaintext:
if i.lower() in alphabet:
new_ind = alphabet.index(i) + shift
ciphertext += alphabet[new_ind % 26]
else:
ciphertext += i
print("The ciphertext is: " + ciphertext)
Thank you!
8
0
Are there any code examples left?
New code examples in category Python