如何按位操作加密灰度图像?
在我的汗学院课程中,他们用bitwise_and、bitwise_or和bitwise_xor加密图像。我试图复制他们的成果,但没有成功。
我的结果:https://imgur.com/a/ltvj5os
我所做的操作是这样的(Python代码):
def get_bitwise_image(image, key, op):
and_image = []
row=0
col=0
while row < len(image):
new_row = []
col = 0
while col < len(image[row]):
# a bitwise and between say 243 and 1 is bitwise and between 2 intergers and not binaries. So, their constituent bits and and-ed together
# ex: 1 => 00000001 and 243 => 11110011
# 00000001
# 11110011
# --------
# 00000001 => 1
if op == 'and':
new_row.append(image[row, col] & key[row, col])
elif op == 'or':
new_row.append(image[row, col] | key[row, col])
else:
new_row.append(image[row, col] ^ key[row, col])
col += 1
and_image.append(new_row)
row+=1
return and_image我的第二个图像是带有值0-255的灰度图像,我的关键是一个与随机1s和0s图像相同维数的矩阵。
键被定义为:key = numpy.random.randint(0, 2, image.shape)
为什么汗学院的成绩和我的完全不同?如何用逐位操作加密灰度图像?
发布于 2019-07-01 22:19:15
我已经解决了。
主要原因是我应该使用逻辑操作而不是BITWISE操作。
新结果:https://imgur.com/a/NLa5Q8J
我的职能:
def logical_xor(message, key, op):
message = np.asarray(message)
key = np.asarray(key)
new_image = []
row=0
col=0
while row < len(message):
new_row = []
col = 0
while col < len(message[row]):
if op == 'and':
new_row.append(message[row, col] and key[row, col]) # and instead of &
elif op == 'or':
new_row.append(message[row, col] or key[row, col])
else:
new_row.append(message[row, col] != key[row, col]) # xor
col += 1
new_image.append(new_row)
row+=1
return new_image但是在使用这个函数之前,我必须这样做:
from PIL import Image
m = Image.open("test_image.jpg") # message
m = m.convert("1") # converts to boolean?https://crypto.stackexchange.com/questions/71677
复制相似问题