def affineDecrypt(): text = request.form["affine-decrypt"] m = int(request.form["multKeyAffineDec"]) a = int(request.form["addKeyAffineDec"]) key = [m, a] sending = decrypt(text, key) return render_template(affineHTML, decrypted=sending)
def crackAffine(raw_text): stats = [] #will hold the error statistics alphabeta = [] #track the alpha beta combinations for a in xrange(26): if affine.verifyAlpha(a): for b in xrange(26): text = affine.decrypt(raw_text, a, b) #decrypt the text with all options #count the number of occurances of each unique letter letter_count = {} for i in xrange(len(text)): if text[i] in letter_count.keys(): letter_count[text[i]] += 1 elif text[i] not in letter_count.keys(): letter_count[text[i]] = 1 #calculate the frequency of the letters letter_freq = {} for key in letter_count.keys(): letter_freq[key] = round( float(letter_count[key]) / len(text) * 100, 5) #count the number of occurances of digrams in the text digram_count = {} n = 0 while n < len(text): if text[n:n + 2] in digram_count.keys(): digram_count[text[n:n + 2]] += 1 elif text[n:n + 2] not in digram_count.keys(): digram_count[text[n:n + 2]] = 1 n += 2 #calculate the frequency of the digrams digram_freq = {} for key in digram_count.keys(): digram_freq[key] = round( float(digram_count[key]) / len(text) / 2 * 100, 5) #calculate errors monogram_error = round( meanSquaredError(monogram_freq, letter_freq), 5) digram_error = round( meanSquaredError(bigram_freq, digram_freq), 5) print "a =", a, "b =", b, (monogram_error + digram_error) / 2 stats.append((monogram_error + digram_error) / 2) alphabeta.append((a, b)) #make a prediction about what values were used to encrypt the text predicted_index, predicted_min = findMinOfList(stats) print "Predicted Alpha =", alphabeta[predicted_index][ 0], "Predicted Beta =", alphabeta[predicted_index][ 1], "Error:", predicted_min
def Affine(): scroll = False msg = '' output = '' set1 = 'opt1' set2 = 'opt4' a = '1' b = '0' if request.method == 'POST': scroll = 'out' task = request.form.get('task') msg = request.form.get('original-text') set1 = request.form.get('set1') set2 = request.form.get('set2') a = request.form.get('value-of-a') b = request.form.get('value-of-b') if task == 'encrypt': output = affine.encrypt(msg, int(a), int(b)) if set1 == 'opt1': pass elif set1 == 'opt2': output = removeAllSpecialCharactersExcludingSpaces(output) elif set1 == 'opt3': output = removeAllSpecialCharactersIncludingSpaces(output) elif task == 'decrypt': output = affine.decrypt(msg, int(a), int(b)) if set1 == 'opt1': pass elif set1 == 'opt2': output = removeAllSpecialCharactersExcludingSpaces(output) elif set1 == 'opt3': output = removeAllSpecialCharactersIncludingSpaces(output) if set2 == 'opt4': pass elif set2 == 'opt5': output = output.upper() elif set2 == 'opt6': output = output.lower() print(scroll) return render_template('affine.html', title='Affine Cipher', msg=msg, output=output, set1=set1, set2=set2, a=a, b=b, scroll=scroll)
def decrypt(ciphertext): return affine.decrypt(25, 25, ciphertext)
def fetch(entries): # for entry in entries: # field = entry[0] # text = entry[1].get() # print('%s: "%s"' % (field, text)) # print(getKey(entries), getText(entries), entries[0][1].get(), entries[3][1].get()) res = "" if (entries[2][1].get() == "Vigenere Cipher"): if (entries[1][1].get() == "Encrypt"): res = vigenere.encryptStandard(getText(entries), getKey(entries)[0][0]) elif (entries[1][1].get() == "Decrypt"): res = vigenere.decryptStandard(getText(entries), getKey(entries)[0][0]) elif (entries[2][1].get() == "Full Vigenere Cipher"): if (entries[1][1].get() == "Encrypt"): res = vigenere.encryptFull(getText(entries), getKey(entries)[0][0], int(getKey(entries)[1][0])) elif (entries[1][1].get() == "Decrypt"): res = vigenere.decryptFull(getText(entries), getKey(entries)[0][0], int(getKey(entries)[1][0])) elif (entries[2][1].get() == "Auto Vigenere Cipher"): if (entries[1][1].get() == "Encrypt"): res = vigenere.encryptAutoKey(getText(entries), getKey(entries)[0][0]) elif (entries[1][1].get() == "Decrypt"): res = vigenere.decryptAutoKey(getText(entries), getKey(entries)[0][0]) elif (entries[2][1].get() == "Extended Vigenere Cipher"): if (entries[1][1].get() == "Encrypt"): res = vigenere.encryptExtended(getText(entries), getKey(entries)[0][0]) elif (entries[1][1].get() == "Decrypt"): res = vigenere.decryptExtended(getText(entries), getKey(entries)[0][0]) elif (entries[2][1].get() == "Playfair Cipher"): if (entries[1][1].get() == "Encrypt"): res = playfair.encrypt(getText(entries), getKey(entries)) elif (entries[1][1].get() == "Decrypt"): res = playfair.decrypt(getText(entries), getKey(entries)) elif (entries[2][1].get() == "Super Enkripsi"): if (entries[1][1].get() == "Encrypt"): res = supere.encrypt(getText(entries), getKey(entries)[0][0]) elif (entries[1][1].get() == "Decrypt"): res = supere.decrypt(getText(entries), getKey(entries)[0][0]) elif (entries[2][1].get() == "Affine Cipher"): if (entries[1][1].get() == "Encrypt"): res = affine.encrypt(getText(entries), int(getKey(entries)[0][0]), int(getKey(entries)[1][0])) elif (entries[1][1].get() == "Decrypt"): res = affine.decrypt(getText(entries), int(getKey(entries)[0][0]), int(getKey(entries)[1][0])) elif (entries[2][1].get() == "Hill Cipher"): if (entries[1][1].get() == "Encrypt"): res = hill.encrypt(getText(entries), getKey(entries)) elif (entries[1][1].get() == "Decrypt"): res = hill.decrypt(getText(entries), getKey(entries)) printResult(res)
def main(): parser = argparse.ArgumentParser(description='Image encriprition tool Uses the affine cipher or the Tiny Encryption Algorithm. Can also display ppm images for convenience.') parser.add_argument('image', help='Path to PPM image to be processed.') parser.add_argument('output', help='Output path to the result.') parser.add_argument('-e', '--encrypt', action="count", help='Set to encription mode.') parser.add_argument('-d', '--decrypt', action="count", help='Set to decription mode.') parser.add_argument('--cipher', required=True, type=str, choices=['affine', 'tea', 'none'], default='none', help='''Sets the cipher to be used by the program. If 'none' is chosen, the specified image will be displayed. Defaults to 'none'.''') args = parser.parse_args() if(args.encrypt == 0 and args.decrypt == 0 and args.cipher != 'none'): print('You have to either set decrypt or encrypt on to run the script.') parser.print_help() return elif(args.encrypt == 0 and args.decrypt == 0): print("You can't set both modes on.") parser.print_help() return image = Image.open(args.image) imData = image.getdata() if(args.cipher != 'none'): pixelData = [] for pixel in list(imData): pixelData.append(list(pixel)) result = [] if args.cipher == 'affine': ''' Affine cipher call ''' if(args.encrypt): print('Insert keys "a" and "b", respectively.') keyA = int(input('Key "a": ')) keyB = int(input('Key "b": ')) if not affine.checkKeys(keyA, keyB): print('Invalid keys, try again.') keyA = int(input('Key "a": ')) keyB = int(input('Key "b": ')) print('Starting encription...') result = affine.encrypt(pixelData, keyA, keyB) print('Encription successfull.') elif(args.decrypt): print('Insert keys "a" and "b", respectively.') keyA = int(input('Key "a": ')) keyB = int(input('Key "b": ')) if not affine.checkKeys(keyA, keyB): print('Invalid keys, try again.') keyA = int(input('Key "a": ')) keyB = int(input('Key "b": ')) print('Starting decription...') result = affine.decrypt(pixelData, keyA, keyB) print('Decription successfull.') elif args.cipher == 'tea': '''TEA cipher call''' key = [] print('Insert the four parts of the key in hexadecimal (32 bits, e.g.: 0x943abc12).') for i in range(4): keypart = input("Part [" + str(i) + "]: 0x" ) try: decimal = int(keypart, 16) key.append(decimal) except: print('Invalid value, try again.') i = i - 1 mode = input("Enter the mode of operation ('ecb'/'cfb'): ") if(args.encrypt): print('Starting encription...') if(mode == 'ecb'): result = tea.encrypt_ECB(pixelData, key) elif(mode == 'cfb'): result = tea.encrypt_CFB(pixelData, key) print('Encription successfull.') elif(args.decrypt): print('Starting decription...') if(mode == 'ecb'): result = tea.decrypt_ECB(pixelData, key) elif(mode == 'cfb'): result = tea.decrypt_CFB(pixelData, key) print('Decription successfull.') elif args.cipher == 'none': '''Just shows image and exits''' image.show() return #print(str(result)) print('Saving image as ' + args.output + '...') resultImage = Image.new(image.mode, image.size) resultImage.putdata(result) resultImage.show() if(args.encrypt): resultImage.save(args.output) elif(args.decrypt): resultImage.save(args.output) print('Image saved as ' + args.output + '...') return