This program interactively lets you encrypt and decrypt text as well as text files using a key system as well as a cipher.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | from random import randint,choice
from math import ceil,log
getVar = lambda searchList, ind: [searchList[i] for i in ind]
find = lambda searchList, elem: [[i for i, x in enumerate(searchList) if x == e] for e in elem]
mod = lambda n,m: n % m
def set_up_cipher(): #Use this to change your cipher
alpha = '1234567890qwertyuiop[]asdfghjkl;zxcvbnm,.!@#$%^&*()_+-=-{}:<>|QWERTYUIOPASDFGHJKLZXCVBNM ~`?°'
cipher = "".join([list(alpha)[randint(0,len(list(alpha))-1)] for i in range(5000)])
f = open('cipher.txt','r+')
f.truncate()
f = open('cipher.txt','r+')
f.write(cipher)
def baseExpansion(n,c,b):
i = len(n)
base10 = sum([pow(c,i-k-1)*n[k] for k in range(i)])
j = int(ceil(log(base10 + 1,b)))
baseExpanded = [mod(base10//pow(b,j-p),b) for p in range(1,j+1)]
return baseExpanded
cipher = open('cipher.txt').read()+'\n'
def wordEncrypt(word):
cipherWord = find(cipher,list(word))
keys = [randint(5001,7000), randint(2,5000)]
encryptedWord = baseExpansion(list(map(choice, cipherWord)),keys[0],keys[1])
encryptedWord.extend(keys)
return list(map(int,encryptedWord))
def wordDecrypt(encryptedList):
encryptedWord = encryptedList[0:len(encryptedList)-2]
keys = encryptedList[len(encryptedWord):len(encryptedList)]
decryptedList = map(int,baseExpansion(encryptedWord, keys[1], keys[0]))
return "".join(getVar(cipher,decryptedList))
def mainEandD(): #Interactive
print('Please Enter e for Encryption or d for Decryption')
print(' ')
counter = True
counter2 = True
while counter:
while counter2:
func = input('Encrypt or Decrypt: ')
print(' ')
if func.lower() == 'e':
print('Would You Like to Encrypt a Word or a Text File?')
print(' ')
while True:
fOrw = input('F/W: ')
print(' ')
if fOrw.lower().rstrip() == 'w':
word = input('Enter Word: ')
print(' ')
print('Encrypting...')
print(' ')
print('The Encrypted Word is: {}'.format(wordEncrypt(word)))
print(' ')
counter2 = False
break
elif fOrw.lower().rstrip() == 'f':
while True:
try:
wordInput = input('Enter Filename: ')
print(' ')
print('Encrypting...')
print(' ')
word = open(wordInput, 'r+')
encryptedWord = wordEncrypt(word.read())
word.close()
word = open(wordInput, 'r+')
word.truncate()
word.write(str(encryptedWord))
word.close()
print('The Text File Has Been Encrypted')
print(' ')
counter2 = False
break
except:
print(' ')
print('Enter a Valid Filename or Type')
print(' ')
break
else:
print('Please enter f for File or w for Word')
print(' ')
elif func.lower() == 'd':
print('Would You Like to Decrypt a Word or Text File?')
print(' ')
while True:
fOrw = input('F/W: ')
print(' ')
if fOrw.lower().rstrip() == 'w':
while True:
try:
encryptedWord = eval(input('Enter Encrypted Word: ').rstrip())
print(' ')
print('Decrypting...')
print(' ')
print('The Decrypted Word is: {}'.format(wordDecrypt(encryptedWord)))
print(' ')
counter2 = False
break
except:
print(' ')
print('You did not enter a the correct type')
print(' ')
break
elif fOrw.lower().rstrip() == 'f':
while True:
try:
wordInput = input('Enter Filename: ')
print(' ')
print('Decrypting...')
print(' ')
word = open(wordInput, 'r+')
decryptedWord = wordDecrypt(eval(word.read()))
word.close()
word = open(wordInput, 'r+')
word.truncate()
word.write(str(decryptedWord))
word.close()
print('The Text File Has Been Decrypted')
print(' ')
counter2 = False
break
except:
print(' ')
print('You did not enter a the correct type or Filename')
print(' ')
break
else:
print('Please enter f for File or w for Word')
print(' ')
else:
print('Please Enter e for Encryption and d for Decryption')
print(' ')
print('Would you like to Encrypt or Decrypt another word or file?')
print(' ')
while True:
tryAgain = input('Y/N: ')
print(' ')
if tryAgain.lower().rstrip() == 'n':
print('Thank You!')
print(' ')
counter = False
break
elif tryAgain.lower().rstrip() == 'y':
counter2 = True
break
else:
print('Please Enter Either Y for Yes or N for No')
print(' ')
#mainEandD()
|
There is some set-up necessary before this program can run. After you create the python file containing the script above it is necessary for you to create a blank .txt file in the same folder as the script and name it 'cipher.txt'. Then run module in the python shell and enter the command:
>>> set_up_cipher()
After that you can run the mainEandD() command to encrypt and decrypt text or text files. If you are encrypting and decrypting text files, the file must be in the same folder as the script.
Full Example:
>>> mainEandD()
Please Enter e for Encryption or d for Decryption
Encrypt or Decrypt: e
Would You Like to Encrypt a Word or a Text File?
F/W: f
Enter Filename: file.txt
Encrypting...
The Text File Has Been Encrypted
Would you like to Encrypt or Decrypt another word or file?
Y/N: y
Encrypt or Decrypt: d
Would You Like to Decrypt a Word or Text File?
F/W: f
Enter Filename: file.txt
Decrypting...
The Text File Has Been Decrypted
Would you like to Encrypt or Decrypt another word or file?
Y/N: y
Encrypt or Decrypt: e
Would You Like to Encrypt a Word or a Text File?
F/W: w
Enter Word: Hello World
Encrypting...
The Encrypted Word is: [11, 1281, 115, 1834, 1209, 47, 2267, 1684, 495, 264, 1969, 2089, 479, 6774, 2306]
Would you like to Encrypt or Decrypt another word or file?
Y/N: y
Encrypt or Decrypt: d
Would You Like to Decrypt a Word or Text File?
F/W: w
Enter Encrypted Word: [11, 1281, 115, 1834, 1209, 47, 2267, 1684, 495, 264, 1969, 2089, 479, 6774, 2306]
Decrypting...
The Decrypted Word is: Hello World
Would you like to Encrypt or Decrypt another word or file?
Y/N: n
Thank You!
Please Note that it is only necessary to run set_up_cipher() the first time you use this program. Also in order for you to transmit messages, the person you are sending the information to must have the same cipher as you and have the program.
Please no harsh comments, this is only a beta. Suggestions are welcome!
If you have any specific concerns with how it is encrypted you may email me at wallarelvo@hotmail.com