예제 #1
0
def encryptBlock(data: np.matrix, key: np.matrix) -> np.matrix:
    encoded = data.getT() ^ key.getT()
    keys = extendKey(key)
    for i in range(1, len(keys)):
        encoded = bytesSub(encoded)
        # print("{} After subst     {}".format(i, encoded.flatten()))
        encoded = shiftRows(encoded)
        # print("{} After shift row {}".format(i, encoded.flatten()))
        if i < len(keys) - 1:
            encoded = mixColumns(encoded)
            # print("{} After  mix      {}".format(i, encoded.flatten()))
        # print("Applying key {}".format(keys[i].flatten()))
        encoded = encoded ^ keys[i].getT()
        # print("{} After RoundKey  {}".format(i, encoded.flatten()))
    return encoded.getT().tobytes()[::8]
예제 #2
0
def decryptBlock(data: np.matrix, key: np.matrix) -> np.matrix:
    encoded = data.getT()
    # print("Decrypting ")
    # print(encoded)
    keys = extendKey(key)
    for i in range(len(keys) - 1, 0, -1):
        # print("Applying key {}".format(keys[i].flatten()))
        encoded = encoded ^ keys[i].getT()
        # print("{} Befor RoundKey  {}".format(i, encoded.flatten()))
        if i < len(keys) - 1:
            # If we run mixColumns 4 times, matrix doesnt change
            for j in range(3):
                encoded = mixColumns(encoded)
            # print("{} Befor  mix      {}".format(i, encoded.flatten()))
        encoded = shiftRows(encoded, True)
        # print("{} Befor shift row {}".format(i, encoded.flatten()))
        encoded = bytesSubInv(encoded)
        # print("{} Before subst    {}".format(i, encoded.flatten()))
        # == ENCRYPTING
    # print(encoded)
    encoded = encoded ^ key.getT()
    # print(encoded)
    return encoded.getT().tobytes()[::8]
예제 #3
0
def mixColumns_bak(data: np.matrix, decrypt: bool = False) -> np.matrix:
    """
    Return the mixed (or unmixed if decryption) of the given matrix

    Parameters
    ---
    data (np.matrix)
        Matrix to mix/unmix
    decrypt (bool=False)
        Set to True to enable decryption

    Result
    ---
    np.matrix
    """
    return np.concatenate([mixColumn(i) for i in data.getT()]).getT()
예제 #4
0
def mixColumns(data: np.matrix, decrypt: bool = False) -> np.matrix:
    """
    Return the mixed (or unmixed if decryption) of the given matrix

    Parameters
    ---
    data (np.matrix)
        Matrix to mix/unmix
    decrypt (bool=False)
        Set to True to enable decryption

    Result
    ---
    np.matrix
    """
    ar_concat = []
    for i in range(len(data)):
        ar_concat.append(mix_single_column(np.asarray(data.getT())[i]))
    ar_concat = np.concatenate(ar_concat)
    return np.concatenate(ar_concat).getT()