This is a Jython GUI of my previous recipe: Encrypt and Decrypt Text and Text Files
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | """
@Author = Alex Wallar
"""
from javax.swing import *
from java.lang import *
from java.awt import *
from math import ceil, log
from random import choice, randint
class EncryptionAndDecryption:
getVar = lambda self,searchList, ind: [searchList[i] for i in ind]
find = lambda self,searchList, elem: [[i for i, x in enumerate(searchList) if x == e] for e in elem]
mod = lambda self,n,m: n % m
def set_up_cipher(self): #Use this to change your cipher
alpha = '\'1234567890qwertyuiop[]asdfghjkl;zxcvbnm,.!@#$%^&*()_+-=-{}:<>|\/QWERTYUIOPASDFGHJKLZXCVBNM ~`?\"\n\t\\'
cipher = "".join([list(alpha)[randint(0,len(list(alpha))-1)] for i in range(5000)])
f = open('cipher.txt','r+')
f.truncate()
f.close()
f = open('cipher.txt','r+')
f.write(cipher)
f.close()
def baseExpansion(self,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 = [self.mod(base10//pow(b,j-p),b) for p in range(1,j+1)]
return baseExpanded
cipherFile = open('cipher.txt')
cipher = cipherFile.read()
def wordEncrypt(self,word):
cipherWord = self.find(self.cipher,list(word))
keys = [randint(5001,7000), randint(2,5000)]
encryptedWord = self.baseExpansion(list(map(choice, cipherWord)),keys[0],keys[1])
encryptedWord.extend(keys)
return list(map(int,encryptedWord))
def wordDecrypt(self,encryptedList):
encryptedWord = encryptedList[0:len(encryptedList)-2]
keys = encryptedList[len(encryptedWord):len(encryptedList)]
decryptedList = map(int,self.baseExpansion(encryptedWord, keys[1], keys[0]))
return "".join(self.getVar(self.cipher,decryptedList))
cipherFile.close()
frame = JFrame('Encryption & Decryption')
frame.setLayout(GridLayout(1, 2))
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
userChoicePanel = JPanel(GridLayout(4, 1))
inputOutputPanel = JPanel(GridLayout(4, 1))
inputPanel = JPanel(GridLayout(1, 2))
radioButtonEorDPanel = JPanel(GridLayout(1, 2))
radioButtonForWPanel = JPanel(GridLayout(1, 2))
eOrdLabel = JLabel('Encryption or Decryption')
fOrwLabel = JLabel('File or Word')
outputLabel = JLabel('Output:')
inputLabel = JLabel('Please Enter a Word or a Filename:')
inputText = JTextArea(editable=True)
outputText = JTextArea(editable=False)
outputText.setLineWrap(True)
outputText.setWrapStyleWord(True)
areaScrollPane1 = JScrollPane(outputText)
areaScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED)
inputText.setLineWrap(True)
inputText.setWrapStyleWord(True)
areaScrollPane2 = JScrollPane(inputText)
areaScrollPane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED)
buttonGroupeOrd = ButtonGroup()
buttonGroupfOrw = ButtonGroup()
eRadioButton = JRadioButton('Encrypt')
dRadioButton = JRadioButton('Decrypt')
fRadioButton = JRadioButton('File')
wRadioButton = JRadioButton('Word')
buttonGroupeOrd.add(eRadioButton)
buttonGroupeOrd.add(dRadioButton)
buttonGroupfOrw.add(fRadioButton)
buttonGroupfOrw.add(wRadioButton)
def mainAction(event):
eAd = EncryptionAndDecryption()
if inputText.text.lower().rstrip() == 'set up cipher':
try:
eAd.set_up_cipher()
outputText.text = 'The Cipher has been created'
except:
outputText.text = 'cipher.txt has not been created'
else:
if eRadioButton.isSelected():
if fRadioButton.isSelected():
outputText.text = 'Encrypting...'
try:
word = open(inputText.text, 'r+')
encryptedWord = eAd.wordEncrypt(word.read())
word.close()
word = open(inputText.text, 'r+')
word.truncate()
word.write(str(encryptedWord))
word.close()
outputText.text = 'The Text File Has Been Encrypted'
except:
outputText.text = 'Please enter valid filename or character'
elif wRadioButton.isSelected():
try:
outputText.text = 'Encrypting...'
encryptedWord = str(eAd.wordEncrypt(inputText.text))
outputText.text = encryptedWord
except:
outputText.text = 'You have entered an invalid character'
else:
outputText.text = 'Please Choose File or Word'
elif dRadioButton.isSelected():
if fRadioButton.isSelected():
outputText.text = 'Decrypting...'
try:
word = open(inputText.text, 'r+')
decryptedWord = eAd.wordDecrypt(eval(word.read()))
word.close()
word = open(inputText.text, 'r+')
word.truncate()
word.write(str(decryptedWord))
word.close()
outputText.text = 'The Text File Has Been Decrypted'
except:
outputText.text = 'Please enter a valid filename'
elif wRadioButton.isSelected():
try:
outputText.text = 'Decrypting...'
decryptedWord= str(eAd.wordDecrypt(eval(inputText.text)))
outputText.text = decryptedWord
except:
outputText.text = 'Please enter the correct format of the Encrypted word'
else:
outputText.text = 'Please Choose File or Word'
else:
outputText.text = 'Please Choose the Radio Buttons'
goButton = JButton('Go!', actionPerformed=mainAction)
radioButtonEorDPanel.add(eRadioButton)
radioButtonEorDPanel.add(dRadioButton)
radioButtonForWPanel.add(fRadioButton)
radioButtonForWPanel.add(wRadioButton)
inputPanel.add(areaScrollPane2)
inputPanel.add(goButton)
inputOutputPanel.add(inputLabel)
inputOutputPanel.add(inputPanel)
inputOutputPanel.add(outputLabel)
inputOutputPanel.add(areaScrollPane1)
userChoicePanel.add(eOrdLabel)
userChoicePanel.add(radioButtonEorDPanel)
userChoicePanel.add(fOrwLabel)
userChoicePanel.add(radioButtonForWPanel)
frame.add(userChoicePanel)
frame.add(inputOutputPanel)
frame.show()
|
There is some additional set up needed before this code will work. A cipher must be made. First a file named cipher.txt must be created in the same folder as you save the script. Run the script and in the in the text box labeled 'Please Enter a Word or a Filename:', input the text 'set up cipher' and press go. The command 'set up cipher' creates a new cipher and HAS to be done the first time you use the program. If you would like to change the cipher, the command may be ran again.
If there are any questions or concerns don't hesitate to email me at wallarelvo@hotmail.com