Exemplo n.º 1
0
            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)
Exemplo n.º 2
0
            def chat(self, data):
                """接收聊天信息并打印"""
                sender = data['from']
                receiver = data['to']
                logArea = self.father.logArea
                log = ctime() + " Receive Message\n\n"

                encryptedMessage = data['encryptedMessage']
                encryptedKey = data['encryptedKey']
                signature = data['signature']
                senderPubKey = data['senderPubKey']
                decryptionType = self.father.father.selfInfo['decryptionType']
                # message = ''
                r = RSA(0)
                priKey = self.father.father.selfInfo['priKey']
                key = r.decryption(encryptedKey, priKey)
                if self.father.father.selfInfo['communicationType'] == 'rsa':

                    # priKey = self.father.father.selfInfo['priKey']
                    if len(encryptedMessage) > 5:
                        result = r.verify(encryptedMessage[0:6], signature,
                                          senderPubKey)
                    else:
                        result = r.verify(encryptedMessage, signature,
                                          senderPubKey)
                    if result:
                        log += ctime() + " Verify Successfully\n\n"
                        # father.father.logArea.insert(tk.END)
                        key = r.decryption(encryptedKey, priKey)
                        if decryptionType == 'caesar':
                            c = Caesar()
                            key = int(key)
                            message = c.decryption(encryptedMessage, key)
                            # pass
                        elif decryptionType == 'playfair':
                            p = Playfair(0)
                            p.generateMatrix(key)
                            message = p.decryption(encryptedMessage)
                            # pass
                        elif decryptionType == 'des':
                            d = des()
                            message = d.decrypt(key, encryptedMessage)
                        else:
                            d = des()
                            less = data['less']
                            # print("--------------------",key1, key2, encryptedMessage)
                            message = d.tdecrypt(key[2:10], key[14:22],
                                                 encryptedMessage)

                            message = message[0:-less]
                            # print("--------------------",message)
                            # pass
                    else:
                        # 输出不合格
                        pass
                else:
                    r = RSA(0)
                    if len(encryptedMessage) > 5:
                        result = r.verify(encryptedMessage[0:6], signature,
                                          senderPubKey)
                    else:
                        result = r.verify(encryptedMessage, signature,
                                          senderPubKey)
                    if result:
                        log += ctime() + " Verify Successfully\n\n"
                        if decryptionType == 'caesar':
                            key = self.father.father.selfInfo['key']
                            c = Caesar(key)
                            ct, secretKey = c.encryption('aaa')
                            message = c.decryption(encryptedMessage[0],
                                                   secretKey)
                            print('-----------pass')
                        elif decryptionType == 'playfair':
                            p = Playfair(0)
                            p.generateMatrix(key)
                            message = p.decryption(encryptedMessage)
                        elif decryptionType == 'des':
                            d = des()
                            message = d.decrypt(key, encryptedMessage)
                        else:
                            d = des()
                            less = data['less']

                            message = d.tdecrypt(key[2:10], key[14:22],
                                                 encryptedMessage)

                            message = message[0:-less]
                            # pass
                    else:
                        print('-----------not pass')
                        pass
                # print("i am here")
                textArea = self.father.textArea
                text = '['+ sender + ' -> ' + receiver + ' ' + \
                        ctime() + ']\n\t' + message + '\n'
                textArea.insert(tk.END, text)
                textArea.see(tk.END)

                logArea.insert(tk.END, log)
                logArea.see(tk.END)

                print("i am here")