2016-03-31 33 views

回答

0

這個工作需要多個工具。

要閱讀XML文件並生成節點樹,我推薦使用Python的ElementTree API。它內置且易於使用。

走樹並找到要加密的文本。 PyCrypto圖書館可以幫助你。選擇一個適合你的要求的密碼,或者如果你不確定,選擇AES。谷歌發現this example,複製如下:

from Crypto.Cipher import AES 
import base64 
import os 

# the block size for the cipher object; must be 16, 24, or 32 for AES 
BLOCK_SIZE = 32 

# the character used for padding--with a block cipher such as AES, the value 
# you encrypt must be a multiple of BLOCK_SIZE in length. This character is 
# used to ensure that your value is always a multiple of BLOCK_SIZE 
PADDING = '{' 

# one-liner to sufficiently pad the text to be encrypted 
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING 

# one-liners to encrypt/encode and decrypt/decode a string 
# encrypt with AES, encode with base64 
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s))) 
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING) 

# generate a random secret key 
secret = os.urandom(BLOCK_SIZE) 

# create a cipher object using the random secret 
cipher = AES.new(secret) 

# encode a string 
encoded = EncodeAES(cipher, 'password') 
print 'Encrypted string:', encoded 

# decode the encoded string 
decoded = DecodeAES(cipher, encoded) 
+0

謝謝。我打算做的是根據xml節點的標籤名稱進行加密。我已經能夠解析XML文件並使用ElementTree生成標籤名稱列表。面臨的挑戰是對標籤進行加密,並且仍然以XML形式存在文件,但其內容無法訪問,直到解密。如果問題看起來很愚蠢,對所有這些都有點新東西:) – chisky

+0

您可以使用上述技術加密標籤名稱或內容。攻擊問題,如果您需要進一步幫助,請提出另一個更具體的問題。事實上,這個問題已經得到解答 - 所剩下的只是一些閱讀,試驗和錯誤。根據規則和習慣,您無法獲得完整的代碼,無法在此網站上完成所有操作。至少給半小時,你會學習10倍的:) – slezica

相關問題