Welcome, guest | Sign In | My Account | Store | Cart
```"""
@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')
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))

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()

def mainAction(event):
if inputText.text.lower().rstrip() == 'set up cipher':
try:
outputText.text = 'The Cipher has been created'
except:
outputText.text = 'cipher.txt has not been created'
else:
outputText.text = 'Encrypting...'
try:
word = open(inputText.text, 'r+')
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'
try:
outputText.text = 'Encrypting...'
outputText.text = encryptedWord
except:
outputText.text = 'You have entered an invalid character'
else:
outputText.text = 'Please Choose File or Word'
outputText.text = 'Decrypting...'
try:
word = open(inputText.text, 'r+')
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'
try:
outputText.text = 'Decrypting...'
outputText.text = decryptedWord
except:
outputText.text = 'Please enter the correct format of the Encrypted word'
else:
outputText.text = 'Please Choose File or Word'
else:

goButton = JButton('Go!', actionPerformed=mainAction)

frame.show()
```

#### Diff to Previous Revision

```--- revision 2 2011-11-23 04:25:56
+++ revision 3 2011-11-23 05:37:04
@@ -14,7 +14,7 @@
mod = lambda self,n,m: n % m

def set_up_cipher(self): #Use this to change your cipher
-        alpha = '1234567890qwertyuiop[]asdfghjkl;zxcvbnm,.!@#\$%^&*()_+-=-{}:<>|QWERTYUIOPASDFGHJKLZXCVBNM ~`?'
+        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()
@@ -31,7 +31,7 @@
return baseExpanded

cipherFile = open('cipher.txt')
def wordEncrypt(self,word):
cipherWord = self.find(self.cipher,list(word))
keys = [randint(5001,7000), randint(2,5000)]
@@ -97,9 +97,9 @@
else:
+                outputText.text = 'Encrypting...'
try:
word = open(inputText.text, 'r+')
-                    outputText.text = 'Encrypting...'
word.close()
word = open(inputText.text, 'r+')
@@ -108,17 +108,21 @@
word.close()
outputText.text = 'The Text File Has Been Encrypted'
except:
-                    outputText.text = 'Please enter valid filename'
+                    outputText.text = 'Please enter valid filename or character'
-                outputText.text = 'Encrypting...'
+                try:
+                    outputText.text = 'Encrypting...'
+                    outputText.text = encryptedWord
+                except:
+                    outputText.text = 'You have entered an invalid character'
else:
outputText.text = 'Please Choose File or Word'
+                outputText.text = 'Decrypting...'
try:
word = open(inputText.text, 'r+')
-                    outputText.text = 'Decrypting...'
word.close()
word = open(inputText.text, 'r+')
@@ -131,7 +135,8 @@