Created
December 29, 2016 19:44
-
-
Save mattsedlar/b28622edb296cb396a55ed74d81a7c9b to your computer and use it in GitHub Desktop.
Simple Python script that encrypts text file using monoalphabetic substitution cypher
This file contains 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
def encrypt_mono(file, key="key"): | |
'''This function encrypts text in a text file using a key and monoalphabetic substituion cypher''' | |
import string | |
def strip_punctuation(s): | |
return ''.join(c for c in s if c not in string.punctuation) | |
# remove repeating characters from key and add remaining letters of alphabet | |
alphabet = 'abcdefghijklmnopqrstuvwxyz' | |
key = key + alphabet | |
n = '' | |
for i in key: | |
if i not in string.ascii_letters: | |
n += i | |
elif i not in n: | |
n += i | |
dictionary = dict(zip(list(alphabet),n)) | |
with open(file,'r+') as text: | |
lines = '\n'.join(text.readlines()) | |
lines = lines.lower() | |
lines = lines.replace(' ','') | |
lines = strip_punctuation(lines) | |
for key, value in dictionary.iteritems(): | |
lines = lines.replace(key,value.upper()) | |
text.seek(0) | |
text.write(lines) | |
text.truncate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment