在linux上获取python代码的词表

2024-04-23 23:14:08 发布

您现在位置:Python中文网/ 问答频道 /正文

看看这段代码,如果我想让这段代码从单词列表中随机抓取单词,直到得到一个叫做flag的东西,我该怎么办?你知道吗

from Crypto.Cipher import AES
import base64
import os

BLOCK_SIZE = 32

PADDING = '{'

# Encrypted text to decrypt
encrypted = "Z5p+ZK9f8m9z+wVHw2SsvS0qT0DtqiTY1+yStCzXvP4="

DecodeAES = lambda c, e:   c.decrypt(base64.b64decode(e)).rstrip(PADDING)

secrets = #here you need to open words.txt

for secret in secrets:
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)

    if (decoded.startswith('FLAG:')):
        print ("\n")
        print ("Success: "+secret+"\n")
        print (decoded+"\n")
    else:
        print ('Wrong password')

我尝试使用模块导入它,但仍然有相同的问题


Tags: theto代码importsizesecretstring单词
2条回答

我假设您知道如何打开和读取文件;在许多联机资料中都可以找到这一点。你知道吗

请注意,代码按顺序遍历单词列表。要随机获取条目,请尝试以下操作:

import random

word_list = [
    "apple",
    "baker",
    "cat",
    "FLAG:",
    "gorilla"
]

word = random.choice(word_list)
while not word.startswith("FLAG:"):
    print word
    word = random.choice(word_list)

一次较长运行的输出:

cat
apple
apple
gorilla
cat
cat
baker
cat
FLAG: stop here 
   DONE

当你说if (secret[-1:] == "\n"):你得到了所有的可能性,因为文本文件中的每一行都会以换行符结束。你可以去掉它,把secrets定义为[line.strip() for line in open("secrets.txt")]

相关问题 更多 >