简体   繁体   中英

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

The type of encrypted_index is dictionary, and now I want to append values in it.

     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'

The key of the dictionary is hashed by using hmac, so I can't append values. Is it possible to append values when the key is hashed?

encrypted_index looks like that

{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() is for adding an element to a list. Since the value is a string, you need to concatenate with the + operator.

encrypted_index[enc_keyword] += enc_text

There's also no need for the for loop. Just write:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM