def MyFileEncryptMAC(filepath): if not os.path.isfile(filepath): print("Invalid file path or nonexistent file.") return None, None, None, None, None, None else: #generate random keys print("Generating keys...") enc_key = KeyGen.generateKey() HMACKey = KeyGen.generateKey() #open and read the file print("Reading in file...") file = open(filepath, 'rb') plaintext = file.read() file.close() #encrypting the file print("Generating MAC and encrypting...") ciphertext, IV, tag = MyEncryptMAC(plaintext, enc_key, HMACKey) #seperating the file name and the file extension fileName, fileExt = os.path.splitext(filepath) #write the ciphertext to a new file at the same file path print("Writing encryption out...") file = open(fileName + constants.ENC_EXT, 'wb') file.write(ciphertext) file.close() return ciphertext, IV, tag, enc_key, HMACKey, fileExt
def GenerateKeyPair(filenamePrivK, filenamePK): if (filenamePK != '' and filenamePrivK != ''): KeyGen.GenerateKeyPair(filenamePrivK, filenamePK) messagebox.showinfo("Notification", "Keypair generated") else: messagebox.showerror("Error", "Please type in filenames for the keys") Menu()
def verify_RSA_keys(): if (not os.path.isfile(constants.RSA_PUBLIC_KEYPATH)) or (not os.path.isfile(constants.RSA_PRIVATE_KEYPATH)): KeyGen.RSA_generate_keys() print("At least one of the keys not found.\nGenerating new keys...") else: print("Keys found") return
def KeyGenerator(password): #generiert das Keypaar aus einem Passwort mit sha3 P = [ Decimal(1763), Decimal(YCalc(1763)) ] #Jan: P0 -> kurze Zahl; P1 -> lange Zahl mit Komma nach ca. 7 Stellen Privat = int(KeyGen.KeyGen(password), 16) Public = (multiplikation(P, Privat)) return Privat, Public
def ElGamalDecrypt(cipher, password): C = [0, 0] C[0] = Decimal(cipher.split('v')[0]) zwischenwert = cipher.split('v')[1] C[1] = Decimal(zwischenwert.split('u')[0]) d = Decimal(zwischenwert.split('u')[1]) x = int(KeyGen.KeyGen(password), 16) c1 = multiplikation(C, x)[0] m1 = str(d / c1 % prim + 1) m1 = m1.split('.') m1 = int(m1[0]) output = UTF8.UTFdeConvert(m1) return output
def on_encrypt_clicked(self, button): print("We're now encrypting") print("Name = ", self.entryName.get_text()) print("Age = ", self.entryAge.get_text()) print("Address = ", self.entryAddress.get_text()) print("Phone Number = ", self.entryNum.get_text()) print("Password = Do you think i'd be this stupid ;) ") print( "Confirm password = i'm really not that stupid, we're at a cyber security event." ) print( KeyGen.generateKey(self.entryName.get_text(), self.entryAge.get_text(), self.entryAddress.get_text(), self.entryNum.get_text()))
def create(self): """ PURPOSE: creates a new host instance, just contains keys for lookup RETURNS: the private key and a socket token """ host = Host() keys = KeyGen.generate() host.private = keys['private'] host.public = keys['public'] host.put() return { 'private': keys['private'], 'socket': channel.create(keys['public']) }
def create(self, name, model, pin): """ PURPOSE: creates a new host instance, just contains keys for lookup RETURNS: the private key and a socket token """ device = Device() keys = KeyGen.generate() device.private = keys['private'] device.public = keys['public'] device.name = name device.model = model device.pin = pin device.put() return { 'private': keys['private'] }
def test(): #testing MyEncrypt key = KeyGen.generateKey() ct, iv = MyEncrypt(b"Hello world", key) print(ct, iv) #testing MyDecrypt print(MyDecrypt(ct, key, iv)) #testing MyFileEncrypt path = "/Users/sovathana/Documents/CECS 378/test/test.jpg" ct, iv, key, ext = MyFileEncrypt(path) print('\nCiphertext: ', ct, '\nIV: ', iv, '\nKey: ', key, '\nExtension: ', ext) #testing MyFileDecrypt filePath = "/Users/sovathana/Documents/CECS 378/test/test.enc" pt = MyFileDecrypt(filePath, ext, key, iv) print('\nDecrypted plaintext: ', pt)
def test(): #testing MyEncrypt key = KeyGen.generateKey() ct, iv = MyEncrypt(b"Hello world", key) print(ct, iv) #testing MyDecrypt print(MyDecrypt(ct, key, iv)) #testing MyFileEncrypt path = r"%PATH%test.txt" ct, iv, key, ext = MyFileEncrypt(path) print('\nCiphertext: ', ct, '\nIV: ', iv, '\nKey: ', key, '\nExtension: ', ext) #testing MyFileDecrypt filePath = r"%PATH%test.enc" pt = MyFileDecrypt(filePath, ext, key, iv) print('\nDecrypted plaintext: ', pt)
def MyFileEncrypt(filePath): #generate a random key key = KeyGen.generateKey() #open and read the file file = open(filePath, 'rb') plaintext = file.read() file.close() #encrypting the file ciphertext, iv = MyEncrypt(plaintext, key) #seperating the file name and the file extension fileName, fileExt = os.path.splitext(filePath) #write the ciphertext to a new file at the same file path file = open(fileName + constants.ENC_EXT, 'wb') file.write(ciphertext) file.close() return ciphertext, iv, key, fileExt
def MyEncrypt(message, key): #check if the size of the key is incorrect if len(key) != constants.KEY_LENGTH: print("ERROR: key is not ", constants.KEY_LENGTH, " bytes long.") return #padding the message padder = padding.PKCS7(algorithms.AES.block_size).padder() padded_plaintext = padder.update(message) + padder.finalize() backend = default_backend() #generate random InitialVector iv = KeyGen.generateIV() #encrypting the message using AES algorithm with CBC mode cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend()) encryptor = cipher.encryptor() ciphertext = encryptor.update(padded_plaintext) + encryptor.finalize() return ciphertext, iv
def decrypt(key, input_filename, output_dir): input = open(input_filename, 'rb') tar_name = str(uuid.uuid4()) output = open(tar_name, 'wb') decrypt_file(key, input, output, chunksize) output.close() tar = tarfile.open(tar_name) tar.extractall(path=output_dir) os.remove(tar_name) key = KeyGen.GenerateKey(16*4, "This","Is","A test") plaintext = "plaintext.txt" #plaintext = "test_files/" enc_file = "cyphertext" outdir = "outdir/" encrypt(key, plaintext, enc_file) decrypt(key, enc_file, outdir) #Check input and output are identical #import filecmp #assert(filecmp.cmp(plaintext, plaintext_out))
if n == 0: r = [] r.append(0) r.append(0) return r if n % 2 == 1 and n != 1: zwischenwert = multiplikation(p, n - 1) r = additionMG(p, zwischenwert) return r if n == 1: return p x = int( KeyGen.KeyGen( 'das ist das passwort, welches zu kurz ist, du lappen, anscheinen sei er immernoch zu kurz. ich meine jetzt ernsthaft?' ), 16) def ElGamal(text, Key): m = int(UTF8.UTFConvert(text)) print(type(m)) k = random.randint(0, 100) P = [Decimal(3), Decimal(YCalc(3))] Y = multiplikation(P, x) c = multiplikation(Y, k)[0] Px = 3 C = multiplikation(P, k) d = c * m % prim
def KeyGenerator(password): P = [Decimal(3), Decimal(YCalc(3))] Privat = int(KeyGen.KeyGen(password), 16) Public = multiplikation(P, Privat) return Privat, Public
import KeyGen user_db = {} for _ in range(4): priv_key, pub_key = KeyGen.generateKeyPair() user_db[pub_key.to_string()] = {"priv_key": priv_key, "balance": 100}
import KeyGen import pickle import base64 import hashlib import sys import json import csv import os import string import glob # Open the file in read mode # Open the file in read mode import glob import errno client = KeyGen.SIDClient() KG = open("secret.key", "rb").read() KSE = open("KSE.key", "rb").read() c = dict() AllMap = dic()
return open("Key\\" + fileName + "_" + "key.key", "rb").read() def encrypt(filename, key): """ Given a filename (str) and key (bytes), it encrypts the file and write it """ f = Fernet(key) with open(filename, "rb") as file: # read all file data file_data = file.read() # encrypt data encrypted_data = f.encrypt(file_data) # write the encrypted file with open(filename, "wb") as file: file.write(encrypted_data) KeyGen.createKeis() path = "data" for path, subdir, files in os.walk(path): for name in files: #write_key() key = load_key(name) print('data: ' + os.path.join(path, name)) print('Key: ' + str(key)) for x in range(2): #print(x) encrypt(os.path.join(path, name), key)