views:

134

answers:

1

i am using XML as my backend for the application...

LXML is used to parse the xml.

How can i encrypt this xml file to make sure that the data is protected......

thanks in advance.

+2  A: 

As XML contains repetitive structure it is better to first compress and then encrypt it.

Download and install PyDes.

from pyDes import *
import bz2

def encrypt(data,password):
    k = des(password, CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
    d = k.encrypt(data)
    return d

def decrypt(data,password):
    k = des(password, CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
    d = k.decrypt(data)
    return d

password = "eight222" # password length should be 8

data = '''
<?xml version="1.0"?>
  <library>
   <shelf id="fiction">
    <book>
     <title>Of Mice and Men</title>
     <author>John Steinbeck</author>
    </book>
    <book>
     <title>Harry Potter and the Philosopher's Stone</title>
     <author>J.K. Rowling</author>
    </book>
   </shelf>
  </library> 
'''

print len(data)

compressed_data = bz2.compress(data)
print len(compressed_data)

encrypted_data = encrypt(compressed_data,password)

print "%r"%encrypted_data

uncompressed_encrypted_data = encrypt(data,password)

print len(encrypted_data)
print len(uncompressed_encrypted_data)
print bz2.decompress(decrypt(encrypted_data,password))

There are lots of cryptography libraries available in python

  1. Pure-Python RSA implementation
  2. Python Encryption Examples
  3. PyXMLSec
  4. PyCrypto - The Python Cryptography Toolkit
TheMachineCharmer
i think hashlib is irreversible...i need a reversible one
RSK
after decrypting we will get the data as a string. right????then how can i parse that string xml with lxml library????
RSK