XOR Encryption?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darktemp
    New Member
    • Feb 2010
    • 25

    XOR Encryption?

    Is there a simple method that can allow for xor encryption on python? What I want to do is pass a file into the method along with the key or integrate the key into the method.

    I have seen the ones on google, but they don't really work. The long one only accepts certain file types, and the second one doesn't work at all.

    I appreciate all the help!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Here's my first attempt at XOR encryption:
    Code:
    def encryptXOR(s, key="\x101Z"):
        output = ""
        for character in s:
            for letter in key:
                character = chr(ord(character) ^ ord(letter))
            output += character
        return output
    
    def decryptXOR(s, key="\x101Z"):
        output = ""
        for character in s:
            for letter in key[::-1]:
                character = chr(ord(character) ^ ord(letter))
            output += character
        return output
    
    print repr(encryptXOR('Try to encrypt this message using XOR encryption.'))
    print
    print repr(decryptXOR(encryptXOR('Try to encrypt this message using XOR encryption.')))
    Output:
    Code:
    >>> '/\t\x02[\x0f\x14[\x1e\x15\x18\t\x02\x0b\x0f[\x0f\x13\x12\x08[\x16\x1e\x08\x08\x1a\x1c\x1e[\x0e\x08\x12\x15\x1c[#4)[\x1e\x15\x18\t\x02\x0b\x0f\x12\x14\x15U'
    
    'Try to encrypt this message using XOR encryption.'
    >>>

    Comment

    • darktemp
      New Member
      • Feb 2010
      • 25

      #3
      I managed to find a method for xoring which is very simple. bvdet's code is also good, but since xoring is the same method for encrypting and decrypting, it would be simpler to keep encryption and decryption as the same method. I'll just finish and contribute with the code I found.

      Code:
      def xor_crypt_string(data, key):
          return ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))
      So to call this:
      Code:
      decryptedfile = xor_crypt_string(my_data, my_key)
      Thanks for your help bvdet :].

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Actually, I made a false assumption when I assumed that I had to reverse the order of the key. The same function works both ways. Here's the result:
        Code:
        def cryptXOR(s, key="\x1027"):
            output = ""
            for character in s:
                for letter in key:
                    character = chr(ord(character) ^ ord(letter))
                output += character
            return output
        
        s = cryptXOR('Try to encrypt this message using XOR encryption.')
        print repr(s)
        print repr(cryptXOR(s))
        Output:
        Code:
        >>> 'Agl5az5p{vglea5a}|f5xpfftrp5`f|{r5MZG5p{vglea|z{;'
        'Try to encrypt this message using XOR encryption.'
        The code you found uses itertools functions. Function cycle() cycles through the letters in the key and XORs letter pairs until the text is exhausted. This may be a better solution because there are no matching text patterns to the original string.

        Comment

        • hard123
          New Member
          • May 2010
          • 2

          #5
          Originally posted by bvdet
          Actually, I made a false assumption when I assumed that I had to reverse the order of the key. The same function works both ways. Here's the result:
          Code:
          def cryptXOR(s, key="\x1027"):
              output = ""
              for character in s:
                  for letter in key:
                      character = chr(ord(character) ^ ord(letter))
                  output += character
              return output
          
          s = cryptXOR('Try to encrypt this message using XOR encryption.')
          print repr(s)
          print repr(cryptXOR(s))
          Output:
          Code:
          >>> 'Agl5az5p{vglea5a}|f5xpfftrp5`f|{r5MZG5p{vglea|z{;'
          'Try to encrypt this message using XOR encryption.'
          The code you found uses itertools functions. Function cycle() cycles through the letters in the key and XORs letter pairs until the text is exhausted. This may be a better solution because there are no matching text patterns to the original string.
          hi,

          any one help me bite wise encryption in xor .

          Comment

          • hard123
            New Member
            • May 2010
            • 2

            #6
            hi...

            when i using the file for xor encryption





            Originally posted by bvdet
            Actually, I made a false assumption when I assumed that I had to reverse the order of the key. The same function works both ways. Here's the result:
            Code:
            def cryptXOR(s, key="\x1027"):
                output = ""
                for character in s:
                    for letter in key:
                        character = chr(ord(character) ^ ord(letter))
                    output += character
                return output
            
            s = cryptXOR('Try to encrypt this message using XOR encryption.')
            print repr(s)
            print repr(cryptXOR(s))
            Output:
            Code:
            >>> 'Agl5az5p{vglea5a}|f5xpfftrp5`f|{r5MZG5p{vglea|z{;'
            'Try to encrypt this message using XOR encryption.'
            The code you found uses itertools functions. Function cycle() cycles through the letters in the key and XORs letter pairs until the text is exhausted. This may be a better solution because there are no matching text patterns to the original string.
            hi,

            any one help me bite wise encryption in xor .

            Comment

            Working...