Welcome, guest | Sign In | My Account | Store | Cart
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():
    alpha = '1234567890qwertyuiop[]asdfghjkl;zxcvbnm,.!@#$%^&*()_+-=-{}:<>|QWERTYUIOPASDFGHJKLZXCVBNM~`? '
    cipher = "".join([list(alpha)[randint(0,len(list(alpha))-1)] for i in range(5000)])
    f = open('cipher.txt',"w")
    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()
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():
    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':
                word = input('Enter Word: ')
                print(' ')
                print('The Encrypted Word is: {}'.format(wordEncrypt(word)))
                print(' ')
                counter2 = False
            elif func.lower() == 'd':
                while True:
                    try:
                        encryptedWord = eval(input('Enter Encrypted Word: '))
                        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(' ')
            else:
                print('Please Enter e for Encryption and d for Decryption')
                print(' ')
        print('Would you like to Encrypt or Decrypt another word?')
        print(' ')
        while True:
            tryAgain = input('Y/N: ')
            print(' ')
            if tryAgain.lower() == 'n':
                print('Thank You!')
                print(' ')
                counter = False
                
                try:
                    exit(0)
                except:
                    exit(0)
            elif tryAgain.lower() == 'y':
                counter2 = True
                break
            else:
                print('Please Enter Either Y for Yes or N for No')
                print(' ')
                
#mainEandD()

Diff to Previous Revision

--- revision 1 2011-11-21 14:38:57
+++ revision 2 2011-11-21 14:41:18
@@ -5,10 +5,10 @@
 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(filename):
+def set_up_cipher():
     alpha = '1234567890qwertyuiop[]asdfghjkl;zxcvbnm,.!@#$%^&*()_+-=-{}:<>|QWERTYUIOPASDFGHJKLZXCVBNM~`? '
     cipher = "".join([list(alpha)[randint(0,len(list(alpha))-1)] for i in range(5000)])
-    f = open(filename,"w")
+    f = open('cipher.txt',"w")
     f.write(cipher)
 
 def baseExpansion(n,c,b):

History