我使用XML作为应用程序的后端......
LXML用于解析xml。
如何加密此xml文件以确保数据受到保护......
提前感谢。
答案 0 :(得分:4)
由于XML包含重复结构,最好先compress
然后encrypt
。
下载并安装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))
python中有很多加密库