2010-03-16 3 views
0

J'utilise XML comme backend pour l'application ...Chiffrement de la base de données XML en python

LXML est utilisé pour analyser le fichier XML.

Comment puis-je chiffrer ce fichier xml pour vous assurer que les données sont protégées ......

merci à l'avance. Comme XML contient une structure répétitive, il vaut mieux d'abord compress puis encrypt

+1

vous manque le R dans Encry pt. – Pretzel

+0

merci de pointer l'erreur – RSK

Répondre

4

Téléchargez et installez 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)) 

Il y a beaucoup de bibliothèques de cryptographie disponibles en python

  1. Pure-Python RSA implementation
  2. Python Encryption Examples
  3. PyXMLSec
  4. PyCrypto - The Python Cryptography Toolkit
+0

je pense hashlib est irréversible ... j'ai besoin d'un – RSK

+0

réversible après le décryptage, nous obtiendrons les données sous forme de chaîne. droite???? alors comment puis-je analyser cette chaîne xml avec la bibliothèque lxml ???? – RSK

Questions connexes