簡體   English   中英

當鍵是字典中的哈希字符串時,如何 append 值?

[英]How to append values when the key is a hashed string in dictionary?

encrypted_index 的類型是字典,現在我想在其中輸入 append 值。

     def update(self, keyword, text):
        encrypted_index=dict()
        with open('encrypted_index', "rb") as f:
            encrypted_index = pickle.load(f)
        with open('keys', "rb") as f:
            keys = pickle.load(f)
        #initial keys and iv
        keyword_sk = keys[0]
        doc_sk = keys[1]
        iv = keys[2]

        #encrypt the text and keyword
        enc_text = self.aesEncrypt(doc_sk, text, iv)
        encrypted_search_keyword = self.encrypt_keyword(keyword_sk, bytes(keyword,'utf-8'))

        for enc_keyword in encrypted_index:
            if(encrypted_search_keyword == enc_keyword):
                encrypted_index[enc_keyword].append(enc_text)
            print(encrypted_index)
File "c:/Users/76998/Downloads/SSE/Client.py", line 85, in update
    encrypted_index[enc_keyword].append(enc_text)
AttributeError: 'bytes' object has no attribute 'append'

字典的鍵是使用 hmac 散列的,所以我不能 append 值。 當密鑰被散列時,是否可以 append 值?

encrypted_index 看起來像這樣

{b'E\x9e\x06\x8f\xca\xb6\x8b\x95\xb0.5\xc2\xc4\xce\xd9\xe1\xefB\xcc\x0b\xe7Y\xec\xbc\x1b\x04\xde\x15\x06+R\xbf': b'>\xb6}\x0e\xc9[\x17VS\xa3h\x9aC\xb9?\x0c\xbap\x99\xc4`\xed\xdb0*\x17\x81\x90H^\xc6S'}

append()用於將元素添加到列表中。 由於該值是一個字符串,因此您需要與+運算符連接。

encrypted_index[enc_keyword] += enc_text

也不需要for循環。 寫吧:

if enc_keyword in encrypted_index:
    encrypted_index[enc_keyword] += enc_text
    print(encrypted_index)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM