簡體   English   中英

Python3 - 加密和解密圖像(Fernet)問題

[英]Python3 - Encrypting and decrypting an image (Fernet) Issue

再會,

我正在做密碼學的作業。 這是一項簡單的任務,我需要拍攝任何圖像,將其轉換為 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 互聯網上的隨機圖像: 來自功夫熊貓的寶

主要問題是,盡管您先在加密器中進行 hexlify 然后在加密器中加密,但在解密器中解密后並沒有 unhexlify。 以另一種方式做事更常見,先加密然后十六進制化,以便加密的二進制文件可以存儲在常規文本文件中或通過 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM