def main(): print('================== - Criptografia RSA - ======================') blockSize = 15 rsa = RSA() msg_f = open("original_msg") o_message = msg_f.read() print('================== -------------------- ======================') numList = str2num(o_message) print(' ') print('Mensagem original') print(o_message) print(' ') print('---------------------------') #print(' ') print(numList) blocks = num2block(numList, blockSize) #print(blocks) y = rsa.encryption(blocks) print("--------------------------") x = rsa.decryption(y) numList = block2num(x, blockSize) print('---------- Voltando numero para string----------------') #print(numList) print(' ') msg = num2str(numList) print(msg)
def send(self, data): receiverCertificate = data['certificate'] inputEntry = self.father.inputEntry message = inputEntry.get() sender = self.father.father.username receiver = receiverCertificate['username'] encryptionType = receiverCertificate['encryptionType'] receiverPublicKey = receiverCertificate['publicKey'] communicationType = receiverCertificate['communicationType'] less = 0 logArea = self.father.logArea log = ctime() + " Receive Certificate\n\n" if communicationType == 'rsa': log += ctime() + " Communication Type: RSA\n\n" # logArea.insert(tk.END, log + '\n\n') # logArea.see(tk.END) if encryptionType == 'caesar': # encryption = Caesar() c = Caesar() cypherText, key = c.encryption(message) r = RSA(0) encryptedKey = r.encryption(key, receiverPublicKey) # print(cypherText, self.father.father.selfInfo['priKey']) if len(cypherText) > 5: signature = r.sign( cypherText[0:6], self.father.father.selfInfo['priKey']) else: signature = r.sign( cypherText, self.father.father.selfInfo['priKey']) data = { 'type': 'singleChat', 'encryptedMessage': cypherText, 'encryptedKey': encryptedKey, 'signature': signature, 'to': receiver, 'from': sender } log += ctime() + " Encryption Type: Caesar\n\n" # logArea.insert(tk.END, log + '\n\n') # logArea.see(tk.END) elif encryptionType == 'playfair': p = Playfair() cypherText, key = p.encryption(message) r = RSA(0) encryptedKey = r.encryption(key, receiverPublicKey) if len(cypherText) > 5: signature = r.sign( cypherText[0:6], self.father.father.selfInfo['priKey']) else: signature = r.sign( cypherText, self.father.father.selfInfo['priKey']) data = { 'type': 'singleChat', 'encryptedMessage': cypherText, 'encryptedKey': encryptedKey, 'signature': signature, 'to': receiver, 'from': sender } log += ctime() + " Encryption Type: Playfair\n\n" elif encryptionType == 'des': d = des() key, temp = d.randomkey() cypherText = d.encrypt(key, message) # if message r = RSA(0) encryptedKey = r.encryption(key, receiverPublicKey) if len(cypherText) > 5: signature = r.sign( cypherText[0:6], self.father.father.selfInfo['priKey']) else: signature = r.sign( cypherText, self.father.father.selfInfo['priKey']) data = { 'type': 'singleChat', 'encryptedMessage': cypherText, 'encryptedKey': encryptedKey, 'signature': signature, 'to': receiver, 'from': sender } log += ctime() + " Encryption Type: DES\n\n" # pass else: d = des() keys = d.randomkey() # less = 0 if len(message) % 8 != 0: less = 8 - (len(message) - len(message) // 8 * 8) for i in range(less): message += '0' print(message) # print("--------------------", keys[0], keys[1]) cypherText = d.tencrypt(keys[0], keys[1], message) print("--------------------", keys[0], keys[1], cypherText) r = RSA(0) encryptedKey = r.encryption(keys, receiverPublicKey) if len(cypherText) > 5: signature = r.sign( cypherText[0:6], self.father.father.selfInfo['priKey']) else: signature = r.sign( cypherText, self.father.father.selfInfo['priKey']) data = { 'less': less, 'type': 'singleChat', 'encryptedMessage': cypherText, 'encryptedKey': encryptedKey, 'signature': signature, 'to': receiver, 'from': sender } log += ctime() + " Encryption Type: 3DES\n\n" else: mA = data['mA'] key = pow(mA, self.father.father.selfInfo['secretB'], self.father.father.selfInfo['pAndg'][0]) if encryptionType == 'caesar': c = Caesar(key) cypherText = c.encryption(message) r = RSA(0) encryptedKey = r.encryption(key, receiverPublicKey) if len(cypherText) > 5: signature = r.sign( cypherText[0:6], self.father.father.selfInfo['priKey']) else: signature = r.sign( cypherText, self.father.father.selfInfo['priKey']) data = { 'type': 'singleChat', 'encryptedMessage': cypherText, 'encryptedKey': encryptedKey, 'signature': signature, 'to': receiver, 'from': sender } log += ctime() + " Encryption Type: Playfair\n\n" elif encryptionType == 'playfair': p = Playfair() cypherText, key = p.encryption(message) r = RSA(0) encryptedKey = r.encryption(key, receiverPublicKey) if len(cypherText) > 5: signature = r.sign( cypherText[0:6], self.father.father.selfInfo['priKey']) else: signature = r.sign( cypherText, self.father.father.selfInfo['priKey']) data = { 'type': 'singleChat', 'encryptedMessage': cypherText, 'encryptedKey': encryptedKey, 'signature': signature, 'to': receiver, 'from': sender } log += ctime() + " Encryption Type: Playfair\n\n" elif encryptionType == 'des': d = des() key, temp = d.randomkey() cypherText = d.encrypt(key, message) # if message r = RSA(0) encryptedKey = r.encryption(key, receiverPublicKey) if len(cypherText) > 5: signature = r.sign( cypherText[0:6], self.father.father.selfInfo['priKey']) else: signature = r.sign( cypherText, self.father.father.selfInfo['priKey']) data = { 'type': 'singleChat', 'encryptedMessage': cypherText, 'encryptedKey': encryptedKey, 'signature': signature, 'to': receiver, 'from': sender } log += ctime() + " Encryption Type: DES\n\n" # pass else: d = des() keys = d.randomkey() # less = 0 if len(message) % 8 != 0: less = 8 - (len(message) - len(message) // 8 * 8) for i in range(less): message += '0' print(message) # print("--------------------", keys[0], keys[1]) cypherText = d.tencrypt(keys[0], keys[1], message) print("--------------------", keys[0], keys[1], cypherText) r = RSA(0) encryptedKey = r.encryption(keys, receiverPublicKey) if len(cypherText) > 5: signature = r.sign( cypherText[0:6], self.father.father.selfInfo['priKey']) else: signature = r.sign( cypherText, self.father.father.selfInfo['priKey']) data = { 'less': less, 'type': 'singleChat', 'encryptedMessage': cypherText, 'encryptedKey': encryptedKey, 'signature': signature, 'to': receiver, 'from': sender } log += ctime() + " Encryption Type: 3DES\n\n" # pAndg = receiverCertificate['pAndg'] # self.father.father.selfInfo['secretB'] = getPrimeNum(20) # mB = pow(pAndg[1], self.father.father.selfInfo['secretB'], pAndg[0]) # receiverCertificate['mB'] = mB pass jData = json.dumps(data) self.socket.send(jData.encode()) print('__send__' + jData) # update log log += ctime() + " Send Message Success\n\n" logArea.insert(tk.END, log) logArea.see(tk.END) # update text textArea = self.father.textArea if less != 0: message = message[0:-less] text = '['+ sender + ' -> ' + receiver + ' ' + \ ctime() + ']\n\t' + message + '\n' textArea.insert(tk.END, text) textArea.see(tk.END) inputEntry.delete(0, tk.END)