示例#1
0
def des_string_to_key(str_key):
    # Result variable
    key = bytearray(8)
    # Doing some bit shifting, please refer to the URL above
    for index in range(0,len(str_key)):
        # This is not mandatory as Python seems to handle unsigned bytes by default
        b_val = ord(str_key[index]) & 0xff
        if (index % 16) < 8:
            key[index % 8] = key[index % 8] ^ (b_val<<1)
        else:
            b_val = ((b_val << 4) & 0xf0) | (ror(b_val,4) & 0x0f)
            b_val = ((b_val << 2) & 0xcc) | (ror(b_val,2) & 0x33)
            b_val = ((b_val << 1) & 0xaa) | (ror(b_val,1) & 0x55)
            key[7- (index % 8)] ^= b_val;
        index = index + 1
    # Getting odd parity
    key = set_odd_parity(key)
    # Computing CBC checksum (generating key)
    key = des_cbc_checksum(str_key, key, key)
    # Getting odd parity
    key = set_odd_parity(key)
    return key
示例#2
0
def des_string_to_2keys(str_key):
    key1 = bytearray(8)
    key2 = bytearray(8)
    for index in range(0,len(str_key)):
        # This is not mandatory as Python seems to handle unsigned bytes by default
        b_val = ord(str_key[index]) & 0xff
        # Doing some bit shifting, please refer to the URL above
        if (index % 32) < 16:
            if (index % 16) < 8:
                key1[index % 8] ^= (b_val<<1)
            else:
                key2[index % 8] ^= (b_val<<1)
        else:
            b_val = ((b_val << 4) & 0xf0) | (ror(b_val,4) & 0x0f)
            b_val = ((b_val << 2) & 0xcc) | (ror(b_val,2) & 0x33)
            b_val = ((b_val << 1) & 0xaa) | (ror(b_val,1) & 0x55)
            if (index % 16) < 8:
                key1[7-(index % 8)] ^= b_val
            else:
                key2[7-(index % 8)] ^= b_val
        if len(str_key)<=8:
            for index in range(0,8):
                key2[index] = key1[index]
        # Getting odd parity
        key1 = set_odd_parity(key1)
        # Computing CBC checksum (generating key)
        key1 = des_cbc_checksum(str_key, key1, key1)
        # Getting odd parity
        key1 = set_odd_parity(key1)
        
        # Getting odd parity
        key2 = set_odd_parity(key2)
        # Computing CBC checksum (generating key)
        key2 = des_cbc_checksum(str_key, key2, key2)
        # Getting odd parity
        key2 = set_odd_parity(key2)
        
        return key1.extend(key2).extend(key1)