Пример #1
0
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)
Пример #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")