我从文件映像中读取二进制文件,每个128字节并使用rsa加密。但是加密后我不能从二进制文件创建图像。我创建了函数generate key rsa。我把二进制转换成int进行加密
from PIL import Image
import KeyRSA
import RsaMath
pub, piv = KeyRSA.GenerateRsaKey(size=1024)
n, e = pub
with open("hinh.png", "rb") as infile:
data = infile.read()
step = 128
cipher_data = []
for i in range(0, len(data), step):
value = data[i:i+step]
m = RsaMath.OS2IP(value)
c = pow(m, e, n)
cipher_data.append(RsaMath.I2OSP(c, 128))
cipher_data = "".join(cipher_data)
im = Image.open("hinh.png")
W, H = im.size
img = Image.frombytes("RGB", (W,H), cipher_data)
img.show()e here
因为我不能访问KeyRSA和RsaMath,所以我决定使用pycrypto包。原始RSA加密是不安全的,而且速度很慢,所以这段代码使用AES,这是一种快速的,在128字节块的块模式下使用非常安全。在
下面的代码不是从文件中读取图像,而是生成一个简单的双色图像。然后对其进行加密,将加密的数据转换为图像,并将其显示并保存为PNG。然后解密加密的数据以恢复原始图像。在
加密过程是使用128字节块执行的,因此,如果您真的想这样做,您可以轻松地将其调整为适合您的RSA加密算法。但是,在一个步骤中执行加密更为有效;我添加了一个注释行,说明了如何进行加密。在
给你红蓝.png还有红蓝_埃斯.png在
相关问题 更多 >
编程相关推荐