Convert to binary and convert back to strings

Eric Pederson eric.pederson at gmail.com
Fri Feb 23 10:44:30 EST 2007


Harlin Seritt wrote:

>Hi...
>
>I would like to take a string like 'supercalifragilisticexpialidocius'
>and write it to a file in binary forms -- this way a user cannot read
>the string in case they were try to open in something like ascii text
>editor. I'd also like to be able to read the binary formed data back
>into string format so that it shows the original value. Is there any
>way to do this in Python?
>
>Thanks!
>
>Harlin
>
>  
>

To my mind, the more sensible job you do at programming this the worse 
off you are, unless you use strong encryption.  There are nearly 
infinite approaches, so the random approach you use will be part of the 
"security" of the obfuscation.

OK, I am not really taking this so seriously, but it is a fun question 
(Python makes these minor things fun).  Is there anyway to do this in 
Python?  You bet, so many ways... here's another:

s="""I would like to take a string like 'supercalifragilisticexpialidocius'
and write it to a file in binary forms -- this way a user cannot read
the string in case they were try to open in something like ascii text
editor. I'd also like to be able to read the binary formed data back
into string format so that it shows the original value. Is there any
way to do this in Python?"""

s0=s+"$"
s2="0 ".join([str(ord(c)) for c in s])
s1="".join([chr(int(i[:-1])) for i in s2.split(" 
")[:-1]])+chr(int(s2[-1]))[:-1]

def codeMe(s):
    s0=s+"$"
    return "0 ".join([str(ord(c)) for c in s0])

def uncodeMe(s):
    return "".join([chr(int(i[:-1])) for i in s.split(" 
")[:-1]])+chr(int(s[-1]))[:-1]

def testit(s):
    s2=codeMe(s)
    s1=uncodeMe(s2)
    strings={"original":s, "obfuscated":s2, "decoded":s1}
    for k in strings.keys():
        print k,":  ","\n",strings[k], "\n\n"
       
testit(s)

-------------
the obfuscated looks like this:

730 320 1190 1110 1170 1080 1000 320 1080 1050 1070 1010 320 1160 1110 
320 1160 970 1070 1010 320 970 320 1150 1160 1140 1050 1100 1030 320 
1080 1050 1070 1010 320 390 1150 1170 1120 1010 1140 990 970 1080 1050 
1020 1140 970 1030 1050 1080 1050 1150 1160 1050 990 1010 1200 1120 1050 
970 1080 1050 1000 1110 990 1050 1170 1150 390 100 970 1100 1000 320 
1190 1140 1050 1160 1010 320 1050 1160 320 1160 1110 320 970 320 1020 
1050 1080 1010 320 1050 1100 320 980 1050 1100 970 1140 1210 320 1020 
1110 1140 1090 1150 320 450 450 320 1160 1040 1050 1150 320 1190 970 
1210 320 970 320 1170 1150 1010 1140 320 990 970 1100 1100 1110 1160 320 
1140 1010 970 1000 100 1160 1040 1010 320 1150 1160 1140 1050 1100 1030 
320 1050 1100 320 990 970 1150 1010 320 1160 1040 1010 1210 320 1190 
1010 1140 1010 320 1160 1140 1210 320 1160 1110 320 1110 1120 1010 1100 
320 1050 1100 320 1150 1110 1090 1010 1160 1040 1050 1100 1030 320 1080 
1050 1070 1010 320 970 1150 990 1050 1050 320 1160 1010 1200 1160 100 
1010 1000 1050 1160 1110 1140 460 320 730 390 1000 320 970 1080 1150 
1110 320 1080 1050 1070 1010 320 1160 1110 320 980 1010 320 970 980 1080 
1010 320 1160 1110 320 1140 1010 970 1000 320 1160 1040 1010 320 980 
1050 1100 970 1140 1210 320 1020 1110 1140 1090 1010 1000 320 1000 970 
1160 970 320 980 970 990 1070 100 1050 1100 1160 1110 320 1150 1160 1140 
1050 1100 1030 320 1020 1110 1140 1090 970 1160 320 1150 1110 320 1160 
1040 970 1160 320 1050 1160 320 1150 1040 1110 1190 1150 320 1160 1040 
1010 320 1110 1140 1050 1030 1050 1100 970 1080 320 1180 970 1080 1170 
1010 460 320 730 1150 320 1160 1040 1010 1140 1010 320 970 1100 1210 100 
1190 970 1210 320 1160 1110 320 1000 1110 320 1160 1040 1050 1150 320 
1050 1100 320 800 1210 1160 1040 1110 1100 630 36

Of course some overly curious application user may note the pattern of 
"0" endings, strip those off, concatenate the numbers, and try several 
conversions on them, at which point they may figure this out-  and 
contact you to gloat that they have hacked the file.

That's when you recruit them onto your development team and give them 
some real work.  :-)

Have fun


EP




More information about the Python-list mailing list