I'm searching on the encrypted cloud database, and the returned records are decrypted on the client side, I received the returned data on a data frame. when the returned data is only one record, it decrypts the returned encrypted data. But when the returned data is more records, it raised the exception error as shown in code below returned data is only one record
def search_by_SCKHAblindindex_age_sex_maritalstatus(db, Keywordage,
Keywordsex,Keywordmaritalstatus, SCKHAindexKey, enckey, uname):
SCKHAindexsex = getSCKHABlindIndexC1(SCKHAindexKey,Keywordsex)
SCKHAindexage = getSCKHABlindIndexC1(SCKHAindexKey, Keywordage)
SCKHAindexmaritalstatus = getSCKHABlindIndexC1(SCKHAindexKey, Keywordmaritalstatus)
cursor = db.cursor(dictionary=True)
query = "SELECT sex_iv, sex_enc, sex_tag FROM Adult WHERE sex_idx like'%{}%' AND age_idx like '%{}%' AND maritalstatus_idx like '%{}%';".format(SCKHAindexsex, SCKHAindexage,SCKHAindexmaritalstatus)
print("Query is : ",query)
cursor.execute(query)
output = cursor.fetchall()
print("Affect Rows are", output)
rowcount=cursor.rowcount
print("Number of Rows are", rowcount)
df = pd.read_sql_query(query, con = db)
sex_iv = df.loc[0:, 'sex_iv'].to_string().split(' ')[4]
sex_enc = df.loc[0:, 'sex_enc'].to_string().split(' ')[4]
sex_tag = df.loc[0:, 'sex_tag'].to_string().split(' ')[4]
print("enckey",enckey)
r=0
for i, row in df.iterrows():
try:
plaintext = decrypt(enckey, uname.encode('UTF-8'), b64decode(sex_iv.encode('UTF-8')), b64decode(sex_enc.encode('UTF-8')), b64decode(sex_tag.encode('UTF-8')))
r=r+1
# print("plaintext: " + plaintext)
except:
return "An error has occurredd"
return "Encrypted: " + sex_enc + ", Decrypted: " + plaintext.decode('UTF-8')
An example of the data is:
[
{'sex_iv': 'F+W0/R0SywASqee0', 'sex_enc': 'WMsTfNc=', 'sex_tag': 'h6xW87yLmk+irzVwDsTlcg=='},
{'sex_iv': 'Md5npLEcSdDFlq9J', 'sex_enc': 'yNGNcmg=', 'sex_tag': 'B0aqju14wBOuwQpuboOjqw=='},
{'sex_iv': 'NqKMbfqgCFS+TkwQ', 'sex_enc': 'wcVyCQ8=', 'sex_tag': 'dODuWc5sDmGWPueQvqLNEA=='},
{'sex_iv': 'gDdfuZIBttomJ6Sr', 'sex_enc': '0xRKHZo=', 'sex_tag': 'b9cIBycHciWKUkcYjOLpug=='}
]
The implementation of decode() looks like:
def decrypt(key, associated_data, iv, ciphertext, tag):
decryptor = Cipher(algorithms.AES(key), modes.GCM(iv, tag),).decryptor()
decryptor.authenticate_additional_data(associated_data)
return decryptor.update(ciphertext) + decryptor.finalize()