def E3DES(message,ikey):
    keys = keyCreator(ikey)


    return (op.bintohex(op.stringtobin(DESencrypt(DESdecrypt(DESencrypt(message, keys, 0), keys, 1), keys, 2))))
def DESdecrypt(input, Keys, round):

    final = ''

    # a lookup table for initial permutation
    InitialPerm = [58, 50, 42, 34, 26, 18, 10, 2,
                   60, 52, 44, 36, 28, 20, 12, 4,
                   62, 54, 46, 38, 30, 22, 14, 6,
                   64, 56, 48, 40, 32, 24, 16, 8,
                   57, 49, 41, 33, 25, 17, 9, 1,
                   59, 51, 43, 35, 27, 19, 11, 3,
                   61, 53, 45, 37, 29, 21, 13, 5,
                   63, 55, 47, 39, 31, 23, 15, 7]

    # a lookup table for expansion bit
    Expansionbit = [32, 1, 2, 3, 4, 5,
                    4, 5, 6, 7, 8, 9,
                    8, 9, 10, 11, 12, 13,
                    12, 13, 14, 15, 16, 17,
                    16, 17, 18, 19, 20, 21,
                    20, 21, 22, 23, 24, 25,
                    24, 25, 26, 27, 28, 29,
                    28, 29, 30, 31, 32, 1]

    # a lookup table for column number, one fo each iteration
    ColumnNumberTables = [[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
                           0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
                           4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
                           15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13],

                          [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
                           3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
                           0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
                           13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 2, 0, 5, 14, 9],

                          [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
                           13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
                           13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
                           1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12],

                          [7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
                           13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
                           10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
                           3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14],

                          [2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
                           14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
                           4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
                           11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3],

                          [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
                           10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
                           9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
                           4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13],

                          [4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
                           13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
                           1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
                           6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12],

                          [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
                           1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
                           7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
                           2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]]

    # a lookup table for second permutation
    SecondPerm = [16, 7, 20, 21,
                  29, 12, 28, 17,
                  1, 15, 23, 26,
                  5, 18, 31, 10,
                  2, 8, 24, 14,
                  32, 27, 3, 9,
                  19, 13, 30, 6,
                  22, 11, 4, 25]

    # a lookup table for the final permutation
    Finalperm = [40, 8, 48, 16, 56, 24, 64, 32,
                 39, 7, 47, 15, 55, 23, 63, 31,
                 38, 6, 46, 14, 54, 22, 62, 30,
                 37, 5, 45, 13, 53, 21, 61, 29,
                 36, 4, 44, 12, 52, 20, 60, 28,
                 35, 3, 43, 11, 51, 19, 59, 27,
                 34, 2, 42, 10, 50, 18, 58, 26,
                 33, 1, 41, 9, 49, 17, 57, 25]



    input = op.stringtobin(input)

    # pads the message with zeros after it to make it a multiple of 64
    while len(input) % 64 != 0:
        input += "0"

    # finds out how many blocks of 64 there are
    blocks = len(input) // 64

    # runs for every block
    for index in range(blocks):

        # gets the 64 bit chunk from the message
        message = input[index * 64 : (index + 1) * 64]

        # does the initial permutation
        message = Perm(InitialPerm,message)


        # splits the message into left and right
        MessageLeft = message[0:32]
        MessageRight = message[32:64]



        # runs for each key
        for subkey in range (16):
            righttemp = ''

            # lefttemp becomes message right we do this because messageLeft has to equal
            # message right, but we use MessageRight in another step so lefttemp is necessary
            lefttemp = MessageRight

            # expands righttemp
            righttemp = Perm(Expansionbit, MessageRight)

            righttemp = op.bitxor(Keys[round][15 - subkey],righttemp)

            while len(righttemp) < 48:
                righttemp = "0" + righttemp

            righttemp = Sbox(righttemp)

            righttemp = Perm(SecondPerm,righttemp)

            MessageRight = op.bitxor(MessageLeft, righttemp)


            while len(MessageRight) < 32:
                MessageRight = "0" + MessageRight

            MessageLeft = lefttemp

        temp = ''

        message = (MessageRight + MessageLeft)


        for g in range(64):
            temp += message[Finalperm[g] - 1]

        final += temp


    return op.bintostring(final)
def keyCreator(ogkey):
    PC_1 = [57, 49, 41, 33, 25, 17, 9, 1,  # Look up table for Permuted Choice 1
            58, 50, 42, 34, 26, 18, 10, 2,
            59, 51, 43, 35, 27, 19, 11, 3,
            60, 52, 44, 36, 63, 55, 47, 39,
            31, 23, 15, 7, 62, 54, 46, 38,
            30, 22, 14, 6, 61, 53, 45, 37,
            29, 21, 13, 5, 28, 20, 12, 4]

    PC_2 = [14, 17, 11, 24, 1, 5,  # Look up table for Permuted Choice 2
            3, 28, 15, 6, 21, 10,
            23, 19, 12, 4, 26, 8,
            16, 7, 27, 20, 13, 2,
            41, 52, 31, 37, 47, 55,
            30, 40, 51, 45, 33, 48,
            44, 49, 39, 56, 34, 53,
            46, 42, 50, 36, 29, 32]

    iteration = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1]  # List for the changing iterations for rotating

    ogkey = op.stringtobin(ogkey)  # changes the key to bits

    while len(ogkey) < 192:  # Pads the bits with zeros
        ogkey += "0"

    Keys = [[], [], []]  # Initalizes the final list of the three keys

    for n in range(3):  # seperates the 192 bits into three chunks

        key = ogkey[n * 64: (n + 1) * 64]

        temp = ""
        newkey = ""
        lkey = ""
        rkey = ""
        Key = []

        for i in range(56):  # Permutes the block with Permuted choice 1
            temp += (key[PC_1[i] - 1])

        for value in range(28):  # splits the block into two halves
            lkey += (temp[value])

        for value in range(28, 56):
            rkey += (temp[value])

        for i in range(16):
            lkey = op.leftrotate(lkey, iteration[i])  # rotates left block by the given iteration
            rkey = op.leftrotate(rkey, iteration[i])  # rotates right block by the given iteration

            newkey = lkey + rkey  # puts the blocks back together

            temp = ""
            for i in range(48):  # permutes the block again with Permuted choice 2
                temp += (newkey[PC_2[i] - 1])

            Key.append(temp)  # appends these values to a list

        Keys[n] = Key  # appends the list to a final list.

    return Keys  # returns the final list of the three keys and their subkeys.
Exemplo n.º 4
0
    def hash(message, salt=""):
        """
        This method hashes any string.
        :param message: The messaee that needs to be hashed
        :param salt: A salt is appended to a message, it is optional whether you want to include a salt while hashing
        :return: The hashed message
        """
        # ---------Initial Hash Values-------
        # these hash values change for each 512 bit chunk, and start as the following
        h0 = "01101010000010011110011001100111"
        h1 = "10111011011001111010111010000101"
        h2 = "00111100011011101111001101110010"
        h3 = "10100101010011111111010100111010"
        h4 = "01010001000011100101001001111111"
        h5 = "10011011000001010110100010001100"
        h6 = "00011111100000111101100110101011"
        h7 = "01011011111000001100110100011001"

        # -----------------Padding-----------------
        # This sections initializes the first 16 "words". Each "word" is 32 bits.

        # gets the message input and turns it into binary
        paddedmessage = op.stringtobin(message + salt)

        # takes the length of the padded message, converts it into bits, and stores it in bitsinmessage
        bitsinmessage = op.inttobin(len(paddedmessage))
        # adds a one to the end of the padded message
        paddedmessage += "1"

        # adds 0's to make the length of  (paddedmessage - the length of bitsinmessage) a multiple of 512
        while (len(paddedmessage) + len(bitsinmessage)) % 512 != 0:
            paddedmessage += "0"

        # adds bits in messsage to the end of padded message
        paddedmessage += bitsinmessage

        # creates a variable called fullmessage which is the padded message and is not affected
        fullmessage = paddedmessage

        # for each 512-bit chunk of full message (represented my paddedmessage)the following happens
        for z in range(len(paddedmessage) // 512):
            # words is a list that stores the 32 bit 'words'
            words = []
            # takes the chunk and seperates it into 16, 32 bit words
            paddedmessage = fullmessage[512 * z:512 * (z + 1)]
            for i in range(int(16)):
                words.append(paddedmessage[32 * i:32 * (i + 1)])

            # ----------Creating the 64 words from the first 16-------

            # creates the other 48 words using the first 16
            for i in range(16, 64):

                s0 = op.bitxor(
                    op.bitxor(op.rightrotate(words[i - 15], 7),
                              op.rightrotate(words[i - 15], 18)),
                    op.rightshift(words[i - 15], 3))
                s1 = op.bitxor(
                    op.bitxor(op.rightrotate(words[i - 2], 17),
                              op.rightrotate(words[i - 2], 19)),
                    op.rightshift(words[i - 2], 10))

                sum1 = op.bitadd(words[i - 16], s0)
                sum2 = op.bitadd(words[i - 7], s1)
                total = op.bitadd(sum1, sum2)
                while len(total) < 32:
                    total = "0" + total

                # Only take the first 32 bits, starting from the right by chopping of the first 8 bytes
                # if its bigger
                if len(total) > 32:
                    total = total[8:]

                # adds the new word into the list of words
                words.append(total)

            #     for i in range(len(words)):
            #        print(str((i+1)) + ") " + str(words[i]))

            # -----Initialize variables to initial hash values, as they will change-----
            a = h0
            b = h1
            c = h2
            d = h3
            e = h4
            f = h5
            g = h6
            h = h7

            # Checks to make sure the bit length of any variable doesn't exceed 32
            for i in range(0, 64):
                if len(a) > 32:
                    a = a[8:]
                if len(b) > 32:
                    b = b[8:]
                if len(c) > 32:
                    c = c[8:]
                if len(d) > 32:
                    d = d[8:]
                if len(e) > 32:
                    e = e[8:]
                if len(f) > 32:
                    f = f[8:]
                if len(g) > 32:
                    g = g[8:]
                if len(h) > 32:
                    h = h[8:]

                # The Main Algorithm
                S1 = op.bitxor(
                    op.bitxor(op.rightrotate(e, 6), op.rightrotate(e, 11)),
                    op.rightrotate(e, 25))
                ch = op.bitxor(op.bitand(e, f), op.bitand(op.bitnot(e), g))
                temp1 = op.bitadd(
                    op.bitadd(op.bitadd(op.bitadd(h, S1), ch),
                              SHA256.constants[i]), words[i])
                S0 = op.bitxor(
                    op.bitxor(op.rightrotate(a, 2), op.rightrotate(a, 13)),
                    op.rightrotate(a, 22))
                maj = op.bitxor(op.bitxor(op.bitand(a, b), op.bitand(a, c)),
                                op.bitand(b, c))
                temp2 = op.bitadd(S0, maj)

                # Variable Swaps and additions
                h = g
                g = f
                f = e
                e = op.bitadd(d, temp1)
                d = c
                c = b
                b = a
                a = op.bitadd(temp1, temp2)

            # Add each letter to its corresponding hash value
            h0 = op.bitadd(h0, a)
            h1 = op.bitadd(h1, b)
            h2 = op.bitadd(h2, c)
            h3 = op.bitadd(h3, d)
            h4 = op.bitadd(h4, e)
            h5 = op.bitadd(h5, f)
            h6 = op.bitadd(h6, g)
            h7 = op.bitadd(h7, h)

        # Convert to Hex, then to a string.
        h0 = str(op.bintohex(h0))
        h1 = str(op.bintohex(h1))
        h2 = str(op.bintohex(h2))
        h3 = str(op.bintohex(h3))
        h4 = str(op.bintohex(h4))
        h5 = str(op.bintohex(h5))
        h6 = str(op.bintohex(h6))
        h7 = str(op.bintohex(h7))

        # Cut off if the length of the hex string is greater than 8
        if len(h0) > 8:
            h0 = h0[len(h0) - 8:]
        if len(h1) > 8:
            h1 = h1[len(h1) - 8:]
        if len(h2) > 8:
            h2 = h2[len(h2) - 8:]
        if len(h3) > 8:
            h3 = h3[len(h3) - 8:]
        if len(h4) > 8:
            h4 = h4[len(h4) - 8:]
        if len(h5) > 8:
            h5 = h5[len(h5) - 8:]
        if len(h6) > 8:
            h6 = h6[len(h6) - 8:]
        if len(h7) > 8:
            h7 = h7[len(h7) - 8:]

        # Concatenate the strings
        finalhash = h0 + h1 + h2 + h3 + h4 + h5 + h6 + h7
        return finalhash