Skip to content

Instantly share code, notes, and snippets.

@Ram-Aditya
Created October 18, 2018 16:41
Show Gist options
  • Save Ram-Aditya/b7b4eae0d1ba73aa392e104853f961bd to your computer and use it in GitHub Desktop.
Save Ram-Aditya/b7b4eae0d1ba73aa392e104853f961bd to your computer and use it in GitHub Desktop.
Crypto Mentorship: Caesar Cipher
# To run the file: python3 file_name method_flag (either 1:brute force or 2:frequency analysis)
import sys
cipher=open("cipher.txt","r")
lines=[]
for line in cipher:
lines.append(line)
if(int(sys.argv[1])==1):
#Try shifting and printing
while(True):
shift=int(input("Enter the shift: "))
for line in lines:
l=[]
for char in line:
if(ord(char)>=ord("a") and ord(char) <= ord("z") ):
l.append(chr( ( ( ord(char)-ord('a') )+shift )%26 +ord('a') ))
else:
l.append(char)
l=''.join(map(str, l))
print(l)
else:
#By frequency analysis
freq=[0 for _ in range(26)]
alphaCount=0
for line in lines:
l=[]
for char in line:
if(ord(char)>=ord("a") and ord(char) <= ord("z") ):
alphaCount+=1
freq[ord(char)-ord("a")]+=1
for i in range(26):
print((chr(ord("a")+i)),":",(freq[i]/alphaCount)*100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment