def decrypt_acrastruct(data, private_key, client_id=None, zone_id=None): public_key = data[8:8+45] encrypted_symmetric = data[8+45:8+45+84] smessage_decryptor = smessage.SMessage(private_key, public_key) symmetric = smessage_decryptor.unwrap(encrypted_symmetric) encrypted_data = data[8+45+84+8:] if zone_id: return scell.SCellSeal(symmetric).decrypt(encrypted_data, zone_id) else: return scell.SCellSeal(symmetric).decrypt(encrypted_data)
def decrypt_private_key(private_key, key_id, master_key): return scell.SCellSeal(master_key).decrypt(private_key, key_id)
if node.text: # encrypt leaf data and replace it in file by base64 encoding node.text = base64.b64encode( encryptor.encrypt(node.text.encode('utf-8'), context + "/" + node.tag)).decode('ascii') def decrypt_children(node, context): if len(node): for i in range(0, len(node)): decrypt_children(node[i], context + "/" + node.tag) else: if node.text: # decrypt base64 encoded leaf data and replace it by plain value node.text = encryptor.decrypt(base64.b64decode( node.text), context + "/" + node.tag).decode('utf-8') # encoding file 'example_data/test.xml' and save result to encoded_data.xml tree = ET.parse('example_data/test.xml') root = tree.getroot() encryptor = scell.SCellSeal(master_key) encrypt_children(root, "") tree.write("encoded_data.xml") # decoding file encoded_data.xml and save result to decoded_data.xml tree2 = ET.parse('encoded_data.xml') root2 = tree2.getroot() decrypt_children(root2, "") tree2.write("decoded_data.xml")
# limitations under the License. # import sys from base64 import b64encode, b64decode from pythemis import scell _, COMMAND, KEY, MESSAGE, CONTEXT = range(5) if len(sys.argv) not in (4, 5): print('Usage: <command: enc | dec > <key> <message> <context (optional)>') exit(1) command = sys.argv[COMMAND] key = sys.argv[KEY] message = sys.argv[MESSAGE] context = sys.argv[CONTEXT].encode('utf-8') if len(sys.argv) == 5 else None cell = scell.SCellSeal(key.encode('utf-8')) if command == 'enc': encrypted_message = cell.encrypt(message.encode('utf-8'), context) encoded_message = b64encode(encrypted_message).decode('ascii') print(encoded_message) elif command == 'dec': decoded_message = b64decode(message.encode('ascii')) decrypted_message = cell.decrypt(decoded_message, context) print(decrypted_message.decode('utf-8')) else: print('Wrong command, use "enc" or "dec"') exit(1)
import sys from base64 import b64encode, b64decode from pythemis import scell _, COMMAND, KEY, MESSAGE, CONTEXT = range(5) if len(sys.argv) not in (4, 5): sys.stderr.write('usage: {enc|dec} <passphrase> <message> [context]\n') exit(1) command = sys.argv[COMMAND] passphrase = sys.argv[KEY] message = sys.argv[MESSAGE] context = sys.argv[CONTEXT].encode('UTF-8') if len(sys.argv) == 5 else None cell = scell.SCellSeal(passphrase=passphrase) if command == 'enc': encrypted_message = cell.encrypt(message.encode('UTF-8'), context) encoded_message = b64encode(encrypted_message) print(encoded_message.decode('ASCII')) elif command == 'dec': decoded_message = b64decode(message.encode('ASCII')) decrypted_message = cell.decrypt(decoded_message, context) print(decrypted_message.decode('UTF-8')) else: sys.stderr.write( 'unknown command "{}", use "enc" or "dec"\n'.format(command)) exit(1)
if node.text: # encrypt leaf data and replace it in file by base64 encoding node.text = base64.b64encode( encryptor.encrypt(node.text.encode('utf-8'), context + "/" + node.tag)).decode('ascii') def decrypt_children(node, context): if len(node): for i in range(0, len(node)): decrypt_children(node[i], context + "/" + node.tag) else: if node.text: # decrypt base64 encoded leaf data and replace it by plain value node.text = encryptor.decrypt(base64.b64decode( node.text), context + "/" + node.tag).decode('utf-8') # encoding file 'example_data/test.xml' and save result to encoded_data.xml tree = ET.parse('example_data/test.xml') root = tree.getroot() encryptor = scell.SCellSeal(password) encrypt_children(root, "") tree.write("encoded_data.xml") # decoding file encoded_data.xml and save result to decoded_data.xml tree2 = ET.parse('encoded_data.xml') root2 = tree2.getroot() decrypt_children(root2, "") tree2.write("decoded_data.xml")