Пример #1
0
Файл: SDES.py Проект: PJYGit/FT
def e_SDES_CBC(plaintext, key):
    undefined = utilities.get_undefined(plaintext, utilities.get_B6Code())
    text = utilities.remove_undefined(plaintext, utilities.get_B6Code())
    size = int(int(get_SDES_value('block_size')) / 6)
    blocks = utilities.text_to_blocks(text, size)
    while len(blocks[-1]) != size:
        blocks[-1] += 'Q'

    IV = get_IV()

    ciphertext = ''
    for block in blocks:
        cipher_block = ''
        for b in block:
            cipher_block += encode_B6(b)

        cipher_block = utilities.xor(cipher_block, IV)

        for i in range(int(get_SDES_value('rounds'))):
            cipher_block = feistel(cipher_block, get_subKey(key, i + 1))

        cipher_block = cipher_block[
            int(len(cipher_block) /
                2):] + cipher_block[:int(len(cipher_block) / 2)]

        b_blocks = utilities.text_to_blocks(cipher_block, 6)
        for b_b in b_blocks:
            ciphertext += decode_B6(b_b)

        IV = cipher_block

    ciphertext = utilities.insert_undefinedList(ciphertext, undefined)

    return ciphertext
Пример #2
0
Файл: SDES.py Проект: PJYGit/FT
def d_SDES_ECB(ciphertext, key):
    # your code here
    if not isinstance(ciphertext, str) or len(ciphertext) == 0:
        print('Error(d_SDES_ECB): Invalid input', end='')
        return ''
    if get_SDES_value('encoding_type') == '' or get_SDES_value(
            'block_size') == '' or get_SDES_value('key_size') == '':
        print('Error(d_SDES_ECB): Invalid configuration', end='')
        return ''

    if len(key) == 0:
        if get_SDES_value('p') == '' or get_SDES_value(
                'q') == '' or get_SDES_value('key_size') == '':
            print('Error(d_SDES_ECB): Invalid key', end='')
            return ''
        key = generate_key_SDES()
    if not utilities.is_binary(key):
        print('Error(d_SDES_ECB): Invalid key', end='')
        return ''
    if len(key) != int(get_SDES_value('key_size')):
        print('Error(d_SDES_ECB): Invalid key', end='')
        return ''

    undefined = utilities.get_undefined(ciphertext, utilities.get_B6Code())
    text = utilities.remove_undefined(ciphertext, utilities.get_B6Code())
    size = int(int(get_SDES_value('block_size')) / 6)
    blocks = utilities.text_to_blocks(text, size)

    plaintext = ''
    for block in blocks:
        plain_block = ''
        for b in block:
            plain_block += encode_B6(b)

        rounds = int(get_SDES_value('rounds'))
        for i in range(rounds):
            plain_block = feistel(plain_block, get_subKey(key, rounds - i))

        plain_block = plain_block[int(
            len(plain_block) / 2):] + plain_block[:int(len(plain_block) / 2)]

        b_blocks = utilities.text_to_blocks(plain_block, 6)
        for b_b in b_blocks:
            plaintext += decode_B6(b_b)

    while plaintext[-1] == 'Q':
        plaintext = plaintext[:-1]

    plaintext = utilities.insert_undefinedList(plaintext, undefined)

    return plaintext
Пример #3
0
Файл: SDES.py Проект: PJYGit/FT
def e_SDES_ECB(plaintext, key):
    # your code here
    if not isinstance(plaintext, str) or len(plaintext) == 0:
        print('Error(e_SDES_ECB): Invalid input', end='')
        return ''
    if get_SDES_value('encoding_type') == '' or get_SDES_value(
            'block_size') == '' or get_SDES_value('key_size') == '':
        print('Error(e_SDES_ECB): Invalid configuration', end='')
        return ''

    if len(key) == 0:
        if get_SDES_value('p') == '' or get_SDES_value(
                'q') == '' or get_SDES_value('key_size') == '':
            print('Error(e_SDES_ECB): Invalid key', end='')
            return ''
        key = generate_key_SDES()
    if not utilities.is_binary(key):
        print('Error(e_SDES_ECB): Invalid key', end='')
        return ''
    if len(key) != int(get_SDES_value('key_size')):
        print('Error(e_SDES_ECB): Invalid key', end='')
        return ''

    undefined = utilities.get_undefined(plaintext, utilities.get_B6Code())
    text = utilities.remove_undefined(plaintext, utilities.get_B6Code())
    size = int(int(get_SDES_value('block_size')) / 6)
    blocks = utilities.text_to_blocks(text, size)
    while len(blocks[-1]) != size:
        blocks[-1] += 'Q'

    ciphertext = ''
    for block in blocks:
        cipher_block = ''
        for b in block:
            cipher_block += encode_B6(b)

        for i in range(int(get_SDES_value('rounds'))):
            cipher_block = feistel(cipher_block, get_subKey(key, i + 1))

        cipher_block = cipher_block[
            int(len(cipher_block) /
                2):] + cipher_block[:int(len(cipher_block) / 2)]

        b_blocks = utilities.text_to_blocks(cipher_block, 6)
        for b_b in b_blocks:
            ciphertext += decode_B6(b_b)

    ciphertext = utilities.insert_undefinedList(ciphertext, undefined)

    return ciphertext
Пример #4
0
Файл: SDES.py Проект: PJYGit/FT
def d_SDES_OFB(ciphertext, key):
    undefined = utilities.get_undefined(ciphertext, utilities.get_B6Code())
    text = utilities.remove_undefined(ciphertext, utilities.get_B6Code())
    size = int(int(get_SDES_value('block_size')) / 6)
    blocks = utilities.text_to_blocks(text, size)

    IV = get_IV()
    plaintext = ''
    counter = 0
    for block in blocks:
        temp = ''
        plain_block = IV
        rounds = int(get_SDES_value('rounds'))
        for i in range(rounds):
            plain_block = feistel(plain_block, get_subKey(key, i + 1))

        plain_block = plain_block[int(
            len(plain_block) / 2):] + plain_block[:int(len(plain_block) / 2)]

        IV = plain_block

        for b in block:
            temp += encode_B6(b)

        if counter <= len(blocks) - 2:
            plain_block = utilities.xor(plain_block, temp)
        else:
            plain_block = utilities.xor(temp, plain_block[:len(temp)])

        b_blocks = utilities.text_to_blocks(plain_block, 6)
        for b_b in b_blocks:
            plaintext += decode_B6(b_b)

        counter += 1

    plaintext = utilities.insert_undefinedList(plaintext, undefined)

    return plaintext
Пример #5
0
Файл: SDES.py Проект: PJYGit/FT
def d_SDES_CBC(ciphertext, key):
    undefined = utilities.get_undefined(ciphertext, utilities.get_B6Code())
    text = utilities.remove_undefined(ciphertext, utilities.get_B6Code())
    size = int(int(get_SDES_value('block_size')) / 6)
    blocks = utilities.text_to_blocks(text, size)

    plaintext = ''
    IV = get_IV()
    temp = ''
    for block in blocks:
        plain_block = ''
        for b in block:
            plain_block += encode_B6(b)
        temp = plain_block

        rounds = int(get_SDES_value('rounds'))
        for i in range(rounds):
            plain_block = feistel(plain_block, get_subKey(key, rounds - i))

        plain_block = plain_block[int(
            len(plain_block) / 2):] + plain_block[:int(len(plain_block) / 2)]

        plain_block = utilities.xor(IV, plain_block)

        b_blocks = utilities.text_to_blocks(plain_block, 6)
        for b_b in b_blocks:
            plaintext += decode_B6(b_b)

        IV = temp

    while plaintext[-1] == 'Q':
        plaintext = plaintext[:-1]

    plaintext = utilities.insert_undefinedList(plaintext, undefined)

    return plaintext
def d_SDES(ciphertext,key):
    plaintexttext = ''

    if isinstance(ciphertext, str) and ciphertext != '':
        conf = get_SDES_config()
        k = get_SDES_value('key_size')
        r = get_SDES_value('rounds')
        e = get_SDES_value('encoding_type')
        b = get_SDES_value('block_size')
        p = get_SDES_value('p')
        q = get_SDES_value('q')
        if k!= '' and r != '' and e != '' and b !='':
            if key == '' and p!= '' and q !='':
                key = generate_key_SDES()
            if key!='' and int(k) == len(key):
                k = int(k)
                r = int(r)
                b = int(b)
                b6_base = utilities.get_B6Code()
                sv_undef = utilities.get_undefined(ciphertext, b6_base)
                text = utilities.remove_undefined(ciphertext, b6_base)
                blk_text = []
                while len(text)%2 != 0:
                    text+='Q'
                for i in range(0,len(text),2):
                    blk_text.append(text[i]+text[i+1])
                round = 0 
                enc_text = []
                for x in blk_text:
                    enc_text.append(encode(x[0],e)+encode(x[1],e))
                stream = ''

                for blocks in enc_text:
                    for rounds in range(r):
                        if rounds == 0:
                            rnd_text = blocks
                        ki = get_subKey(key,r-rounds)
                        rnd_text = feistel(rnd_text, ki)
                        if rounds == r-1:
                            m = int(len(rnd_text)//2)
                            Li = rnd_text[:m]
                            Ri = rnd_text[m:]
                            rnd_text = Ri+Li    
                    stream += rnd_text
                plaintext = ''
                for s in range(0, len(stream), b//2):
                    plaintext+=decode(stream[s:s+b//2], e)
                plaintext = utilities.insert_undefinedList(plaintext, sv_undef)
                plaintext = plaintext.strip('Q')

            elif key!= '' and int(k) != len(key):
                print('Error(d_SDES): Invalid key')
                return ''
            elif key=='' and p == '' or q == '':
                print('Error(d_SDES): Invalid key')
                return ''
        else:
            print("Error(d_SDES): Invalid configuration")
            return ''
    else:
        print("Error(d_SDES): Invalid input")
        return ''
    return plaintext