首页 \ 问答 \ 如何逐行读取python中的txt文件并将每一行设置为变量(How to read txt file in python line by line and set each line to a variable)

如何逐行读取python中的txt文件并将每一行设置为变量(How to read txt file in python line by line and set each line to a variable)

我有在python和单词列表中编写的字典代码,解密特定加密文本的python代码在这里:

from Crypto.Cipher import AES
import base64
import os

BLOCK_SIZE = 32

PADDING = '{'

# Encrypted text to decrypt
encrypted = "t0ed+TDTf4e1V3Vz94nAN+nj1uDgMPZnfd7BDyBoy/GeGk6LiImMBPPHvN8DcLgIhWo4ByqxpZby99nQpU8KuA=="

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

f = open('words.txt')

for line in f.readlines():
    secret = line.rstrip('\n')
f.close()

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'

我希望代码循环遍历words.txt中的所有行,并尝试检查它们是否是解密过程的正确值,此代码在读取第一行时停止并输出wrong password


I have dictionary code which is programmed in python and a word list, the python code which decrypts a specific encrypted text is here:

from Crypto.Cipher import AES
import base64
import os

BLOCK_SIZE = 32

PADDING = '{'

# Encrypted text to decrypt
encrypted = "t0ed+TDTf4e1V3Vz94nAN+nj1uDgMPZnfd7BDyBoy/GeGk6LiImMBPPHvN8DcLgIhWo4ByqxpZby99nQpU8KuA=="

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

f = open('words.txt')

for line in f.readlines():
    secret = line.rstrip('\n')
f.close()

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'

I want the code to loop through all lines in the words.txt and try them to check if they are the right value for the decryption process, this code stops when it reads the first line and it output wrong password


原文:https://stackoverflow.com/questions/35363528
更新时间:2022-02-28 14:02

最满意答案

基于@Some程序员哥们的评论

UInt16 id2= Convert::ToInt16(textBox2->Text, 16);

是正确的答案。

谢谢@有些程序员老兄!


Based on @Some programmer dude 's comment

UInt16 id2= Convert::ToInt16(textBox2->Text, 16);

is the right answer.

Thank you @Some programmer dude !

相关问答

更多
  • 该机制称为动态数据交换。 当您调用方法UpdateData时,它会设置类CDataExchange的成员并调用DoDataExchange。 DDX只是全局函数,内部调用SetDlgItemText或GetDlgItemText。 设置/获取窗口文本的另一种方法是直接调用这些函数,例如 CEdit* pBoxOne; pBoxOne = (CEdit*) GetDlgItem(IDC_EDIT1); pBoxOne->SetDlgItemText("asd"); The mechanism is call ...
  • 使用com端口时,通常需要使用comm DCB设置端口的参数。 您通常还希望使用SetCommTimeouts来告诉您要如何写入和(尤其)从通信端口读取。 我在之前的回答中包含了一个工作示例。 When working with the com port, you typically want to set up the port's parameters with a comm DCB. You also usually want to use SetCommTimeouts to tell how yo ...
  • 您需要用#include 替换#include #include C ++标头没有.h扩展名,以区别于具有相同名称的C标头。 此外,您的程序不需要#include 标头 - 如果您需要从C ++程序调用stdio函数,无论如何都应该#include 。 编辑:“如果这确实是问题,错误应该在字符串变量的定义上”由PierreBdR评论 在MSVC ++中, #include 创建了一个包含的级联,在某 ...
  • 试试ES_PASSWORD: 为编辑控件中键入的每个字符显示星号(*)。 此样式仅对单行编辑控件有效。 Windows XP:如果编辑控件来自user32.dll,则默认密码字符为星号。 但是,如果编辑控件来自comctl32.dll版本6,则默认字符为黑色圆圈。 若要更改显示的字符,或设置或清除此样式,请使用EM_SETPASSWORDCHAR消息。 注意Comctl32.dll版本6不可再发行,但它包含在Windows XP或更高版本中。 要使用Comctl32.dll版本6,请在清单中指定它。 有关清 ...
  • 如果没有整个项目,我无法对此进行测试,但您可以尝试声明成员变量(而不是本地变量),以便可以应用必要的编组属性。 就像是: Imports System.Runtime.InteropServices Private features As System.Array I can't test this without your whole project, but ...
  • 你试过括号吗? #define IDR_OPBUTTON (_RESOURCE_ID + 10) Did you try to put parenthesis? #define IDR_OPBUTTON (_RESOURCE_ID + 10)
  • 这些宏在整数类型的变量中反转字节:short,unsigned short,int,unsigned int,long long和unsigned long long(假设它们具有相应大小的16,32和64位)。 例如,如果存在某些无符号short类型的变量,其值为0x1234,则在应用第一个宏ES16(0x1234)之后,结果将为0x3412。 可以更清楚地考虑一步一步的宏观行动 ((u16)_val)&0xff00)将等于0x1200 0x1200 >> 8将等于0x0012 ((u16)_val)&0 ...
  • 如前所述,您正在使用Unicode。 你必须为其他功能使用Unicode: long ldata = _wtol(str); CString声明中的某处是这样的: #ifdef UNICODE #define CString CStringW #else #define CString CStringA #endif 当项目编译为Unicode时, CString变为宽字符CStringW ,所以你必须使用宽字符版本的atol ,它是_wtol ,对于其他字符串函数都是一样的。 As mentioned ...
  • 基于@Some程序员哥们的评论 UInt16 id2= Convert::ToInt16(textBox2->Text, 16); 是正确的答案。 谢谢@有些程序员老兄! Based on @Some programmer dude 's comment UInt16 id2= Convert::ToInt16(textBox2->Text, 16); is the right answer. Thank you @Some programmer dude !
  • TCHAR或任何字符串数组只是指向第一个字符的指针。 你要比较的是指针的值,而不是字符串。 此外,您将数组指定为null,这是无意义的。 使用strcmp的 win32变体。 如果使用_tcscmp宏,它将在编译时使用正确的多字节/ unicode函数。 #define MAX_STRING 16523; TCHAR achValue[MAX_STRING]; ZeroMemory(achValue, sizeof(TCHAR) * MAX_STRING); sprintf(achValue, MAX_ ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)