Welcome, guest | Sign In | My Account | Store | Cart

Recipe which can be conveniently used to obfuscate a string of data using simple xor without using any key.

Python, 93 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class Obfuscator:
	""" A simple obfuscator class using repeated xor """

    def __init__(self, data):

        self._string = data

    def obfuscate(self):
        """Obfuscate a string by using repeated xor"""

        out = ""
        
        data = self._string
        
        a0=ord(data[0])
        a1=ord(data[1])
        
        e0=chr(a0^a1)
        out += e0
        
        x=1
        eprev=e0
        while x<len(data):
            ax=ord(data[x])
            ex=chr(ax^ord(eprev))
            out += ex
            #throw some chaff
            chaff = chr(ord(ex)^ax)
            out += chaff
            eprev = ex
            x+=1

        return out
        
    def unobfuscate(self):
        """ Reverse of obfuscation """

        out = ""
        data = self._string
        
        x=len(data) - 2
        
        while x>1:
            apos=data[x]
            aprevpos=data[x-2]

            epos=chr(ord(apos)^ord(aprevpos))
            out += epos
        
            x -= 2

        #reverse string
        out2=""
        x=len(out)-1
        while x>=0:
            out2 += out[x]
            x -= 1

        out=out2
    
        #second character
        e2=data[2]
        a2=data[1]
        
        a1=chr(ord(a2)^ord(e2))
        a1 += out
        out = a1
    
        #first character
        e1=out[0]
        a1=data[0]
        
        a0=chr(ord(a1)^ord(e1))
        a0 += out
        out = a0
        
        return out

def main():
   
    testString="Python obfuscator"
    obfuscator = Obfuscator(testString)
    testStringObf = obfuscator.obfuscate()
    print testStringObf

    obfuscator = Obfuscator(testStringObf)
    testString = obfuscator.unobfuscate()
    
    print testString
    

if __name__=="__main__":
    main()

Encrytion/obfuscation of data is often needed even for the most trivial applications. Some times the programmer wants to obfuscate the data without doing actual encryption for purposes of visibility. This is one such function using simple xor repeatedly. There are many alternate implementations. There are no issues with the recipe because it does not require any key to work.

1 comment

Magnus Lyckå 21 years ago  # | flag

Python already has ROT-13! I think we can agree than this is no encryption to rely on for serious use (even if Microsoft pretends that). For hiding data in a very simple way (corresponding to placing a letter in an envelope) good old rot-13 might be simpler. It's certainly less code! :)

>>> "Hello There".encode('rot13')
'Uryyb Gurer'
>>> 'Uryyb Gurer'.encode('rot13')
'Hello There'
>>>
<pre>

</pre>

Created by Anand on Sun, 16 Mar 2003 (PSF)
Python recipes (4591)
Anand's recipes (38)

Required Modules

  • (none specified)

Other Information and Tasks