如何从 txt 文件导入 raw_input / 将 raw_input 写入 txt 文件

How to import raw_input from a txt file / write raw_input to a txt file

所以我是 Python n00b,我正在尝试破解此脚本以创建端点加密工具。基本上,该脚本采用 raw_input 并使用 16 位数字字符串使用 AES 对其进行编码。为了解密该消息,您需要在其中手动粘贴编码文本,然后是密钥。有没有办法从文件中提取文本,然后将编码后的文本输出到不同的文件?

这是我目前所拥有的:

from Crypto.Cipher import AES
import string
import base64
import time
#import modules
PADDING = '{'
BLOCK_SIZE = 32
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
#prepare crypto method
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
#set encryption/decryption variables
loop=5
while loop==5:
     #set up loop, so the program can be rerun again if desired without restarting
    option=raw_input("Would You Like to Encrypt Or Decrypt Text?\nEncrypt: a\nDecrypt: b\n")
    if option=='a':
        letter=3
        while letter==3:
            secret = raw_input("Please Enter An Encryption Key {must be 16 characters long}: ")
            countTotal= (len(secret))
            if countTotal==16:
                cipher = AES.new(secret)
                letter=0
            else:
                print "Please Ensure The Key You Entered Is 16 Characters In Length\n"
                letter=3
                #this checks the encryption key to ensure it matches the correct length
        # encode a string
        data=raw_input("Please Enter Text You'd Like Encrypted: ")
        encoded = EncodeAES(cipher, data)
        print 'Encrypted string:', encoded
        options=raw_input("Would You Like To Encrypt/Decrypt Again? Y/N\n")
        if options=='y':
            loop=5
        if options=='n':
            loop=0

    if option=='b':

        encoded=raw_input("Please Enter The Encoded String:\n")
        letter=3
        while letter==3:
            secret=raw_input("Please Enter The Decryption Key:\n")
            countTotal= (len(secret))
            #this checks the encryption key to ensure it matches the correct length
            if countTotal==16:
                cipher = AES.new(secret)
                letter=0
                decoded = DecodeAES(cipher, encoded)
                print 'Decrypted string:', decoded
                options=raw_input("Would You Like To Encrypt/Decrypt Again? Y/N\n")
                if options=='y':
                    loop=5
                if options=='n':
                    loop=0
            else:
                print "Please Ensure The Key You Entered Is 16 Characters In Length\n"
                letter=3

if loop==0:
    print "Goodbye!!"
    time.sleep(2)
    exit
    #exits the program if desired by user 

您可以使用 open('filename.extension', 'r/w/...') 打开特定文件。然后您可以使用 read()、readline() 或 readlines() 浏览文件的内容。要写入文件,只需像这样打开一个文件:

f = open('filename.txt', 'w')     #make new file (open for write)
f.write('This is a test\n')       #write to that file

有关阅读和写作的更多信息,请参阅:https://docs.python.org/2/tutorial/inputoutput.html