简体   繁体   English

当键是字典中的哈希字符串时,如何 append 值?

[英]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. 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'

The key of the dictionary is hashed by using hmac, so I can't append values.字典的键是使用 hmac 散列的,所以我不能 append 值。 Is it possible to append values when the key is hashed?当密钥被散列时,是否可以 append 值?

encrypted_index looks like that 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() is for adding an element to a list. append()用于将元素添加到列表中。 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.也不需要for循环。 Just write:写吧:

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