1
$\begingroup$

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?

$\endgroup$
9
  • $\begingroup$ Well, your original images are different, and so the scan rasters might be different in the low bits. Please repeat with Abe... $\endgroup$
    – Paul Uszak
    Jul 1, 2019 at 0:15
  • 1
    $\begingroup$ @MaartenBodewes I believe the problem is with the key selection which only randomizes the least significant bit, so I'm not quite sure why you say it cannot be answered (I believe the OP asking why his result is different is an euphemism for asking what's wrong with his implementation), and unless I'm mistaken the key selection is a cryptography concern. $\endgroup$
    – SleuthEye
    Jul 1, 2019 at 17:12
  • 1
    $\begingroup$ @SleuthEye Sure, but implementation related issues are not on topic, I don't see how this question can be beneficial to anybody studying crypto in general. If you disagree with that, I'm fine with reopening the question on any good argument for it. $\endgroup$
    – Maarten Bodewes
    Jul 1, 2019 at 21:17
  • 1
    $\begingroup$ @MaartenBodewes posting the answer would only do good. Though the solution is implementation specific. It's good to know difference between bitwise and logical. My name is indian, thanks. $\endgroup$ Jul 1, 2019 at 21:36
  • 1
    $\begingroup$ That's a seriously good name for a programmer, you can thank your parents for that one :) $\endgroup$
    – Maarten Bodewes
    Jul 1, 2019 at 21:52

1 Answer 1

0
$\begingroup$

I have solved it.

The main reason was that I'm supposed to use LOGICAL operations instead of BITWISE operations.

The new results: https://i.stack.imgur.com/7zQNv.jpg

my function:

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

but before using this function, I had to do this:

from PIL import Image
m = Image.open("test_image.jpg") # message
m = m.convert("1") # converts to boolean?
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.