0

I am new in this field, Would you help me ? Each byte in my cipher text (encrypt3) is interpret as an integer between 0 to 255.For each byte Plaintext=Ciphertext -100 (mod 256). In the following code I have error: newbyte=c.to_bytes(1,byteorder=sys.byteorder,signed=False) OverflowError: can't convert negative int to unsigned Any help would be appreciated.

import sys
f=open("encrypt3.dat","rb")
s=f.read(100)
d=bytearray(s)
print (len(d))
strnew=''
newbyte=b''
for c in d :
    c=c-100 % 256
    newbyte=c.to_bytes(1,byteorder=sys.byteorder,signed=True)
    strnew=strnew + newbyte.decode('latin1')

print(strnew)
1
  • You are in effect using a byte-based Caesar Cipher. To move on from a Caesar Cipher, have a look at Vigenère or perhaps RC4 for your next cipher exploration. You did well to go for a byte-based implementation rather than an alphabet based implementation. Commented Dec 13, 2016 at 11:49

1 Answer 1

0

The modulus operator (%) is processed before the subtraction operator (-), so your statement c=c-100 % 256 is equivalent to to c=c-(100 % 256), or just c=c-100 (because 100 % 256 == 100). As a result, your code will crash whenever it encounters a character with an ASCII code less than 100 (='d').

You can fix this by changing c=c-100 % 256 to c=(c-100) % 256.

Read up on operator precedence in Python 3.

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

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.