Ejemplo n.º 1
0
def getKeys(key):
    pr.add('generation keys')
    C = [] # константы
    F = [] # ячейки Фейстеля
    K = [key[:int(len(key) / 2)], key[int(len(key) / 2) :]]

    for i in range(32):
        if len(hex(i + 1)[2:]) == 1:
            C.append(L('0' + hex(i + 1)[2:] + '000000000000000000000000000000').upper())
        else:
            C.append(L(hex(i + 1)[2:] + '000000000000000000000000000000').upper())

    # формирование ячеек Фейстеля
    F.append([ K[1], X(L(S(X( K[0], C[0]))),  K[1])])
    for i in range(32):
        K = [ F[i][1], X(L(S(X( F[i][0], C[i]))),  F[i][1])]
        F.append(K)

    # разбиение заданного ключа пополам
    K = [key[:int(len(key) / 2)], key[int(len(key) / 2) :]]

    # формирование новых ключей из ячеек Фейстеля
    for i in range(len(F)):
        if (i + 1) % 8 == 0:
            K.append(F[i][0])
            K.append(F[i][1])
    return (K)
def isThereAUserOrNot(username, password):
    values = (username, password)
    sql = '''SELECT * FROM users WHERE login = ? AND password = ?;'''
    cursor.execute(sql, values)
    conn.commit()
    data = cursor.fetchall()
    if len(data) == 1 and data[0] == values:
        pr.add('successful login by `' + username + '`')
        return (accessed)
    else:
        pr.add('failed login by `' + username + '`')
        return (accessDenied)
Ejemplo n.º 3
0
def decrypt(path, newPath, key):
    pr.add('decrypt file')
    f = open(path, 'r')
    text = f.read()
    f.close()

    K = gh.getKeys(transformKey(key))

    text = hexToUtf8(gh.decrypt(text, K))

    f = open(newPath, 'w')
    f.write(text)
    f.close()
Ejemplo n.º 4
0
def encrypt(path, newPath, key):
    pr.add('encrypt file')
    f = open(path, 'r')
    text = f.read()
    f.close()

    K = gh.getKeys(transformKey(key))

    text = gh.encrypt(utf8ToHex(text), K)

    f = open(newPath, 'w')
    f.write(text)
    f.close()
Ejemplo n.º 5
0
def decrypt(text, K):
    pr.add('text decrypt')
    textArray = []
    for i in range(int(len(text) / 32)):
        textArray.append(text[i * 32 : i * 32 + 32])

    textDecrypt = []
    for j in textArray:
        # расшифровка текста
        textDecrypted = j
        for i in range(9, 0, -1):
            textDecrypted = S(L(X(textDecrypted, K[i]), 'reverse'), 'reverse')
        textDecrypted = X(textDecrypted, K[0])
        textDecrypt.append(textDecrypted)
    return(''.join(textDecrypt))
Ejemplo n.º 6
0
def encrypt(text, K):
    pr.add('text encrypt')
    count = 32 - len(text) % 32
    if count != 0 and count != 32:
        for i in range(count):
            text += '0'
    textArray = []
    for i in range(int(len(text) / 32)):
        textArray.append(text[i * 32 : i * 32 + 32])

    textEncrypt = []
    for j in textArray:
        # шифрование текста
        textEncrypted = j
        for i in range(9):
            textEncrypted = L(S(X(textEncrypted, K[i])))
        textEncrypted = X(textEncrypted, K[9])
        textEncrypt.append(textEncrypted)
    return(''.join(textEncrypt))
def insertUser(username, password):
    pr.add('adding user `' + username + '`')
    values = (username, password)
    sql = '''INSERT INTO users(login, password) VALUES(?,?);'''
    cursor.execute(sql, values)
    conn.commit()
def createTableUsers():
    pr.add('users table creation')
    sql = '''CREATE TABLE users(login text, password text);'''
    cursor.execute(sql)
    conn.commit()
Ejemplo n.º 9
0
def utf8ToHex(text):
    pr.add('converting text from utf-8 to hex')
    text = binascii.hexlify(text.encode('utf8')).decode('utf8')
    return text
Ejemplo n.º 10
0
def readFile(path):
    pr.add('reading file')
    f = open(path, 'r')
    text = f.read()
    f.close()
    return text
Ejemplo n.º 11
0
def hexToUtf8(text):
    pr.add('converting text from hex to utf-8')
    text = binascii.unhexlify(text).decode('utf8')
    return text
Ejemplo n.º 12
0
def writeFile(path, text):
    pr.add('writing to file')
    f = open(path, 'w')
    f.write(text)
    f.close()