Beispiel #1
0
def get_record(connection, id):
    # retrieve record from db by id
    dec = scell.SCellTokenProtect(password)
    with connection.cursor() as cursor:
        cursor.execute(
            "SELECT * FROM scell_data "
            "INNER JOIN scell_data_auth ON "
            "scell_data.id = %s AND scell_data.id=scell_data_auth.id;", [id])
        row = cursor.fetchone()
        _, DATA_NUM, DATA_DATA, AUTH_ID, AUTH_NUM, AUTH_DATA = range(6)
        if (sys.version_info > (3, 0)):
            print("stored data:", row[DATA_NUM].tobytes(),
                  row[AUTH_NUM].tobytes(), row[DATA_DATA].tobytes(),
                  row[AUTH_DATA].tobytes())
        else:
            print("stored data:", bytes(row[DATA_NUM]), bytes(row[AUTH_NUM]),
                  bytes(row[DATA_DATA]), bytes(row[AUTH_DATA]))
        num = dec.decrypt(bytes(row[DATA_NUM]),
                          bytes(row[AUTH_NUM])).decode('utf-8')

        data = dec.decrypt(bytes(row[DATA_DATA]),
                           bytes(row[AUTH_DATA])).decode('utf-8')
    return num, data
Beispiel #2
0
def add_record(connection, field1, field2):
    encryptor = scell.SCellTokenProtect(password)
    # encrypt field1
    encrypted_field1, field1_auth_data = encryptor.encrypt(
        field1.encode('utf-8'))
    # encrypt field2
    encrypted_field2, field2_auth_data = encryptor.encrypt(
        field2.encode('utf8'))

    with connection.cursor() as cursor:
        # store main cryptomessage
        cursor.execute(
            "INSERT INTO scell_data (num, data) VALUES (%s, %s) RETURNING ID",
            (psycopg2.Binary(encrypted_field1),
             psycopg2.Binary(encrypted_field2)))
        new_id_value = cursor.fetchone()[0]

        # store additional auth values
        cursor.execute(
            "INSERT INTO scell_data_auth (id, num, data) VALUES (%s, %s, %s)",
            (new_id_value, psycopg2.Binary(field1_auth_data),
             psycopg2.Binary(field2_auth_data)))
        connection.commit()
    return new_id_value
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]
context = sys.argv[CONTEXT] if len(sys.argv) == 5 else ''

cell = scell.SCellTokenProtect(key.encode('utf-8'))
if command == 'enc':
    message = sys.argv[MESSAGE]
    encrypted, token = cell.encrypt(message.encode('utf-8'),
                                    context.encode('utf-8'))
    encoded_message = b64encode(encrypted).decode('ascii')
    encoded_token = b64encode(token).decode('ascii')
    print(','.join((encoded_message, encoded_token)))
elif command == 'dec':
    message, token = sys.argv[MESSAGE].split(',')
    decoded_message = b64decode(message.encode('utf-8'))
    decoded_token = b64decode(token.encode('utf-8'))
    decrypted = cell.decrypt(decoded_message, decoded_token,
                             context.encode('utf-8'))
    print(decrypted.decode('utf-8'))
else: