def test_rsa_keypassing(self): r = crypto.RSA() s = crypto.RSA(False) key = r.GetPublicKey() s.LoadPublicKey(key) randstring = randomString().encode("utf-8") ciphertext = s.Encrypt(randstring) self.assertEqual(r.Decrypt(ciphertext), randstring)
def test_rsa(): r = c.Reciever(c.RSA()) key = r.generate_keys(bits=8) s = c.Sender(c.RSA()) s.set_key(key) text = 'KODE' enc = s.operate_cypher(text) dec = r.operate_cypher(enc) assert dec == text, f'RSA decrypt failed. Should be "{text}", was "{dec}"'
def __init__(self, mainqueuerecv, mainqueuesend, interparserqueue): super().__init__() self.mainqueuerecv = mainqueuerecv #Declare queue from tcp server to get the data self.mainqueuesend = mainqueuesend #Declare queue from tcp server to send data self.interparserqueue = interparserqueue #Declare queue to get the data from the other parsers self.parser_comms = None threading.Thread(target=self.GetInterParserQueue).start() #Get interparser queue to send data to other parsers threading.Thread(target=self.InterParser).start() self.client_rsa = crypto.RSA(True) #Initialize RSA creating a private key self.server_rsa = crypto.RSA(False) #Initialize RSA without create a key for future encryption to the server´ self.aes = crypto.AES(True) #Initialize AES for using after the key exchange self.use_rsa = True #Only use RSA on the key exchange self.connection_encrypted = False self.lastheartbeat = datetime.datetime.now() threading.Thread(target=self.Heartbeat).start() #Start Heartbeat thread threading.Thread(target=self.HeartbeatTimeout).start() #Start another heartbeat thread if the server dosent respond to exit Carnotaurus self.start() #Start the main parser thread
def click_Decrypt(self): key = self.Edit_key.toPlainText() p = self.Ciphertext.toPlainText() if self.Enc_mode1.currentIndex() == 0: test = crypto.Radiate() TextPlain = test.decryption(p.encode(), key.encode()) self.Plaintext.setPlainText(TextPlain) self.send_Show_msg( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ':仿射解密成功!') if self.Enc_mode1.currentIndex() == 1: if self.Enc_mode2.currentIndex() == 1: test = crypto.cypto_LFSR(key) p = base64.b64decode(p) TextPlain = test.do_crypt(p) self.Plaintext.setPlainText(TextPlain.decode()) self.send_Show_msg( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ':LSFR_J-K解密成功!') if self.Enc_mode2.currentIndex() == 0: test = crypto.RC4(key.encode()) TextPlain = test.do_crypt(p.encode()) self.Plaintext.setPlainText(TextPlain.decode()) self.send_Show_msg( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ':RC4解密成功!') if self.Enc_mode1.currentIndex() == 2: p = bytes().fromhex(p) if self.Enc_mode2.currentIndex() == 0: test = crypto.des_crypto(key.encode(), key.encode()) TextPlain = test.decrypt(p) self.Plaintext.setPlainText(TextPlain.decode()) self.send_Show_msg( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ':DES解密成功!') if self.Enc_mode2.currentIndex() == 1: test = crypto.aes_crypto(key.encode()) TextPlain = test.decrypt(p) self.Plaintext.setPlainText(TextPlain.decode()) self.send_Show_msg( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ':AES解密成功!') if self.Enc_mode1.currentIndex() == 3: if self.Enc_mode2.currentIndex() == 0: p = p.split(' ') p.pop() temp = [] for i in p: temp.append(int(int(i, 16))) print(temp) test = crypto.RSA() TextPlain = test.Decrypt(temp) print(TextPlain) self.Plaintext.setPlainText(TextPlain) self.send_Show_msg( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ':RSA解密成功!')
def click_Encrypt(self): key = self.Edit_key.toPlainText() p = self.Plaintext.toPlainText() if self.Enc_mode1.currentIndex() == 0: test = crypto.Radiate() TextCipher = test.encryption(p.encode(), key.encode()) self.Ciphertext.setPlainText(TextCipher) self.send_Show_msg( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ':仿射加密成功!') if self.Enc_mode1.currentIndex() == 1: if self.Enc_mode2.currentIndex() == 1: test = crypto.cypto_LFSR(key) TextPlain = test.do_crypt(p.encode()) TextPlain = base64.b64encode(TextPlain) self.Ciphertext.setPlainText(TextPlain.decode()) self.send_Show_msg( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ':LSFR+J-K加密成功!') if self.Enc_mode2.currentIndex() == 0: test = crypto.RC4(key.encode()) TextPlain = test.do_crypt(p.encode()) self.Ciphertext.setPlainText(TextPlain.decode()) self.send_Show_msg( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ':RC4加密成功!') if self.Enc_mode1.currentIndex() == 2: if self.Enc_mode2.currentIndex() == 0: test = crypto.des_crypto(key.encode(), key.encode()) TextCipher = test.encrypt(p.encode()) self.Ciphertext.setPlainText( str(binascii.b2a_hex(TextCipher))[2:-1]) self.send_Show_msg( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ':DES加密成功!') if self.Enc_mode2.currentIndex() == 1: test = crypto.aes_crypto(key.encode()) TextCipher = test.encrypt(p.encode()) self.Ciphertext.setPlainText( str(binascii.b2a_hex(TextCipher))[2:-1]) self.send_Show_msg( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ':AES加密成功!') if self.Enc_mode1.currentIndex() == 3: if self.Enc_mode2.currentIndex() == 0: test = crypto.RSA() TextCipher = test.Encrypt(p) msg = '' for i in TextCipher: if i != 0: msg += str(hex(i))[2:10] + ' ' self.Ciphertext.setPlainText(msg) self.send_Show_msg( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ':RSA加密成功!')
def __init__(self, mainqueuerecv, mainqueuesend, interparserqueue): super().__init__() self.mainqueuerecv = mainqueuerecv #Declare queue from tcp server self.mainqueuesend = mainqueuesend #Declare queue from tcp server self.server_rsa = crypto.RSA(True) #Initialize RSA creating a private key self.interparserqueue = interparserqueue #Declare queue to get the data from the other parsers self.parser_comms = None threading.Thread(target=self.GetInterParserQueue).start() #Get interparser queue to send data to other parsers threading.Thread(target=self.InterParser).start() self.client_rsa = {} self.start() #Start the main parser thread
def __init__(self, master=None): super().__init__(master) self.recipientOptions = ['Start New Chat'] self.rsa = crypto.RSA() self.key = [] self.username = '' self.listening = True self.recipients = {} self.chats = {} self.master = master self.createWidgets() self.pack()
def run(self): while True: datafromtcpserver = self.mainqueuerecv.get() #Get the data from the queue data = datafromtcpserver[1] addr = datafromtcpserver[0] try: data.decode("utf-8") except UnicodeDecodeError: data = self.DecryptData(addr, data) logger.debug(data) if data == b'conninit': self.mainqueuesend.put((addr, b'connrecv')) continue if data == b'hr': self.mainqueuesend.put((addr, b'hrok')) continue if b'-----BEGIN PUBLIC KEY-----' in data: rsa = crypto.RSA(False) rsa.LoadPublicKey(data) self.client_rsa[str(len(self.client_rsa))] = {'addr': addr, 'rsaobj':rsa, 'cryptocomms': False, 'use_rsa':True, 'aesobj':None} self.SendDataToClient(addr, self.server_rsa.GetPublicKey()) for i in self.client_rsa: if self.client_rsa[i]['addr'] == addr: self.client_rsa[i]['cryptocomms'] = True continue if b'key' in data and b'iv' in data and b'aesisfkncool' in data: for i in self.client_rsa: if self.client_rsa[i]['addr'] == addr: aes = crypto.AES(False) aes.LoadKey(data) #Try to load the AES key self.client_rsa[i]['aesobj'] = aes self.client_rsa[i]['use_rsa'] = False #Stop using RSA for encryption and start using AES self.client_rsa[i]['rsaobj'] = None #Delete the RSA key to free up some memory self.SendDataToClient(addr, b'aes_ok') #Send to client that the server accepted the aes key logger.debug("{} is now using encrypted communication".format(addr)) break continue if data.decode("utf-8").startswith("{"): #Redirect all json to interparser queue to process it jsondata = json.loads(data.decode("utf-8")) to = jsondata['type'] del jsondata['type'] jsondata['to'] = to jsondata['addr'] = addr self.parser_comms.put(jsondata)
def generate(self): rsa = crypto.RSA() aes = crypto.AES() bits = int(self.bits['values'][self.bits.current()]) keys = rsa.generateKey(bits) if (self.password.get() != ''): password = aes.keySchedule(self.password.get()) cypher = aes.encrypt(self.username.get().encode('utf-8').hex(), password) keyFile = filedialog.asksaveasfile( mode='w+', defaultextension='ckf', title='Select file', filetypes=(('Client Key', '*.ckf'), ('All Files', '*.*'))) for i in range(0, len(cypher), 16): keyFile.write(' '.join(cypher[i:i + 16]) + '\n') for i in range(2): keyFile.write('-' * 47 + '\n') keys[i] = aes.encrypt(keys[i][2:], password) for j in range(0, len(keys[i]), 16): keyFile.write(' '.join(keys[i][j:j + 16]) + '\n') keyFile.close() messagebox.showinfo('Successful', 'Key Generated Successfully') self.master.destroy()
def test_rsa_decrypt_only_bytes(self): r = crypto.RSA() with self.assertRaises(TypeError): r.Decrypt(randomString())
def test_rsa_dont_decrypt_when_public_key_only(self): r = crypto.RSA(False) self.assertEqual(r.Decrypt(randomString().encode("utf-8")), None)
def test_rsa(self): r = crypto.RSA() randstring = randomString().encode("utf-8") ciphertext = r.Encrypt(randstring) self.assertEqual(r.Decrypt(ciphertext), randstring)