codenoob
codenoob

Reputation: 11

Python XOR Decryption Only Decrypting Half of RGB Image

I am trying to XOR decrypt an encrypted image in Python using a provided key. I have been able to decrypt half of the image, and I can't understand why the bottom half does not get decrypted as well.

key = np.load('key.npy')
secret = plt.imread('secret.bmp')

newArr = secret.copy()

for t, k in zip(secret, key):
    e = t^k
    newArr[t] = e

plt.imshow(newArr)

Is there something wrong with my loop that makes the decryption stop after only traversing half the image rows?

enter image description here

Upvotes: 1

Views: 1494

Answers (2)

MEE
MEE

Reputation: 2412

You are writing to newArr[t] but t is the secret byte value not an index. You should replace the for loop entirely with a logical-xor on the contents of secret and key (assuming the key and the secret arrays/matrices are broadcastable to the same shape; read more about broadcasting here):

key = np.load('key.npy')
secret = plt.imread('secret.bmp')

newArr = np.logical_xor(key, secret)
plt.imshow(newArr)

Upvotes: 2

dan04
dan04

Reputation: 91179

I assume that you have len(key) < len(secret).

Python's zip function will stop when it reaches the end of the shorter sequence, so if the key is too short, your problem will not decrypt the leftover data when it reaches the end of key.

Upvotes: 0

Related Questions