使用 python 解密文件
Decryption of files using python
嘿伙计们,我已经开始做一些 python 编码,我能够创建这个程序来解密我提供的加密文本,使用我提供的密钥有人可以帮助我更改这个文本解密进入文件解密。
import sys
def decrypt(cipher, key):
plain = ""
for index in range(len(cipher)):
if cipher[index].isalpha():
if cipher[index].isupper():
plain = plain + chr((ord(cipher[index]) - 64 - key) % 26 + 64)
elif cipher[index].islower():
plain = plain + chr((ord(cipher[index]) - 96 - key) % 26 + 96)
else:
plain = plain + cipher[index]
return plain
in_filename = sys.argv[1]
key = int(sys.argv[2])
out_filename = sys.argv[3]
with open(in_filename, "r") as f:
encrypted = f.read()
decrypted = decrypt(encrypted, key)
with open(out_filename, "w+") as f:
f.write(decrypted)
cipher = sys.argv[1]
plain = sys.argv[1]
key = int(sys.argv[2])
print('{}'.format(cipher))
print('{}'.format(decrypt(cipher, key)))
我可以通过在终端中输入这个命令来使用我当前的程序
python cipher.py 'Yaholy' 7将这个词解密成Rather
您可以使用已有的,只需从文件中读取,然后将结果写回。
in_filename = sys.argv[1]
key = int(sys.argv[2])
out_filename = sys.argv[3] # if you want
with open(in_filename, "r") as f:
encrypted = f.read()
decrypted = decrypt(encrypted, key)
with open(out_filename, "w+") as f:
f.write(decrypted)
print(decrypted) # if you don't want to save to a file
然后您可以调用它,例如 python cipher.py encrypted.txt 7 decrypted.txt
。
嘿伙计们,我已经开始做一些 python 编码,我能够创建这个程序来解密我提供的加密文本,使用我提供的密钥有人可以帮助我更改这个文本解密进入文件解密。
import sys
def decrypt(cipher, key):
plain = ""
for index in range(len(cipher)):
if cipher[index].isalpha():
if cipher[index].isupper():
plain = plain + chr((ord(cipher[index]) - 64 - key) % 26 + 64)
elif cipher[index].islower():
plain = plain + chr((ord(cipher[index]) - 96 - key) % 26 + 96)
else:
plain = plain + cipher[index]
return plain
in_filename = sys.argv[1]
key = int(sys.argv[2])
out_filename = sys.argv[3]
with open(in_filename, "r") as f:
encrypted = f.read()
decrypted = decrypt(encrypted, key)
with open(out_filename, "w+") as f:
f.write(decrypted)
cipher = sys.argv[1]
plain = sys.argv[1]
key = int(sys.argv[2])
print('{}'.format(cipher))
print('{}'.format(decrypt(cipher, key)))
我可以通过在终端中输入这个命令来使用我当前的程序
python cipher.py 'Yaholy' 7将这个词解密成Rather
您可以使用已有的,只需从文件中读取,然后将结果写回。
in_filename = sys.argv[1]
key = int(sys.argv[2])
out_filename = sys.argv[3] # if you want
with open(in_filename, "r") as f:
encrypted = f.read()
decrypted = decrypt(encrypted, key)
with open(out_filename, "w+") as f:
f.write(decrypted)
print(decrypted) # if you don't want to save to a file
然后您可以调用它,例如 python cipher.py encrypted.txt 7 decrypted.txt
。