I´m learning my way thru data types in Python, and it´s being not so easy :)
I´m trying to setup a very simple save/restore XOR´ed data sample script, but I can´t retrieve the original data. I saved it as string, once "encrypted", for easy of reading.
I´ve tried decoding the result of the retrieve function, but a str variable can´t be decoded.
This is my sample code:
mykey = 'SOME%$(=084RANDOM'
# generates encrypted value. s1 is target, s2 is key
def save_xor(s1, s2):
j = "".join([chr(ord(c1) ^ ord(c2)) for (c1,c2) in zip(s1,s2)])
return str(j.encode()) # returns in readable format
# recovers clean value from encryted and key
def retrieve_xor(s1, s2):
j = "".join([chr(ord(c1) ^ ord(c2)) for (c1,c2) in zip(s1,s2)])
return j
myVal = "aBcD123"
toSave = save_xor(myVal,mykey)
print("Saving value",myVal," as ",toSave)
f = open("data.ini","w")
f.write(toSave)
f.close()
r = open("data.ini","r")
toRead = r.read()
r.close()
sameVal = retrieve_xor(toRead,mykey)
print("Retrieving value saved as ",toRead," and now is ",sameVal)
and this is my output:
Saving value aBcD123 as b'2\r.\x01\x14\x16\x1b'
Retrieving value saved as b'2\r.\x01\x14\x16\x1b' and now is 1W tE h*pz7|
What´s the wrong part of it? Yes, I know it´s a very basic security scheme. It´s just the base of it :)
Thanks!