0

I'm looking for simple password-based obfuscation/security of strings. I've pretty much gone over each example of > Simple way to encode a string according to a password? And none of them work with my python 3.7. I got the error with ord() so I updated the code, but even after, its still broken. For examle:

from itertools import cycle

def encode_zip_cycle(key, clear):
    enc = [chr((ord(clear_char) + ord(key_char)) % 256)
           for clear_char, key_char in zip(clear, cycle(key))]
    return base64.urlsafe_b64encode("".join(enc).encode())

def decode_zip_cycle(key, enc):
    enc = base64.urlsafe_b64decode(enc)
    dec = [chr((256 + enc_char - ord(key_char)) % 256)
           for enc_char, key_char in zip(enc, cycle(key))]
    print(dec)
    return "".join(dec)

text = "ATTACKATONCEfor Live   2154125-21-512^!££613-123!"
s = "1235348udgfjff"
print("Text  : " + text)
print("Shift : " + str(s))
print("Cipher: ", encode_zip_cycle(s, text))  # , type(encode(s, text)))
print("Original text: ", decode_zip_cycle(s, encode_zip_cycle(s, text)))

Gives me

Text  : ATTACKATONCEfor Live   2154125-21-512^!££613-123!
Shift : 1235348udgfjff
Cipher:  b'csKGwod2dn95w4nCs8K1wqnCr8OMw5XCo1J_wp7CqcKZWMKVwoTCmcKXwp_CmsKXY2dgZ2RhbcKmwpbDhcKHDQnCnGJlYGZlZ1k='
['A', '\x90', 'S', '\x8d', 'T', 'B', '>', '\n', '\x15', '\\', '#', 'X', 'M', '\\', '\x84', '\x90', 'v', '\x8d', '|', '\x8f', 'T', 'N', '1', '[', '=', 'è', '\x19', '\\', 'm', '\x90', 'v', '\x8d', 'f', '$', '\x8a', ' ', '^', '\x1d', '\\', '/', '\\', '1', '\x91', 'm', '\x8f', 'e', '\x8f', 'c', '+', 'ò', 'ü', '\x00', 'þ', '÷', '\x07', '\\', 'u', '\x90', 'c', '\x8e', 'R', '\x8e', 'O', '\x98', '¥', '[', '6', 'ø', 'ÿ', 'ú', '5', '3', '4', '$']
Original text:  ASTB>
\#XM\v|TN1[=è\mvf$ ^\/\1mec+òü þ÷\ucRO¥[6øÿú534$
Dariusz
  • 960
  • 13
  • 36
  • What do you mean by Python only? What's wrong with actually encrypting the data? – Cow Aug 10 '21 at 11:11
  • I can only use libraries that come with python 3.7. No custom imports. – Dariusz Aug 10 '21 at 11:18
  • Read these answers and comments for differences in python3 https://stackoverflow.com/q/6224052/1216776. You probably need to use byte strings. – stark Aug 10 '21 at 12:22

1 Answers1

0

In encode_zip_cycle you encode the "encrypted" string into utf-8 before doing the second encoding into base64. Yet, you don't revert this operation later in decode_zip_cycle.

This is the correct decode_zip_cycle function:

def decode_zip_cycle(key, enc):
    enc = base64.urlsafe_b64decode(enc).decode()
    dec = [chr((256 + ord(enc_char) - ord(key_char)) % 256)
           for enc_char, key_char in zip(enc, cycle(key))]
    print(dec)
    return "".join(dec)
mrosa
  • 33
  • 5