我目前正在编写一个基本的python脚本,旨在做以下事情:
- 以密码文件为输入
- 对每个密码执行以下转换并写入新文件:
将其反转(例如password变为drowssap)
将所有' 0 '替换为' 0 ',' a '替换为' 4 ',' s '替换为' 5 '(例如password变为p455w0rd
使第一个字符大写(例如password变成password)
在密码后面加上1900 ~ 2014之间的年份,例如:password1900, password1901,…,password2014
"passwordlist.txt"在我的例子中包含一行文本"password"
目前为止我的代码是:
reading_file = open("passwordlist.txt", "r")
new_file_content = ""
for line in reading_file:
stripped_line = line.strip()
new_line = stripped_line.replace("a", "4").replace("o", "0").replace("s", "5").replace("d", "D")
new_file_content += "n" + new_line
reading_file.close()
writing_file = open("hashedpasslist.txt", "w")
years = map(str, range(1900, 2014))
writing_file.write(new_file_content[::-1])
writing_file.close()
我需要帮助使输出文件的第一个字符大写,你可以看到我只设法将'd'交换为'd'。最后,我想将从1900年到2014年的所有年份附加到密码中,因此输出应该是例如Dr0w554p1900, Dr0w554p,…,Dr0w554p2014,最好是列表格式。
可以用下面的代码片段
将第一个字符大写string[0].upper() + string[1:]
在你的代码中将是:
reading_file = open("passwordlist.txt", "r")
new_file_content = ""
for line in reading_file:
stripped_line = line.strip()
new_line = stripped_line.replace("a", "4").replace("o", "0").replace("s", "5")
new_line = new_line[0].upper() + new_line[1:]
new_file_content += "n" + new_line
reading_file.close()
writing_file = open("hashedpasslist.txt", "w")
years = map(str, range(1900, 2014))
writing_file.write(new_file_content[::-1])
writing_file.close()
至于年份,您需要遍历每一年,将其附加到字符串并将其输出到列表。
。
output = []
for year in range(1900, 2015):
appended_line = new_line + str(year)
output.append(appended_line)
你的最终代码看起来像这样:
reading_file = open("passwordlist.txt", "r")
writing_file = open("hashedpasslist.txt", "w")
new_file_content = ""
for line in reading_file:
reversed_line = line[::-1]
stripped_line = reversed_line.strip()
new_line = stripped_line.replace("a", "4").replace("o", "0").replace("s", "5").replace("d", "D")
for year in range(1900, 2015):
appended_line = new_line + str(year)
new_file_content += "n" + appended_line
writing_file.write(new_file_content)
reading_file.close()
writing_file.close()
我做了一些改变。首先,字符串在开始时是反转的(我忘了,你不能用reversed()
反转字符串,哦)。然后,您将看到,对于每个密码,它循环所有年份,将其附加到密码并最终将其写入文件。