【发布时间】:2016-03-10 11:10:26
【问题描述】:
我有一个使用字典解密加密消息的脚本,问题是解密过程会产生大量垃圾(也称为非 ascii)字符。这是我的代码:
from Crypto.Cipher import AES
import base64
import os
BLOCK_SIZE = 32
PADDING = '{'
# Encrypted text to decrypt
encrypted = "WI4wBGwWWNcxEovAe3p+GrpK1GRRQcwckVXypYlvdHs="
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
adib = open('words.txt')
for line in adib.readlines():
secret = line.rstrip('\n')
if (secret[-1:] == "\n"):
print "Error, new line character at the end of the string. This will not match!"
elif (len(secret) >= 32):
print "Error, string too long. Must be less than 32 characters."
else:
# create a cipher object using the secret
cipher = AES.new(secret + (BLOCK_SIZE - len(secret) % BLOCK_SIZE) * PADDING)
# decode the encoded string
decoded = DecodeAES(cipher, encrypted)
print decoded+"\n"
到目前为止,我想到的是将decoded 字符串转换为 Ascii,然后排除非 ascii 字符,但它不起作用。
【问题讨论】:
-
能否请您提供一个“words.txt”文件内容示例
-
包含常用词,但这里有一些词
-
the and One Piece Episode Chapter Pirates Arc The Edit Volume his SLOTNAME Island that Luffy was for with section World Category Special Manga wikipedia Wiki Encyclopedia are Japanese this Anime SBS Vol page BEGIN END Help Wikia Blue Crew from User Buggy Straw Portrait Grand him Pirate New Template Marines they not Hat Devil FLUSH TOP BOXAD Navibox Monkey their Crocodile Down Page Start Shanks have Shichibukai all has Canon Rules wikia AllPages Fruit Zoro Beli Sea name when Image one Usopp Battle Government Guidelines Random -
为什么需要删除 ascii ?您可以使用 base64 编码
print base64.b64encode(decoded)清除解码字符串中的所有“非 ascii”字符 -
因为终端中所有解码后的字符串的输出都会以段落的形式告诉我答案
标签: python non-ascii-characters