Last active
February 21, 2017 04:07
-
-
Save fortable1999/a133169b9cdf48f88567e18dfe678db0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A_ORD = ord('a') | |
E_ORD = ord('e') | |
Z_ORD = ord('z') | |
def count_char(text): | |
res = {} | |
for c in text.lower(): | |
if ord(c) not in range(A_ORD, Z_ORD+1): | |
continue | |
if c in res: | |
res[c] += 1 | |
else: | |
res[c] = 1 | |
return res | |
def encrypt_text(text, offset): | |
res = [] | |
for c in text.lower(): | |
if ord(c) not in range(A_ORD, Z_ORD+1): | |
res += c | |
else: | |
res += chr(A_ORD + (ord(c) - A_ORD + offset) % (Z_ORD - A_ORD)) | |
return "".join(res) | |
def decrypt_text(secret, count=0): | |
char_table = count_char(secret.lower()) | |
key_char, key_count = sorted(char_table.items(), key=lambda x: x[1], reverse=True)[count] | |
return encrypt_text(secret, -1 * (ord(key_char) - E_ORD)) | |
text = """ | |
Sorry but I already now those threats. Its more about those specifice sentences I mentioned before. I know that usally for questions which are not affimative any is used, but why is there some used | |
""" | |
secret = encrypt_text(text, 20) | |
for i in range(5): | |
print decrypt_text(secret, i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
print("Cracking a Caesar cypher.")
print()
print()
text= input("Input cipherText: ")
A_ORD = ord('a') #the alphbeta range a--z
E_ORD = ord('e')#the most used letter in English
Z_ORD = ord('z')
def get_char(text): #get the most used letters
res = {}
for c in text.lower(): #using the lower letter to find
if ord(c) not in range(A_ORD, Z_ORD+1):#keep the space
continue
if c in res: #the most used letter
res[c] += 1
else:
res[c] = 1
return res
def encrypt_text(text, offset): #this function get the shift
res = []
for c in text.lower():
if ord(c) not in range(A_ORD, Z_ORD+1):
res += c
else:
res += chr(A_ORD + (ord(c) - A_ORD + offset) % (Z_ORD - A_ORD))
return "".join(res)
def output_plaintext(secret, count=0):# this function get the decrypte text
char_table = get_char(secret.lower()) #using the most used letter in plaintext
key_char, key_count = sorted(char_table.items(),
key=lambda x: x[1], reverse=True)[count]
return encrypt_text(secret, -1 * (ord(key_char) - E_ORD))
#try to get the decrypte letter
secret = encrypt_text(text, 20)
def main(): #the main function
if name == "main":
main()