【发布时间】:2017-11-16 13:12:06
【问题描述】:
大家可以在 python 2.7 中看到下面的代码。 我遇到了代码的“print(ciphertext)”和“sys.stdout.write(ciphertext)”部分的问题 当我运行代码时,“print(passline)”和“sys.stdout.write(passline)”出来的很好,即如果文件有一行写着“Billz”,它将按原样显示,但是当我尝试使用输出时任一函数(即 sys.stdout.write() 和 print())密文(通过 encryptMessage(key, message) 方法) 输出根据“myKey”变量跨行拆分(代码和示例见下文)
*我了解转置加密方法的局限性,但是'密文'会在原行完成输出之前从它开始的行开始换行
我认为问题在于 encryptMessage() 函数以及它如何与 enc() 方法交互,即特别是代码的 for ...in... 块
这有意义吗?
我认为这个问题的答案可以在以下情况下提供帮助 - 从文件中读取数据但不覆盖这些文件 - 尝试编写与日志、密码/单词列表相关的程序时 - 并了解 for、in 和 .join 如何协同工作
i.e. myKey = 1
C:\Users\baawan\Desktop\Cyber Sec\COMP_lang\python>py cypher7.py
would you like to Encrypt(e) or Decrypt(d): e
Enter file name: pass.txt
Enter Key: 1
This is a list of Passwords to be encrypted
This is a list of Passwords to be encrypted
Billz786
Billz786
123456
123456
Milly
Milly
Bilklzcfvcx
Bilklzcfvcx
i.e. myKey = 2
C:\Users\baawan\Desktop\Cyber Sec\COMP_lang\python>py cypher7.py
would you like to Encrypt(e) or Decrypt(d): e
Enter file name: pass.txt
Enter Key: 2
This is a list of Passwords to be encrypted
Ti sals fPswrst eecytdhsi ito asod ob nrpe
Billz786
Blz8
il76
123456
135
246
Milly
Mlyil
Bilklzcfvcx
Bllcvxikzfc
i.e. myKey = 4
C:\Users\baawan\Desktop\Cyber Sec\COMP_lang\python>py cypher7.py
would you like to Encrypt(e) or Decrypt(d): e
Enter file name: pass.txt
Enter Key: 4
This is a list of Passwords to be encrypted
T asfsrtecthi t sdo reisl Pws eyds ioao bnp
Billz786
Bz
i7l8l6
123456
15263
4
Milly
Myi
ll
Bilklzcfvcx
Blvizclcxkf
i.e. myKey = 8
C:\Users\baawan\Desktop\Cyber Sec\COMP_lang\python>py cypher7.py
would you like to Encrypt(e) or Decrypt(d): e
Enter file name: pass.txt
Enter Key: 8
This is a list of Passwords to be encrypted
Tafreth d eilPsedsia n
sstcitsors w y oobp
Billz786
B
illz786
123456
123456
Milly
Milly
Bilklzcfvcx
Bviclxklzcf
代码是
def enc():
myMessage = raw_input('Enter file name: ')
myKey = int(raw_input('Enter Key: '))
text_file = open(myMessage, "r")
lines = text_file.readlines()
for passline in lines:
myMessage = passline
ciphertext = encryptMessage(myKey, myMessage)
print(passline)
#sys.stdout.write(passline)
print ciphertext
#sys.stdout.write(ciphertext)
text_file.close()
def encryptMessage(key, message):
ciphertext = [''] * key
for col in range(key):
pointer = col
while pointer < len(message):
ciphertext[col] += message[pointer]
pointer += key
return ''.join(ciphertext)
【问题讨论】:
标签: python-2.7 printing output