为什么不能使用增量加密并解密照片文件?
我制作了一个非常简单的加密和解密程序来通过将所有字节增加6来加密文件。但是,在测试中,只有文本文件工作。如果我使用它来加密和解密照片,则OS无法读取结果。
Python中的代码:
import os.path
class fileEncryptor:
@staticmethod
def encrypt(fileLocation, destination):
if os.path.exists(fileLocation):
file = open(fileLocation, "rb")
fileContents = file.read() # fileContents is a byte string
file.close()
btAr = bytearray(fileContents) # Byte string needs to be changed to byte array to manipulate
length = len(btAr)
n = 0
while n < length:
increment = 6
if btAr[n] <= 249:
btAr[n] = btAr[n] + increment
if 249 < btAr[n] <= 255:
btAr[n] = btAr[n] - 250
n = n + 1
encryptedFile = open(destination, "wb")
encryptedFile.write(btAr)
encryptedFile.close()
else:
print("File does not exist")
@staticmethod
def decrypt(fileLocation, destination):
if os.path.exists(fileLocation):
file = open(fileLocation, "rb")
fileContents = file.read()
file.close()
btAr = bytearray(fileContents)
length = len(btAr)
n = 0
while n < length:
increment = 6
if 5 < btAr[n] <= 255:
btAr[n] = btAr[n] - increment
if btAr[n] <= 5:
btAr[n] = btAr[n] + 250
n = n + 1
decryptedFile = open(destination, "wb")
decryptedFile.write(btAr)
decryptedFile.close()
else:
print("File does not exist")
if __name__ == "__main__":
fileEncryptor.encrypt("D:\Python Projects\DesignerProject\ic.ico", "D:\Python Projects\DesignerProject\output\ic.ico")
fileEncryptor.decrypt("D:\Python Projects\DesignerProject\output\ic.ico", "D:\Python Projects\DesignerProject\output\i.ico")
I made a very simple encryption and decryption program to encrypt files by incrementing all bytes by 6. However, in testing, only text files work. If I use it to encrypt and decrypt photos, the result is not readable by the OS.
Code in Python:
import os.path
class fileEncryptor:
@staticmethod
def encrypt(fileLocation, destination):
if os.path.exists(fileLocation):
file = open(fileLocation, "rb")
fileContents = file.read() # fileContents is a byte string
file.close()
btAr = bytearray(fileContents) # Byte string needs to be changed to byte array to manipulate
length = len(btAr)
n = 0
while n < length:
increment = 6
if btAr[n] <= 249:
btAr[n] = btAr[n] + increment
if 249 < btAr[n] <= 255:
btAr[n] = btAr[n] - 250
n = n + 1
encryptedFile = open(destination, "wb")
encryptedFile.write(btAr)
encryptedFile.close()
else:
print("File does not exist")
@staticmethod
def decrypt(fileLocation, destination):
if os.path.exists(fileLocation):
file = open(fileLocation, "rb")
fileContents = file.read()
file.close()
btAr = bytearray(fileContents)
length = len(btAr)
n = 0
while n < length:
increment = 6
if 5 < btAr[n] <= 255:
btAr[n] = btAr[n] - increment
if btAr[n] <= 5:
btAr[n] = btAr[n] + 250
n = n + 1
decryptedFile = open(destination, "wb")
decryptedFile.write(btAr)
decryptedFile.close()
else:
print("File does not exist")
if __name__ == "__main__":
fileEncryptor.encrypt("D:\Python Projects\DesignerProject\ic.ico", "D:\Python Projects\DesignerProject\output\ic.ico")
fileEncryptor.decrypt("D:\Python Projects\DesignerProject\output\ic.ico", "D:\Python Projects\DesignerProject\output\i.ico")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该部分需要更改为
else
:像这样:
否则,如果第一个如果为true,则更改字节,第二个如果 可能会运行,将增量应用两倍。
解密相同。
This part needs to be changed to a
else
:Like this :
Otherwise, if the first
if
is true, the byte is changed and the secondif
might be runned, applying twice the increment.Same for the decryption.