我正在尝试为不和谐的机器人发出加密和解密命令,但是消息似乎没有被解密
import discord
from discord.ext import commands
from cryptography.fernet import Fernet
from collections.abc import Sequence
def make_sequence(seq):
if seq is None:
return ()
if isinstance(seq, Sequence) and not isinstance(seq, str):
return seq
else:
return (seq,)
def message_check(channel=None, author=None, content=None, ignore_bot=True, lower=True):
channel = make_sequence(channel)
author = make_sequence(author)
content = make_sequence(content)
if lower:
content = tuple(c.lower() for c in content)
def check(message):
if ignore_bot and message.author.bot:
return False
if channel and message.channel not in channel:
return False
if author and message.author not in author:
return False
actual_content = message.content.lower() if lower else message.content
if content and actual_content not in content:
return False
return True
return check
@bot.command()
async def encrypt(ctx):
user = ctx.author
await user.send("Please enter the message to decrypt.")
response = await bot.wait_for('message', check=message_check(channel=ctx.author.dm_channel))
message = response.content
encoded = message.encode()
key = Fernet.generate_key()
f = Fernet(key)
encrypted = f.encrypt(encoded)
await ctx.author.send("This is your encrypted message:")
await ctx.author.send(encrypted)
await ctx.author.send("This is the key to decrypt the message:")
await ctx.author.send(key)
@bot.command()
async def decrypt(ctx):
user = ctx.author
await user.send("Please enter the encrypted message.")
response = await bot.wait_for('message', check=message_check(channel=ctx.author.dm_channel))
encrypted = response.content
await user.send("Please enter the decryption key:")
response = await bot.wait_for('message', check=message_check(channel=ctx.author.dm_channel))
key = response.content
f = Fernet(key)
decrypted = f.decrypt(encrypted)
decoded = decrypted.decode()
await user.send(decoded)
print(decoded)
我试图检查消息是否最后甚至用print(decoded)
解密了,但没有打印任何内容。
我还检查了所有其他变量是否都适用于print(),以及除解码之外的所有其他工作。
答案 0 :(得分:1)
这与您编码和解码消息的方式有关。使用decrypt
时,您需要对消息和密钥进行编码;使用encrypt
时,可以选择对消息和密钥进行解码,以使其成为普通字符串,而不是发送至b'...'
时。用户。
以下修改有效,但请注意,由于未知message_check
,因此我注释掉了使用它的区域。
@bot.command()
async def encrypt(ctx):
user = ctx.author
await user.send("Please enter the message to decrypt.")
response = await bot.wait_for('message') # , check=message_check(channel=ctx.author.dm_channel))
message = response.content
encoded = message.encode()
key = Fernet.generate_key()
f = Fernet(key)
encrypted = f.encrypt(encoded)
await ctx.author.send("This is your encrypted message:")
await ctx.author.send(encrypted.decode())
await ctx.author.send("This is the key to decrypt the message:")
await ctx.author.send(key.decode())
@bot.command()
async def decrypt(ctx):
user = ctx.author
await user.send("Please enter the encrypted message.")
response = await bot.wait_for('message') # , check=message_check(channel=ctx.author.dm_channel))
encrypted = response.content.encode()
await user.send("Please enter the decryption key:")
response = await bot.wait_for('message') # , check=message_check(channel=ctx.author.dm_channel))
key = response.content.encode()
f = Fernet(key)
decrypted = f.decrypt(encrypted)
decoded = decrypted.decode()
await user.send(decoded)
print(decoded)