【发布时间】:2017-02-17 14:26:48
【问题描述】:
#encrypting an image using AES
import binascii
from Crypto.Cipher import AES
def pad(s):
return s + b"\0" * (AES.block_size - len(s) % AES.block_size)
filename = 'path to input_image.jpg'
with open(filename, 'rb') as f:
content = f.read()
#converting the jpg to hex, trimming whitespaces and padding.
content = binascii.hexlify(content)
binascii.a2b_hex(content.replace(' ', ''))
content = pad(content)
#16 byte key and IV
#thank you stackoverflow.com
obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
ciphertext = obj.encrypt(content)
#is it right to try and convert the garbled text to hex?
ciphertext = binascii.hexlify(ciphertext)
print ciphertext
#decryption
obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
plaintext = obj2.decrypt(ciphertext)
#content = content.encode('utf-8')
print plaintext
#the plaintext here matches the original hex input file as it should
with open('path to - AESimageDecrypted.txt', 'wb') as g:
g.write(plaintext)
我的问题有两个, 1)我将如何将基本上是十六进制字符串的文本文件的加密文件(在hexlify之前是乱码)转换回图像? 我希望输出在任何查看器上都可以作为 jpg 格式查看。
我已经尝试了一些东西,遇到了 Pillow,但我似乎无法掌握它是否可以做我想做的事。
任何帮助将不胜感激。
PS:我也想用其他密码试试这个。所以我认为如果有人能帮助弄清楚这种理解是否正确,将会很有帮助:
jpg -> 转换为二进制/十六进制 -> 加密 -> 乱码输出 -> 转换为 bin/hex -> 转换为 jpg
2) 以上可能吗?应该将它们转换为十六进制还是二进制?
【问题讨论】:
-
要恢复hexlify + encrypt,应用decrypt + unhexlify?
-
@zvone 有没有办法 hexlify + 加密 -> 转换为 jpg ?为了更清楚,我想知道是否有办法获取乱码密文并将其转换为图像?
-
当然有办法,但是我还是不明白是什么问题。首先,你为什么将 jpg 转换为十六进制?其次,除了将十六进制转换为原始格式之外,您似乎拥有一切。你在这里:stackoverflow.com/q/9641440/389289
-
@zvone 我试图实现的目标是我有一张我想用 AES 加密的普通照片。我希望得到的密文也能够像照片一样打开。这张密码照片(为了更好的术语)稍后应该能够解密为原始图像。我想我正在转换为十六进制,试图在发帖之前为自己解决这个问题……我被密文难住了。它基本上是不可读的符号。那么如何在不解密的情况下将这样的 txt 文件转换为 jpg(或任何照片格式)(我希望图像只是噪音)
-
我想如果你只做
ciphertext = obj.encrypt(content)和content = obj2.decrypt(ciphertext)没有任何填充、编码、hexlifying 等,那应该可以工作。
标签: python cryptography pillow pycrypto