我正在制作加密/解密代碼,這就是我到目前為止所擁有的......
def encrypt():
while True:
try:
userinp = input("Please enter the name of a file: ")
file = open(f"{userinp}.txt", "r")
break
except:
print("That File Does Not Exist!")
second = open("encoded.txt", "w")
for line in file:
line = line.strip()
swap = swapped(line)
print(swap)
change_char = change_character(swap)
reversed = reverse_word(change_char)
second.write(reversed)
file.close()
second.close()
def swapped(line):
line = line.strip()
new = ''
arranged = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
random = ["q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"]
for character in line:
i = arranged.index(character)
if arranged[i] == random[i]:
new = new random[i]
def change_character(swap):
empty = []
secondary = ""
num = ord(swap)
empty.append(num)
for i in range(len(empty)):
if num == 32:
num = 9888
else:
num = num 80
secondary = secondary chr(num)
return secondary
def reverse_word(change_char):
data_1 = change_char[::-1]
return data_1
def decrypt():
file = open(f"encoded.txt", "r")
decode = open("decrypted.txt", "w")
for line in file:
un = unswap(line)
change = unchange_char(un)
normal = unreverse(change)
decode.write(normal)
file.close()
decode.close()
我實際上是在嘗試以 3 種方式修改文本檔案,將其粘貼到另一個檔案中,然后使用該檔案對文本檔案進行解密,并將其放在另一個檔案中。
我這樣做的唯一真正問題是使我的一個更改功能起作用,這是在稱為“交換”的加密功能之后的第一個。我正在嘗試做的是在文本檔案中搜索適合排列串列的單詞,然后將這些字母與隨機串列中相同索引處的字母交換,以便文本檔案根據更改的內容進行相應的更改。我嘗試了很多不同的東西,而我現在正在嘗試的東西,我得到了錯誤......
“”不在串列中,
即使只是列印出排列[i]。我基本上已經嘗試了我能想到的一切,但沒有運氣。如果可能,我希望該函式繼續使用我創建的兩個串列。謝謝!
uj5u.com熱心網友回復:
您收到該錯誤的原因是空格字符不在串列中,因此您有兩個選擇:
- 您可以在兩個串列中添加一個空格。
- 您可以檢查您當前所在的角色是否為空格。
這將解決該問題,但我不確定該函式中的其余邏輯是否正確,此時它只會交換字符,如果它們是完全相同的插槽中的相同字符,這不會發生,這意味著你總是會得到一個空字串。
這就是我要做的,而不會改變你的功能:
def swapped(line):
line = line.strip()
new = ''
arranged = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
random = ["a","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"]
for character in line:
if character == ' ':
new = ' '
else:
i = arranged.index(character)
new = new random[i]
print(new)
swapped("lol this is just a test")
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/461273.html
標籤:Python python-3.x for循环 加密 索引
