首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python3 -加密和解密图像(Fernet)问题

Python3 -加密和解密图像(Fernet)问题
EN

Stack Overflow用户
提问于 2020-03-10 09:07:48
回答 1查看 1.1K关注 0票数 0

日安,

我在做密码学的作业。这是一个简单的任务,我需要拍摄任何图像,把它变成HEX,加密它,然后解密它。

由于我在Python中工作,任务中没有具体的加密方法,所以我只使用Fernet。

我有加密和解密脚本。

加密似乎是有效的,因为作为测试,我用原始HEX创建了一个txt文档,在解密之后,程序声明原来的HEX和解密的HEX是相同的,但是解密后的图像没有加载。

有人能帮个新手吗?

加密器:

代码语言:javascript
复制
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.")

解密器:

代码语言:javascript
复制
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)

我正在使用“功夫熊猫”网上的一张随机图片:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-10 10:18:22

主要的问题是,虽然您已经加密,然后在加密器中加密,但在解密器中解密后,您不会解除加密。更常见的做法是用另一种方式加密,然后加密,这样加密的二进制文件就可以存储在常规文本文件中,或者通过http发送。

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

加密器

代码语言:javascript
复制
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.")

解密器

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60614399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档