0

first of all I'm reasonably new to Python. I'll try to explain this as best I can! I'm currently making an Encryption/Decryption program which encrypts and decrpyts a file, everything works fine, no errors given. Only when I check the newly decrypted file to the original in a Hex editor, very few but some bytes have not been decrpyted correctly. The only places I can see this happening is with any 'FF' byte is now a '00' byte in the decrypted file. I can't work out why this is happening, code is below:

def shift_byte(x):
    if(ord(x) == 0):
        return (ord(x))
    elif(ord(x) == shiftrange):
        v1 = (ord(x) + shift)
        return v1
    else:       
        return ((ord(x) + shift) % 255)

That is the encryption, just a simple shift cipher, this is the decryption from another file:

def shift_byte(x):
    if(x == 0):
        return (x)
    elif(x == shiftrange):
        v1 = x - shift
        return v1
    else:       
        return ((x - shift) % 255)

My only thoughts are that it is something to do with this line:

return ((x - shift) % 255)

Is there something wrong here? Thanks.

4
  • How can ord(x) be a range? Shouldn't ord(x) be within a range? Why not always use the modulus operator? Commented Jan 11, 2015 at 16:15
  • That's just bad variable naming by me, shiftrange isn't actually a range, its just an integer. Commented Jan 11, 2015 at 16:28
  • I have included the modulus on each statement now and again it has improved slightly, but still some random bits are not as they should be in the decrypted file Commented Jan 11, 2015 at 18:04
  • I've just found that the byte that is affected by this issue depends on the shift no. being used, the problem may lie somewhere else Commented Jan 11, 2015 at 18:07

2 Answers 2

1

It has to be:

return ((x + shift) % 256)

and

return ((x - shift) % 256)

With % 255 your values only range from 0 to 254(0x00 to 0xFE) and not from 0 to 255(0x00 to 0xFF).

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! Not sure how I missed that, works fine now!
Actually hasn't fully worked! I'm now getting the same issue but with different bytes, the decrypted image can now be open, but the image is a bit messed up!
This is incorrect. The decryption has to 'undo' the encryption, no matter if %256 or %255 is used. If %255 is performed on encryption then that's what is also needed on decryption.
Yeah I have changed %255 to %256 on both and I am now able to open the decrypted image, only the image is a bit scattered
Thaats's not correct Greg. If you use %255 the input can only be between 0 and 254. Here the input ranges from 0x00 to 0xFF, so from 0 to 255 which means it has to be % 256.
0

I really dont get why you use this structure:

if(x == 0):
    return (x)
elif(x == shiftrange):
    v1 = x - shift
    return v1
else:       
    return ((x - shift) % 256)

why don't you only return ((x-shift) % 256) ? If i understand this correctly you do not encrypt or decrypt 00 right? because if x == 0 you just return x. In this case it wont be "encrypted".

For me this is a problem, because is you encrypt F with a shift of 1, it will be 0. When calling the decryption function 0 will not be shifted back to F, it will just return 0.

Just use:

def shift_byte(x):       
    return ((x - shift) % 256)

Does this help?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.