伙计们在python 2.7中看到下面的代码。 我遇到了“print(ciphertext)”和“sys.stdout.write(ciphertext)”部分代码的问题 当我运行代码时,“print(passline)和”sys.stdout.write(passline)“出来很好,即如果文件有一行显示”Billz“,它将按原样显示,但是当我尝试输出时函数(即sys.stdout.write()和print())密文(通过encryptMessage(密钥,消息)方法) 输出根据“myKey”变量分割不同的行(参见下面的代码和示例)
*我理解转置加密方法的局限性,但是'ciphertext'在原始行完成从它开始的行输出行之前到达新行
我认为问题在于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)
答案 0 :(得分:0)
当使用readlines
和大多数其他方式读取文件中的行时,python包含行中的换行符(因此,在您的情况下,passline
包含(a)换行符( S))。为防止这种情况,请在for循环的开头使用passline = passline.rstrip('\n\r')
之类的内容