所以我一直在研究这个脚本,只是创建一个简单的加密消息并将密文写入文件。我一直在第32行的“意外缩进”中收到错误,我无法弄清楚原因。我已经在网上找到了两段代码,并试图将它们组合起来,提出一个将密文写入文件的解决方案,但即使代码本身也能正常工作,我仍然会收到这些缩进错误。
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:
print '---->>>>))))mAmbA CiPH3r((((<<<<---- \n \n'
#set up loop, so the program can be rerun again if desired without restarting
option=raw_input("Would you like to encrypt or decrypt a message?\nEncrypt: 1\nDecrypt: 2\n")
if option=='1':
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
file1=raw_input("Please Enter The Path and File Name You'd Like to Write To: ")
target = open("file1", 'w')
target.truncate()
target.write(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=='2' :
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
答案 0 :(得分:1)
您可能忘记在复制文本时修复缩进。这五行需要缩进:
options=raw_input("Would You Like To Encrypt/Decrypt Again? Y/N\n")
if options=='y':
loop=5
if options=='n':
loop=0
在IDLE中执行此操作的最简单方法是进行至少包含所有五行中一部分的选择,并使用mac上的击键Control + [
或command + [
。