How does bit-wise operation work for encrypting grayscale images?
In my Khan Academy course they encrypt an image with bitwise_and
, bitwise_or
and bitwise_xor
. I have tried, without success, to replicate their results.
The Khan Academy write-up: https://www.khanacademy.org/computing/computer-science/cryptography/ciphers/a/xor-and-the-one-time-pad
My results: https://i.stack.imgur.com/BD6jJ.jpg
The way I did the operation is this (Python code):
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
My second image is grayscale with values 0-255
, and my key is a matrix of the same dimensions as an image with random 1s and 0s.
Key is defined as: key = numpy.random.randint(0, 2, image.shape)
Why is the Khan Academy's result completely different from mine? How are grayscale images encrypted with bit-wise operations?