-
-
Save fortable1999/a133169b9cdf48f88567e18dfe678db0 to your computer and use it in GitHub Desktop.
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) |
text=input("input cipherText:" )
A_ORD = ord('a')
E_ORD = ord('e')
Z_ORD = ord('z')
def get_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 output_plaintext(secret, count=0):
char_table = get_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))
secret = encrypt_text(text, 20)
for i in range(len(get_char(secret))):
print(output_plaintext(secret, i).upper(), i)
result=input("Is the plaintext readable as English? (yes/no):")
if result.lower() == "yes":
exit(0)
else:
i+=1
A_ORD = ord('a')
E_ORD = ord('e')
Z_ORD = ord('z')
def get_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 output_plaintext(secret, count=0):
char_table = get_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))
secret = encrypt_text(text, 20)
def main():
print("Cracking a Caesar cypher.")
print()
print()
text= input("Input cipherText: ")
for i in range(len(get_char(secret))):
print(output_plaintext(secret, i).upper(), i)
result=input("Is the plaintext readable as English? (yes/no):")
if result.lower() == "yes":
exit(0)
else:
i+=1
if name == "main":
main()
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
#run the project
for i in range(len(get_char(secret))):
print(output_plaintext(secret, i).upper())
result=input("Is the plaintext readable as English? (yes/no):")
#Judgement the if the plaintext readable
if result.lower() == "yes":
break
else:
i+= 1
if name == "main":
main()
Uh oh!
There was an error while loading. Please reload this page.