我正在写一个python脚本,只是为了练习密码学。我想与用户一起创建一个小程序,这些用户可以向自己发送消息,这些消息必须进行加密和解密。我创建了3个班级。第一个包含我可以使用的2个功能encrypt
以及decrypt
使用公钥和私钥的消息。第二类是Message
类,它具有4个属性(the sender
,text
消息的the,recipient
and和a timestamp
)。使用class方法__str__
,我创建了必须以以下格式发送的实际消息:From {the name of the sender}: {Message} {timestamp}
。第三类是User
。有了User
,我可以初始化与每一个用户name
,public
并private
密钥,一个用于接收加密消息的收件箱以及一个称为消息的列表,用于存储接收和解密的每个消息。每个用户可以通过使用收件人的公共密钥对消息进行加密来向其他用户发送消息,然后该用户可以使用自己的私钥对加密的消息进行解密。我想通过使用2个函数来执行此操作,该函数send
对邮件进行加密并将其发送到收件人收件箱中。发送者必须使用此功能,并且该send
功能运行receive
接收者的功能,该功能遍历新消息,decrypts
并将appends
消息(From {sender.name} ...
)循环到消息列表。
然后,我尝试通过创建2个用户(Bob和Alice)来运行该程序并发送一些消息。但是我遇到了一些错误。
我尝试将encrypt
anddecrypt
函数仅用于加密和解密一个简单的字符串(没有添加用户和其他内容),并且确实可以正常工作。
我在这里找到了两个函数(加密和解密):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] 删除。
我来说两句