【问题标题】:Python3 - Encrypting and decrypting an image (Fernet) IssuePython3 - 加密和解密图像(Fernet)问题
【发布时间】:2020-03-10 09:07:48
【问题描述】:

早安,

我正在做密码学作业。这是一项简单的任务,我需要拍摄任何图像,将其转换为 HEX,加密然后解密。

由于我在 Python 中工作,并且任务中没有特定的加密方法,所以我只使用了 Fernet。

我有一个加密器和解密器脚本。

加密似乎有效,因为作为测试,我使用原始 HEX 创建了一个 txt 文档,解密后程序指出原始 HEX 和解密后的文件相同,但未加载解密图像。

有人能帮帮新手吗?

加密器:

import binascii
from cryptography.fernet import Fernet

img = 'panda.png'
with open(img, 'rb') as f:
    content = f.read()
hexValue = binascii.hexlify(content)
key = Fernet.generate_key()

with open('info/key.txt', mode='w+') as keyValue:
    keyValue.write(key)
    keyValue.seek(0)

f = Fernet(key)
encHexVal = f.encrypt(hexValue) 

with open('info/encryptedHex.txt', mode='w+') as hexValueFile:
    hexValueFile.write(encHexVal)
    hexValueFile.seek(0)
a = f.decrypt(encHexVal)

with open('info/realValue.txt', mode='w+') as writeHex:
    originalHex = writeHex.write(hexValue)

with open('info/realValue.txt', mode='r') as reading:
    realValue = reading.read()
if realValue == a:
    print("We're good to go!")
else:
    print("Oops something went wrong. Check the source code.")

解密器:

import binascii
from cryptography.fernet import Fernet

with open('info/key.txt', mode='rb') as keyValue:
    key = keyValue.read()
    f = Fernet(key)
with open('info/encryptedHex.txt', mode='rb') as imageHexValue:
    hexValue = imageHexValue.read()
a = f.decrypt(hexValue)
with open('info/realValue.txt', mode='r') as compare:
    realContents = compare.read()

print("Small test in safe environment...")
if realContents == a:
    print("All good!")
else:
    print("Something is wrong...")
data = a.encode()
data = data.strip()
data = data.replace(' ', '')
data = data.replace('\n', '')
with open('newImage.png', 'wb') as file:
    file.write(data)

我正在使用来自功夫熊猫 Po 的互联网随机图像:

【问题讨论】:

  • Encrypter 中,open('info/key.txt', mode='w+') 错误...需要以二进制模式打开。
  • 是不是应该先加密再转HEX?换一种方式,你会得到二进制,而不是文本。
  • 你用的是哪个版本的python?代码中有几个字节/字符串问题。
  • @tdelaney 我通过写“wb+”来修复它,但图像仍然没有加载。我使用的是python 3。它应该首先将其转换为HEX,然后对HEX值本身进行加密。
  • 在这种情况下,'info/encryptedHex.txt' 是真正的二进制。核心问题是你没有在保存 newImage.png 之前 unhexlify 解密的数据。

标签: python image encryption cryptography


【解决方案1】:

主要问题是,尽管您在加密器中进行了 hexlify 然后加密,但在解密器中解密后您并没有 unhexlify。以另一种方式做事更为常见,先加密然后 hexlify,以便加密的二进制文件可以存储在常规文本文件中或通过 http 发送。

您在尝试将bytes 对象写入以文本打开的文件时遇到了几个问题。我在路上修好了那些。但这确实让我感到困惑,为什么名为 'info/encryptedHex.txt' 的文件会是二进制文件。

加密器

import binascii
from cryptography.fernet import Fernet

# Generate keyfile
#
# TODO: Overwrites key file on each run, invalidating previous
# saves. You could do `if not os.path.exists('info/key.txt'):`
key = Fernet.generate_key()
with open('info/key.txt', mode='wb') as keyValue:
    keyValue.write(key)

# Encrypt image
img = 'panda.png'
with open(img, 'rb') as f:
    content = f.read()
hexValue = binascii.hexlify(content)

f = Fernet(key)
encHexVal = f.encrypt(hexValue) 

with open('info/encryptedHex.txt', mode='wb') as hexValueFile:
    hexValueFile.write(encHexVal)

# Verification checks
a = f.decrypt(encHexVal)

# hexed bytes is same encoding as 'ascii'
with open('info/realValue.txt', mode='wb') as writeHex:
    originalHex = writeHex.write(hexValue)

with open('info/realValue.txt', mode='r', encoding='ascii') as reading:
    realValue = reading.read()
if realValue == a.decode('ascii'):
    print("We're good to go!")
else:
    print("Oops something went wrong. Check the source code.")

解密器

import binascii
from cryptography.fernet import Fernet

# Generate keyfile
#
# TODO: Overwrites key file on each run, invalidating previous
# saves. You could do `if not os.path.exists('info/key.txt'):`
key = Fernet.generate_key()
with open('info/key.txt', mode='wb') as keyValue:
    keyValue.write(key)

# Encrypt image
img = 'panda.png'
with open(img, 'rb') as f:
    content = f.read()
hexValue = binascii.hexlify(content)

f = Fernet(key)
encHexVal = f.encrypt(hexValue) 

with open('info/encryptedHex.txt', mode='wb') as hexValueFile:
    hexValueFile.write(encHexVal)

# Verification checks
a = f.decrypt(encHexVal)

# hexed bytes is same encoding as 'ascii'
with open('info/realValue.txt', mode='wb') as writeHex:
    originalHex = writeHex.write(hexValue)

with open('info/realValue.txt', mode='r', encoding='ascii') as reading:
    realValue = reading.read()
if realValue == a.decode('ascii'):
    print("We're good to go!")
else:
    print("Oops something went wrong. Check the source code.")
(base) td@timpad:~/dev/SO/Encrypting and decrypting in image$ cat de.py
import binascii
from cryptography.fernet import Fernet

with open('info/key.txt', mode='rb') as keyValue:
    key = keyValue.read()
    f = Fernet(key)
with open('info/encryptedHex.txt', mode='rb') as imageHexValue:
    encHexValue = imageHexValue.read()
hexValue = f.decrypt(encHexValue)
binValue = binascii.unhexlify(hexValue)

with open('info/realValue.txt', mode='rb') as compare:
    realContents = compare.read()

print("Small test in safe environment...")
if realContents == hexValue:
    print("All good!")
else:
    print("Something is wrong...")
with open('newImage.png', 'wb') as file:
    file.write(binValue)

【讨论】:

  • 非常感谢您再次详细回答。它是 hexlify ,我通过添加 test1 = binascii.unhexlify(data) 并将 unhexlified(?) 文本写入文件来修复它。再次感谢你!我是初学者,所以我总是犯一些愚蠢的错误。
猜你喜欢
  • 2021-02-15
  • 2021-10-06
  • 1970-01-01
  • 2018-09-10
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 2022-12-30
  • 1970-01-01
相关资源
最近更新 更多