def action(self): # Генератор # m m = self.field_m.get() if not m: m = rsa.randint(10, 10000) self.field_m.put(m) return else: m = int(m) # p p = self.field_p.get() if not p: p = rsa.genprime(32) self.field_p.put(p) else: p = int(p) if not rsa.solovay_strassen(p): messagebox.showerror("Ошибка", "Число p не простое") # q q = self.field_q.get() if not q: q = rsa.genprime(32) self.field_q.put(q) else: q = int(q) if not rsa.solovay_strassen(q) or q == p: messagebox.showerror("Ошибка", "Число q не простое") # k phi = (p - 1) * (q - 1) k = self.field_k.get() if not k: k = rsa.randint(2, phi - 1) while rsa.gcd(k, phi) != 1: k = rsa.randint(2, phi - 1) self.field_k.put(k) else: k = int(k) if not (1 < k < phi and rsa.gcd(k, phi) == 1): messagebox.showerror( "Ошибка", "Некорректное значение k (1 < k < %d и НОД(k, %d))" % (phi, phi)) # u0 N = p * q u0 = self.field_u0.get() if not u0: u0 = rsa.randint(2, N - 2) self.field_u0.put(u0) else: u0 = int(u0) if not (1 < u0 < N - 1): messagebox.showerror( "Ошибка", "Некорректное значение u0 (1 < u0 < %d)" % (N - 1)) seq = rsa.generate(m, p, q, k, u0) self.put(seq) self.calcStat()
def gen_asym_keys(self): if self.key_exc_alg == "rsa": self.my_kr = rsa.generate(2048)
import socket from utils import * import rsa import pickle import serpent public, private = rsa.generate(1024) with open('id_rsa', 'wb') as f: f.write(pickle.dumps(public)) with open('id_rsa.pub', 'wb') as f: f.write(pickle.dumps(private)) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.connect((HOST, PORT)) send_object(sock, public) session_key = rsa.decrypt(private, read_string(sock)) while True: print('Print the name of the desired file:') filename = input() if not len(filename): break send_object(sock, filename) encrypted_text = read_string(sock) text = serpent.decrypt(session_key, encrypted_text) print(text) print()
def RSA(action): if action == '2': title('RSA keygen', fillchar='-') print() print() print('Select directory for the keys.') Tk().withdraw() while True: outfolder = askdirectory( initialdir=cwd, title='Select directory to save keys in...') if not outfolder: print('Please choose a directory.') else: if not os.path.exists(outfolder): os.makedirs(outfolder, exist_ok=True) break bits = int(input('Size of the key (default is 2048): ') or 2048) rsa.generate(bits, outfolder) elif action == '3': title('RSA Encryption / Decryption', fillchar='-') [print() for i in range(5)] print('Do you want to...') print('1. Encrypt a file') print('2. Decrypt a file') print('3. Exit') print('_' * utils.get_terminal_size()[0]) print() action = input('>> ').lower() if action == '1': title('RSA Encryption / Decryption', fillchar='-') print() print() print('Select a file to encrypt in the dialog box.') Tk().withdraw() filename = askopenfilename(initialdir=cwd, title='Choose a file to encrypt...') print(filename) filedir = os.path.dirname(filename) print('Select the public key to encrypt the file with.') Tk().withdraw() keypath = askopenfilename(initialdir=cwd, title='Choose a public key...') print(keypath) keydir = os.path.dirname(keypath) print('Select the name for the encrypted file.') Tk().withdraw() outfile = asksaveasfilename(initialdir=filedir, title='Save as...') print('Select the name for the encrypted key.') Tk().withdraw() outkeyfile = asksaveasfilename(initialdir=filedir, title='Save as...') print(outfile) chunksize = input( 'Select chunksize (leave empty for default): ') or 64 * 1024 rsa.encrypt(keypath, filename, outfile, outkeyfile, chunksize) elif action == '2': title('RSA Encryption / Decryption', fillchar='-') print() print() print('Select a file to decrypt in the dialog box.') Tk().withdraw() filename = askopenfilename(initialdir=cwd, title='Choose a file to decrypt...') print(filename) filedir = os.path.dirname(filename) print('Select the private key to decrypt the file with.') Tk().withdraw() keypath = askopenfilename(initialdir=cwd, title='Choose a private key...') print(keypath) keydir = os.path.dirname(keypath) print('Select the encrypted key file used to encrypt the file.') Tk().withdraw() keyfilepath = askopenfilename( initialdir=filedir, title='Choose the encrypted key file...') print(keyfilepath) print('Select the name for the decrypted file.') Tk().withdraw() outfile = asksaveasfilename(initialdir=filedir, title='Save as...') print(outfile) chunksize = input( 'Select chunksize (leave empty for default): ') or 24 * 1024 rsa.decrypt(keypath, filename, keyfilepath, outfile, chunksize) elif action == '3': exit(0) elif action == '4': title('RSA Signature / verification', fillchar='-') [print() for i in range(5)] print('Do you want to...') print('1. Sign a file') print('2. Verify a file') print('3. Exit') print('_' * utils.get_terminal_size()[0]) print() action = input('>> ').lower() if action == '1': title('RSA Signature / verification', fillchar='-') print() print() print('Select file to sign...') Tk().withdraw() filename = askopenfilename(initialdir=cwd, title='Select file to sign...') print(filename) filedir = os.path.dirname(filedir) print('Select private key...') Tk().withdraw() privKey = askopenfilename(initialdir=cwd, title='Select private key...') print(privKey) print('Select name for signature file...') signature = asksaveasfilename( initialdir=filedir, title='Select signature filename...') or None print(signature) sign(filename, privKey, signature) elif action == '2': title('RSA Signature / verification', fillchar='-') print() print() print('Select file to verify...') Tk().withdraw() filename = askopenfilename(initialdir=cwd, title='Select file to verify...') print(filename) filedir = os.path.dirname(filename) print('Select public key...') Tk().withdraw() pubKey = askopenfilename(initialdir=cwd, title='Select public key...') print(pubKey) print('Select signature file...') signature = askopenfilename(initialdir=filedir, title='Select signature file...') print(signature) valid = verify(filename, signature, pubKey) if valid: print( 'Success! Signature and hash are the same, so the file has not been tampered with.' ) _tmp = input('Press enter to continue...') elif not valid: clear() print('FILE AUTHENTICITY COULD NOT BE VERIFIED!') print( 'Do not trust the file or its sender. Did you use the correct public key?' ) print( 'Error: Verification failed. Authenticity of file could not be verified.' ) elif action == '3': pass else: TypeError('invalid action: \'%s\' in RSA()')
except ConnectionRefusedError: continue break if choice: connect(sock1, destip, 1234) recv_sock = listen(sock2, 5555) send_sock = sock1 else: recv_sock = listen(sock1, 1234) connect(sock2, destip, 5555) send_sock = sock2 n, e, d = rsa.generate() if choice: send_sock.send((str(n) + ',' + str(e)).encode()) recvd = recv_sock.recv(2048).decode().split(',') else: recvd = recv_sock.recv(2048).decode().split(',') send_sock.send((str(n) + ',' + str(e)).encode()) other_n, other_e = [int(num) for num in recvd] win = tk.Tk() win.configure(background=bg) win.iconbitmap("./res/favicon.ico") win.geometry("400x525")