两个用户之间的邮件加密

埃马努埃莱

我正在写一个python脚本,只是为了练习密码学。我想与用户一起创建一个小程序,这些用户可以向自己发送消息,这些消息必须进行加密和解密。我创建了3个班级。第一个包含我可以使用的2个功能encrypt以及decrypt使用公钥和私钥的消息。第二类是Message类,它具有4个属性(the sendertext消息的the,recipientand和a timestamp)。使用class方法__str__,我创建了必须以以下格式发送的实际消息:From {the name of the sender}: {Message} {timestamp}第三类是User有了User,我可以初始化与每一个用户namepublicprivate密钥,一个用于接收加密消息的收件箱以及一个称为消息的列表,用于存储接收和解密的每个消息。每个用户可以通过使用收件人的公共密钥对消息进行加密来向其他用户发送消息,然后该用户可以使用自己的私钥对加密的消息进行解密。我想通过使用2个函数来执行此操作,该函数send对邮件进行加密并将其发送到收件人收件箱中。发送者必须使用此功能,并且该send功能运行receive接收者功能,功能遍历新消息,decrypts并将appends消息(From {sender.name} ...循环到消息列表。

然后,我尝试通过创建2个用户(Bob和Alice)来运行该程序并发送一些消息。但是我遇到了一些错误。

我尝试将encryptanddecrypt函数仅用于加密和解密一个简单的字符串(没有添加用户和其他内容),并且确实可以正常工作。

我在这里找到了两个函数(加密和解密):https : //medium.com/@securegns/implementing-asymmetric-encryption-to-secure-your-project-35368049cb5f

我绝对是密码学的初学者,我可能使用过不好的做法。

可能是什么问题?我不知道

import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
import base64
import datetime

class Crypt:

    @staticmethod
    def encrypt(public_key, text):
        cipher_text = public_key.encrypt(text, 32)[0]
        b64cipher = base64.b64encode(cipher_text)
        return b64cipher
    
    @staticmethod
    def decrypt(private_key, b64cipher):
        decoded_ciphertext = base64.b64decode(b64cipher)
        plain_text = private_key.decrypt(decoded_ciphertext)
        return plain_text

class Message:
    def __init__(self, sender, txt, recipient):
        self.sender = sender
        self.txt = txt
        self.recipient = recipient
        self.timestamp = datetime.datetime.now().date()

    def __str__(self):
        return f"From {self.sender}: {self.txt} {self.timestamp}"


class User:
    def __init__(self, name):
        random = Random.new().read
        self.name = name
        self.private_key = RSA.generate(1024, random)
        self.public_key = self.private_key.publickey()
        self.inbox = []
        self.messages = []

    def receive(self):
        # Decrypts encrypted messages in the inbox and transfers the decrypted messages into the messages 
          list
        for i in self.inbox:
            message = Crypt.decrypt(self.private_key, i)
            self.messages.append(message)
            self.inbox.pop()
        return self.messages

    def send(self, msg, recipient): 
        # User use send function and encrypts the message with the recipient's public key and appends into the recipient's inbox the encrypted message
        message = Message(self.name, msg, recipient)
        b64cipher = Crypt.encrypt(recipient.public_key, message.__str__())
        self.messages.append(message.__str__())
        recipient.inbox.append(b64cipher)
        recipient.receive()

Bob = User("Bob")

Alice = User("Alice")

Bob.send("Hi", Alice)
Alice.send("Hi Bob!", Bob)
Bob.send("How are you?", Alice)

错误


File "\encrypt.py", line 59, in send
    b64cipher = Crypt.encrypt(recipient.public_key, message.__str__())

File \encrypt.py", line 11, in encrypt
    cipher_text = public_key.encrypt(text, 32)[0]


File "Crypto\PublicKey\RSA.py", line 150, in encrypt
    return pubkey.pubkey.encrypt(self, plaintext, K)

File "\Crypto\PublicKey\pubkey.py", line 75, in encrypt
    ciphertext=self._encrypt(plaintext, K)

File "\Crypto\PublicKey\RSA.py", line 224, in _encrypt
    return (self.key._encrypt(c),)

File "\Crypto\PublicKey\_slowmath.py", line 65, in _encrypt
    return pow(m, self.e, self.n)
TypeError: unsupported operand type(s) for pow(): 'str', 'int', 'int'

import Crypto
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA256
from  Crypto.Cipher import PKCS1_OAEP
from Crypto import Random
import base64
import datetime


class Crypt:

    @staticmethod
    def encrypt(public_key, text):
        oaep = PKCS1_OAEP.new(public_key, SHA256)
        ciphertext = oaep.encrypt(bytes(text, encoding="ASCII"))
        # cipher_text = public_key.encrypt(text, 32)[0]
        b64cipher = base64.b64encode(ciphertext)
        return b64cipher

    @staticmethod
    def decrypt(private_key, b64cipher):
        decoded_ciphertext = base64.b64decode(b64cipher)
        oaep = PKCS1_OAEP.new(private_key, SHA256)
        plaintext = oaep.decrypt(decoded_ciphertext)

        # plain_text = private_key.decrypt(decoded_ciphertext)
        print("Decrypted Plaintext: {}".format(plaintext))
        return plaintext


class Message:
    def __init__(self, sender, txt, recipient):
        self.sender = sender
        self.txt = txt
        self.recipient = recipient
        self.timestamp = datetime.datetime.now().date()

    def __str__(self):
        return f"From {self.sender}: {self.txt} {self.timestamp}"


class User:
    def __init__(self, name):
        random = Random.new().read
        self.name = name
        self.private_key = RSA.generate(1024, random)
        self.public_key = self.private_key.publickey()
        self.inbox = []
        self.messages = []

    def receive(self):
        # Decrypts encrypted messages in the inbox and transfers the decrypted messages into the messages
        list
        for i in self.inbox:
            message = Crypt.decrypt(self.private_key, i)
            self.messages.append(message)
            self.inbox.pop()
            return self.messages

    def send(self, msg, recipient):
        # User use send function and encrypts the message with the recipient's public key and appends into the recipient's inbox the encrypted message
        message = Message(self.name, msg, recipient)
        b64cipher = Crypt.encrypt(recipient.public_key, message.__str__())
        self.messages.append(message.__str__())
        recipient.inbox.append(b64cipher)
        recipient.receive()


Bob = User("Bob")
Alice = User("Alice")
Bob.send("Hi", Alice)
Alice.send("Hi Bob!", Bob)
Bob.send("How are you?", Alice)
哈立德·加伯

修复后这是您的代码

import Crypto
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA256
from  Crypto.Cipher import PKCS1_OAEP
from Crypto import Random
import base64
import datetime


class Crypt:

    @staticmethod
    def encrypt(public_key, text):
        oaep = PKCS1_OAEP.new(public_key, SHA256)
        ciphertext = oaep.encrypt(bytes(text, encoding="ASCII"))
        # cipher_text = public_key.encrypt(text, 32)[0]
        b64cipher = base64.b64encode(ciphertext)
        return b64cipher

    @staticmethod
    def decrypt(private_key, b64cipher):
        decoded_ciphertext = base64.b64decode(b64cipher)
        oaep = PKCS1_OAEP.new(private_key, SHA256)
        plaintext = oaep.decrypt(decoded_ciphertext)

        # plain_text = private_key.decrypt(decoded_ciphertext)
        print("Decrypted Plaintext: {}".format(plaintext))
        return plaintext


class Message:
    def __init__(self, sender, txt, recipient):
        self.sender = sender
        self.txt = txt
        self.recipient = recipient
        self.timestamp = datetime.datetime.now().date()

    def __str__(self):
        return f"From {self.sender}: {self.txt} {self.timestamp}"


class User:
    def __init__(self, name):
        random = Random.new().read
        self.name = name
        self.private_key = RSA.generate(1024, random)
        self.public_key = self.private_key.publickey()
        self.inbox = []
        self.messages = []

    def receive(self):
        # Decrypts encrypted messages in the inbox and transfers the decrypted messages into the messages
        list
        for i in self.inbox:
            message = Crypt.decrypt(self.private_key, i)
            self.messages.append(message)
            self.inbox.pop()
            return self.messages

    def send(self, msg, recipient):
        # User use send function and encrypts the message with the recipient's public key and appends into the recipient's inbox the encrypted message
        message = Message(self.name, msg, recipient)
        b64cipher = Crypt.encrypt(recipient.public_key, message.__str__())
        self.messages.append(message.__str__())
        recipient.inbox.append(b64cipher)
        recipient.receive()


Bob = User("Bob")
Alice = User("Alice")
Bob.send("Hi", Alice)
Alice.send("Hi Bob!", Bob)
Bob.send("How are you?", Alice)

它打印正确的明文。我用于OAEP加密和解密,因为不建议单独加密明文,RSA因为RSA具有延展性,因此必须使用对明文增加一些随机性的加密,请参见此链接

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

计算两个日期之间的用户

来自分类Dev

是否可以为两个密钥但相同的电子邮件加密电子邮件?

来自分类Dev

如何在Ubuntu中为两个不同的用户配置两个邮件

来自分类Dev

获取两个用户之间的度数连接时出错

来自分类Dev

SQL仅在两个用户之间检索结果

来自分类Dev

在两个Bootstrap列之间创建用户定义的间隙

来自分类Dev

搜索介于两个日期之间的用户输入日期

来自分类Dev

查询两个用户之间的公共组

来自分类Dev

在两个用户控件/视图之间传递数据

来自分类Dev

在两个用户之间共享配置文件

来自分类Dev

获取两个用户之间的度数连接时出错

来自分类Dev

在两个用户窗体之间传递TextBox值

来自分类Dev

Xorg在两个用户之间切换DISPLAY

来自分类Dev

SQL仅在两个用户之间检索结果

来自分类Dev

在两个 Azure AD 之间同步用户

来自分类Dev

检索两个用户之间的聊天记录 sequelize

来自分类Dev

CORS 在两个项目和用户之间通过 javascript

来自分类Dev

我的加密货币的哈希值如何在两个哈希值之间跳跃?

来自分类Dev

如何-由两个用户检查一个电子邮件地址

来自分类Dev

通过VeraCrypt加密两个硬盘

来自分类Dev

如果这两个用户之间的距离小于某个值,如何在python中创建用户图,以使两个用户之间存在边?

来自分类Dev

在两个表之间选择每个用户的值之间的最小差异

来自分类Dev

如何在电子邮件中的两个关键词之间替换句子?

来自分类Dev

我可以在JavaMail(IMAP)中下载在两个日期之间收到的电子邮件吗?

来自分类Dev

如何在电子邮件中的两个关键词之间替换句子?

来自分类Dev

在GMail中,我可以在两个帐户之间自动转发电子邮件,以便同时查看两个帐户中的所有邮件吗?

来自分类Dev

RSpec测试,不允许两个用户使用相同的电子邮件地址

来自分类Dev

两个表之间的两个联接

来自分类Dev

如何验证两个单选按钮,用户应选择两个单选按钮中的任何一个(电子邮件或电话)

Related 相关文章

  1. 1

    计算两个日期之间的用户

  2. 2

    是否可以为两个密钥但相同的电子邮件加密电子邮件?

  3. 3

    如何在Ubuntu中为两个不同的用户配置两个邮件

  4. 4

    获取两个用户之间的度数连接时出错

  5. 5

    SQL仅在两个用户之间检索结果

  6. 6

    在两个Bootstrap列之间创建用户定义的间隙

  7. 7

    搜索介于两个日期之间的用户输入日期

  8. 8

    查询两个用户之间的公共组

  9. 9

    在两个用户控件/视图之间传递数据

  10. 10

    在两个用户之间共享配置文件

  11. 11

    获取两个用户之间的度数连接时出错

  12. 12

    在两个用户窗体之间传递TextBox值

  13. 13

    Xorg在两个用户之间切换DISPLAY

  14. 14

    SQL仅在两个用户之间检索结果

  15. 15

    在两个 Azure AD 之间同步用户

  16. 16

    检索两个用户之间的聊天记录 sequelize

  17. 17

    CORS 在两个项目和用户之间通过 javascript

  18. 18

    我的加密货币的哈希值如何在两个哈希值之间跳跃?

  19. 19

    如何-由两个用户检查一个电子邮件地址

  20. 20

    通过VeraCrypt加密两个硬盘

  21. 21

    如果这两个用户之间的距离小于某个值,如何在python中创建用户图,以使两个用户之间存在边?

  22. 22

    在两个表之间选择每个用户的值之间的最小差异

  23. 23

    如何在电子邮件中的两个关键词之间替换句子?

  24. 24

    我可以在JavaMail(IMAP)中下载在两个日期之间收到的电子邮件吗?

  25. 25

    如何在电子邮件中的两个关键词之间替换句子?

  26. 26

    在GMail中,我可以在两个帐户之间自动转发电子邮件,以便同时查看两个帐户中的所有邮件吗?

  27. 27

    RSpec测试,不允许两个用户使用相同的电子邮件地址

  28. 28

    两个表之间的两个联接

  29. 29

    如何验证两个单选按钮,用户应选择两个单选按钮中的任何一个(电子邮件或电话)

热门标签

归档