コード例 #1
0
def d_SDES(ciphertext, key, mode):
    # your code here
    modes = ['ECB', 'CBC', 'OFB']
    if mode not in modes:
        print('Error(d_SDES): undefined mode', end='')
        return ''
    if not isinstance(ciphertext, str) or len(ciphertext) == 0:
        print('Error(d_SDES): Invalid input', end='')
        return ''

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

    if mode == modes[0]:
        return SDES.d_SDES_ECB(ciphertext, key)
    elif mode == modes[1]:
        return SDES.d_SDES_CBC(ciphertext, key)
    else:
        return SDES.d_SDES_OFB(ciphertext, key)
コード例 #2
0
def F(Ri,ki):
    if utilities.is_binary(ki) and len(Ri)%2 == 0 and len(ki)%2== 0:
        expander = expand(Ri)
        xor_output = utilities.xor(expander,ki)
        mid = len(xor_output)//2
        bx1 = xor_output[:mid]
        bx2 = xor_output[mid:]
        sx1 = sbox1(bx1)
        sx2 = sbox2(bx2)
        Ri2 = sx1+sx2
        return Ri2
    elif not utilities.is_binary(ki) or len(ki)%2!= 0:
        print('Error(F): invalid key',end='')
        return ''
    elif len(Ri)%2 != 0:
        print('Error(F): invalid input',end='')
        return ''
コード例 #3
0
ファイル: SDES.py プロジェクト: PJYGit/FT
def feistel(bi, ki):
    # your code here
    if not utilities.is_binary(ki) or len(ki) != int(
            get_SDES_value('key_size')) - 1:
        print('Error(feistel): Invalid key', end='')
        return ''
    if not utilities.is_binary(bi) or len(bi) != int(
            get_SDES_value('block_size')):
        print('Error(feistel): Invalid block', end='')
        return ''

    after_F = F(bi[int(len(bi) / 2):], ki)
    after_xor = utilities.xor(after_F, bi[:int(len(bi) / 2)])

    bi2 = bi[int(len(bi) / 2):] + after_xor

    return bi2
コード例 #4
0
ファイル: SDES.py プロジェクト: PJYGit/FT
def expand(R):
    # your code here
    if not utilities.is_binary(R) or len(R) < 6 or len(R) % 2 != 0:
        print('Error(expand): invalid input', end='')
        return ''

    mid = int(len(R) / 2)
    t1 = R[mid - 1]
    t2 = R[mid]
    output = R[:mid - 1] + t2 + t1 + t2 + t1 + R[mid + 1:]

    return output
コード例 #5
0
def decode(b,codeType):
    if not utilities.is_binary(b):
        print('Error(decode): invalid input',end ='')
        return ''
    if codeType == 'ASCII':
        c = chr(utilities.bin_to_dec(b))
    elif codeType == 'B6':
        b6 = utilities.get_B6Code()
        c = b6[utilities.bin_to_dec(b)]
    else:
        print('Error(decode): Unsupported Coding Type',end ='')
        return ''
    return c
コード例 #6
0
def expand(R):
    if not utilities.is_binary(R) or len(R)%2 != 0 or utilities.bin_to_dec(R)<6:
        print('Error(expand): invalid input',end='')
        return ''
    else: 
        mid = int(len(R)/2)-1
        i = mid 
        start = R[0:i]
        middle = R[i+1]+R[i]+R[i+1]+R[i]
        end = R[i+2:]
        output = start+middle+end
    
    return output
コード例 #7
0
ファイル: SDES.py プロジェクト: PJYGit/FT
def decode_B6(b):
    # your code here
    if not utilities.is_binary(b):
        print('Error(decode_B6): invalid input', end='')
        return ''
    if len(b) != 6:
        print('Error(decode_B6): invalid input', end='')
        return ''

    b6_code = utilities.get_B6Code()
    c = b6_code[utilities.bin_to_dec(b)]

    return c
コード例 #8
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
コード例 #9
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
コード例 #10
0
def get_subKey(key,i):
    key_size = int(get_SDES_value('key_size'))
    if not utilities.is_binary(key) or len(key) != key_size:
        print('Error(get_subKey): Invalid key',end='')
        return '' 
    elif not isinstance(i, int) or i <= 0: 
        print('Error(get_subKey): invalid i',end='')
        return ''
    else: 
        i-=1
        leftA = key[0:i]
        leftB = key[i:]
        subKey = leftB+leftA
        subKey = subKey[:len(subKey)-1]
    return subKey
