I am trying to run code from the answer in this post (which works perfectly with python3 version 3.5.3) with python2 version 2.7.13:
def myencode_str(ori_str, key):
enc = []
for i in range(len(ori_str)):
key_c = key[i % len(key)]
enc_c = (ord(ori_str[i]) + ord(key_c)) % 256
enc.append(enc_c)
return (base64.urlsafe_b64encode(bytes(enc))).decode("utf-8")
I am using following decode fn:
def mydecode(enc_str, key):
dec = []
enc_str = base64.urlsafe_b64decode(enc_str)
for i in range(len(enc_str)):
key_c = key[i % len(key)]
dec_c = chr((256 + enc_str[i] - ord(key_c)) % 256)
dec.append(dec_c)
return "".join(dec)
But I get following error message:
dec_c = chr((256 + enc_str[i] - ord(key_c)) % 256)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
I tried code with following changes but they also do not work:
dec_c = chr((256 + int(enc_str[i]) - int(ord(key_c))) % 256)
ValueError: invalid literal for int() with base 10: '\xc3'
Where is the problem and how can it be solved?