def CEupload(file_name): # Upload a file with convergent encryption (using the file's hash as its key). remote_file_name = base64.urlsafe_b64encode(enc.siv_encrypt(pbkey1, pbkey2, file_name, "")) content_file_name = remote_file_name + '.contents' key_file_name = remote_file_name + '.key' s = open(file_name, 'rb').read() key = hashlib.sha256(s).digest()[0:16] # print "key: " + key c1 = enc.encrypt(key, s) temp_contents = open("./tempc", "wb") temp_contents.write(c1) temp_contents.close() temp_contents = open("./tempc", "rb") hash_r = hmac.new(pbkey3, c1+remote_file_name+key, hashlib.sha256).digest() c2 = enc.encrypt(pbkey4, key + hash_r) temp_key = open("./tempk", "wb") temp_key.write(c2) temp_key.close() temp_key = open("./tempk", "rb") up_thread = Thread( target=dpbx_client.put_file, args=(content_file_name, temp_contents, True) ) up_thread.start() dpbx_client.put_file(key_file_name, temp_key, True) up_thread.join() print file_name + " has been uploaded with convergent encryption."
def encdelete(file_name): remote_file_name = base64.urlsafe_b64encode(enc.siv_encrypt(pbkey1, pbkey2, file_name, "")) content_file_name = remote_file_name + '.contents' key_file_name = remote_file_name + '.key' #print "key file name: " + key_file_name dpbx_client.file_delete(key_file_name) dpbx_client.file_delete(content_file_name)
def download(file_name): # Download a file remote_file_name = base64.b64encode(enc.siv_encrypt(pbkey1, pbkey2, file_name, "")) f2 = '/' + remote_file_name + ".key" fx1 = '/' + remote_file_name + ".contents" down_thread = Thread(target=download_proxy, args=(fx1,)) down_thread.start() c2 = dpbx_client.get_file(f2).read() down_thread.join() hash_and_key = enc.decrypt(pbkey4, c2) hash_r = hash_and_key[AES_BLOCK_SIZE:] key = hash_and_key[:AES_BLOCK_SIZE] hash2 = hmac.new(pbkey3, cx1+remote_file_name+key, hashlib.sha256).digest() if (hash_r != hash2): print hash_r.encode("hex"), hash2.encode("hex"), "Ciphertext has been corrupted" return contents = enc.decrypt(key, cx1) outf = open(file_name, "wb") outf.write(contents) outf.close() print file_name + " has been downloaded."
def p2pupload(file_name, info): # Upload a file. remote_file_name = base64.urlsafe_b64encode(enc.siv_encrypt(pbkey1, pbkey2, file_name, "")) content_file_name = remote_file_name + '.contents' key_file_name = remote_file_name + '.key' file_plaintext = open(file_name, 'rb').read() key = enc.get_convergent_key(file_plaintext, info) c1 = enc.encrypt(key, file_plaintext) temp_contents = open("./tempc", "wb") temp_contents.write(c1) temp_contents.close() temp_contents = open("./tempc", "rb") hash_r = hmac.new(pbkey3, c1+remote_file_name+key, hashlib.sha256).digest() c2 = enc.encrypt(pbkey4, key + hash_r) temp_key = open("./tempk", "wb") temp_key.write(c2) temp_key.close() temp_key = open("./tempk", "rb") up_thread = Thread( target=dpbx_client.put_file, args=(content_file_name, temp_contents, True) ) up_thread.start() dpbx_client.put_file(key_file_name, temp_key, True) up_thread.join() print file_name + " has been uploaded with p2p encryption."