decrypt encrypted caesar python code example
Example 1: python automatic caesar cipher decrypt
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))
Example 2: caesar's encrption python'
def encrypt(text,s):
result = ""
for i in range(len(text)):
char = text[i]
if (char.isupper()):
result += chr((ord(char) + s-65) % 26 + 65)
else:
result += chr((ord(char) + s - 97) % 26 + 97)
return result
text = "CEASER CIPHER DEMO"
s = 4
print "Plain Text : " + text
print "Shift pattern : " + str(s)
print "Cipher: " + encrypt(text,s)