コード例 #11
0
ファイル: SDES.py プロジェクト: PJYGit/FT
def F(Ri, ki):
    # your code here
    if not utilities.is_binary(ki):
        print('Error(F): invalid key', end='')
        return ''
    if len(ki) != int(get_SDES_value('key_size')) - 1:
        print('Error(F): invalid key', end='')
        return ''
    if not utilities.is_binary(Ri):
        print('Error(F): invalid input', end='')
        return ''
    if len(Ri) != int(get_SDES_value('block_size')) / 2:
        print('Error(F): invalid input', end='')
        return ''

    expanded = expand(Ri)
    xored = utilities.xor(expanded, ki)

    b1 = sbox1(xored[:int(len(xored) / 2)])
    b2 = sbox2(xored[int(len(xored) / 2):])

    Ri2 = b1 + b2

    return Ri2
コード例 #12
0
def feistel(bi,ki):
    if len(bi)%2!= 0:
        print('Error(feistel): Invalid block',end='')
        return ''
    elif not utilities.is_binary(ki) or len(ki)%2!= 0:
        print('Error(feistel): Invalid key',end='')
        return ''
    else:
        # print(bi, ki)
        m = int(len(bi)//2)
        Li = bi[:m]
        Ri = bi[m:]
        Li1 = Ri 
        Ri1 = utilities.xor(Li, F(Ri,ki))
        bi2 = Li1 + Ri1
    return bi2
コード例 #13
0
ファイル: SDES.py プロジェクト: PJYGit/FT
def decode(b, codeType):
    # your code here
    if not utilities.is_binary(b):
        print('Error(decode): invalid input', end='')
        return ''
    if not isinstance(codeType, str):
        print('Error(decode): Unsupported Coding Type', end='')
        return ''
    if str(codeType) != 'B6' and str(codeType) != 'ASCII':
        print('Error(decode): Unsupported Coding Type', end='')
        return ''

    if codeType == 'ASCII':
        c = chr(utilities.bin_to_dec(b))
    else:
        c = decode_B6(b)

    return c
コード例 #14
0
ファイル: SDES.py プロジェクト: PJYGit/FT
def get_subKey(key, i):
    # your code here
    if not utilities.is_binary(key):
        print('Error(get_subKey): Invalid key', end='')
        return ''
    if len(key) != int(get_SDES_value('key_size')):
        print('Error(get_subKey): Invalid key', end='')
        return ''
    if not isinstance(i, int):
        print('Error(get_subKey): invalid i', end='')
        return ''
    if i <= 0:
        print('Error(get_subKey): invalid i', end='')
        return ''

    if i == 1:
        subKey = key[:-1]
    else:
        subKey = key[i - 1:] + key[:i - 2]

    return subKey
コード例 #15
0
ファイル: SDES.py プロジェクト: PJYGit/FT
def sbox2(R):
    # your code here
    if get_SDES_value('block_size') == '':
        print('Error(sbox2): undefined block size', end='')
        return ''
    if not utilities.is_binary(R):
        print('Error(sbox2): invalid input', end='')
        return ''
    if len(R) != int(int(get_SDES_value('block_size')) / 4 + 1):
        print('Error(sbox2): invalid input', end='')
        return ''

    sbox2_file = open(sbox2File, 'r')
    sbox2_list = sbox2_file.read().split('\n')

    if len(R) - 1 > len(sbox2_list):
        print('Error(sbox2): undefined sbox2', end='')
        return ''

    box_2d = []
    for box in sbox2_list:
        box = box[box.find(':') + 1:]
        box_2d.append(box.split(','))

    row = int(utilities.bin_to_dec(R[0]))
    column = int(utilities.bin_to_dec(R[1:]))

    temp = 2**(len(R) - 1)

    try:
        output = box_2d[len(R) - 2][row * temp + column]
    except IndexError:
        print('Error(sbox2): undefined sbox2', end='')
        return ''

    return output