为什么不能使用增量加密并解密照片文件?

发布于 2025-01-29 16:51:20 字数 2049 浏览 2 评论 0原文

我制作了一个非常简单的加密和解密程序来通过将所有字节增加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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆之渊 2025-02-05 16:51:21

该部分需要更改为else

if btAr[n] <= 249:
    btAr[n] = btAr[n] + increment
if 249 < btAr[n] <= 255:
    btAr[n] = btAr[n] - 250

像这样:

if btAr[n] <= 249:
    btAr[n] = btAr[n] + increment
else:
    btAr[n] = btAr[n] - 250

否则,如果第一个如果为true,则更改字节,第二个如果 可能会运行,将增量应用两倍。

解密相同。

This part needs to be changed to a else :

if btAr[n] <= 249:
    btAr[n] = btAr[n] + increment
if 249 < btAr[n] <= 255:
    btAr[n] = btAr[n] - 250

Like this :

if btAr[n] <= 249:
    btAr[n] = btAr[n] + increment
else:
    btAr[n] = btAr[n] - 250

Otherwise, if the first if is true, the byte is changed and the second if might be runned, applying twice the increment.

Same for the decryption.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文