I want to decrypt an Image but I don't know how I should read the file

Question:

I have an image "1.jpg.enc", I know how to decrypt it in Javascript but when I try to do it on Python I keep getting error of "io".
here’s my Javascript code

function _decryptImage( encrypted, key ) {
    // create a view for the buffer
    let decrypted = new Uint8Array( encrypted );
    for( let index in decrypted ) {
        decrypted[index] ^= key[index % key.length];
    }
    return decrypted;
}

I tried to make an equivalent for python

decrypted =  open("1.jpg.enc",'r')
new = np.uint8(decrypted)
for index in new:
    new[index] ^= key[index % len(key)] //Different kinds of error are encountered here according to the file options 'r','rb', etc.
//for 'rb' - TypeError: int() argument must be a string, a bytes-like object or a real number, not '_io.BufferedReader'
//for 'r' - TypeError: int() argument must be a string, a bytes-like object or a real number, not '_io.TextIOWrapper'        
with open("img.jpg",'wb') as writable:
    writable.write(new)

The Javascript code is working fine and gave me the image, but in the python one I keep getting errors related to "io". please help me.

also tried making np array of uint8 type but same errors

Asked By: Satyam

||

Answers:

You’re opening the file to read characters, and then you’re iterating over it, which causes it to read it line by line.

with open("1.jpg.enc", "rb") as file
    new = np.frombuffer(file.read(), dtype=np.uint8)

...
Answered By: Frank Yellin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.