示例#1
0
    def _test_encrypt():
        data = "\x00" * 14
        key = ("\x00" * 15)
        outputs = []
        for key_count, key_byte in enumerate(range(256)):
            _key = bytearray(key + chr(key_byte))
            key_parity = xor_parity(_key)
            pride.functions.utilities.print_in_place(
                str(key_count / 256.0) + '% complete; Current bias: {}'.format(
                    float(outputs.count(1)) / (outputs.count(0) or 1)))
            for count in range(65535):
                _data = bytearray(
                    data + cast(cast(count, "binary").zfill(16), "bytes"))
                plaintext = _data[:]
                #  print len(_data), count
                encrypt_block(_data, _key)
                ciphertext = _data[:]
                plaintext_parity = xor_parity(plaintext)
                ciphertext_parity = xor_parity(ciphertext)
                outputs.append(1 if plaintext_parity
                               ^ ciphertext_parity == key_parity else 0)

        zero_bits = outputs.count(0)
        one_bits = outputs.count(1)
        print float(one_bits) / zero_bits, one_bits, zero_bits
示例#2
0
def ctr_mode(block, iv, key, cipher, tag=None, tweak=None):
    cipher(iv, key, tag, tweak)
    xor_subroutine(block, iv)
    replacement_subroutine(
        iv,
        bytearray(cast(
            cast(cast(bytes(iv), "binary"), "integer") + 1, "bytes")))
示例#3
0
文件: speck.py 项目: overgter/crypto
def test_speck_round():
    left = right = cast(cast("\x01" * 3, "binary"), "integer")
    leftkey = rightkey = left

    #  print left, right
    left, right = speck_round(left, right, leftkey, 2**(3 * 8))
    #  print left, right
    left, right = invert_speck_round(left, right, leftkey, 2**(3 * 8))
示例#4
0
文件: speck.py 项目: erose1337/crypto
def test_speck_round():
    left = right = cast(cast("\x01" * 3, "binary"), "integer")
    leftkey = rightkey = left
    
  #  print left, right
    left, right = speck_round(left, right, leftkey, 2 ** (3 * 8))
  #  print left, right
    left, right = invert_speck_round(left, right, leftkey, 2 ** (3 * 8))
示例#5
0
def rotational_difference(input_one, input_two):
    if input_one == input_two:
        return 0

    input_one_bits = cast(input_one, "binary")
    input_two_bits = cast(input_two, "binary")
    if input_one_bits.count('1') == input_two_bits.count('1'):
        for rotation_amount in range(1, 8):
            if rotate(input_one_bits, rotation_amount) == input_two_bits:
                return rotation_amount
示例#6
0
文件: speck.py 项目: erose1337/crypto
 def encrypt_block(self, data, key):     
     left, right, leftkey, rightkey, modulus, round_info = self._crypt_block(data, key)      
     speck_round(left, right, leftkey, modulus)        
     print left, right
     for round in range(1, 4):#NUMBER_OF_ROUNDS[round_info]):
         leftkey, rightkey = speck_round(rightkey, leftkey, round, modulus)
         left, right = speck_round(left, right, leftkey, modulus)
         print left, right
     replacement_subroutine(data, cast(left, "bytes") + cast(right, "bytes")) 
     print left, right
     print data
     print word_to_integer(data[:len(data) / 2]), word_to_integer(data[len(data) / 2:])
示例#7
0
文件: speck.py 项目: erose1337/crypto
 def decrypt_block(self, data, key):   
     print "Decrypting\n", data
     left, right, leftkey, rightkey, modulus, round_info = self._crypt_block(data, key)   
     print left, right
     for round_number in range(1, 4):#NUMBER_OF_ROUNDS[round_info]):            
         leftkey, rightkey = speck_round(rightkey, leftkey, round_number, modulus)
     
     print left, right
     for round in reversed(range(1, 4)):#NUMBER_OF_ROUNDS[round_info])):
         left, right = invert_speck_round(left, right, leftkey, modulus)
         print left, right
         leftkey, rightkey = invert_speck_round(rightkey, leftkey, round, modulus)
     left, right = invert_speck_round(left, right, leftkey, modulus)
     replacement_subroutine(data, cast(left, "bytes") + cast(right, "bytes"))
示例#8
0
文件: speck.py 项目: overgter/crypto
 def encrypt_block(self, data, key):
     left, right, leftkey, rightkey, modulus, round_info = self._crypt_block(
         data, key)
     speck_round(left, right, leftkey, modulus)
     print left, right
     for round in range(1, 4):  #NUMBER_OF_ROUNDS[round_info]):
         leftkey, rightkey = speck_round(rightkey, leftkey, round, modulus)
         left, right = speck_round(left, right, leftkey, modulus)
         print left, right
     replacement_subroutine(data,
                            cast(left, "bytes") + cast(right, "bytes"))
     print left, right
     print data
     print word_to_integer(data[:len(data) / 2]), word_to_integer(
         data[len(data) / 2:])
示例#9
0
文件: speck.py 项目: overgter/crypto
    def decrypt_block(self, data, key):
        print "Decrypting\n", data
        left, right, leftkey, rightkey, modulus, round_info = self._crypt_block(
            data, key)
        print left, right
        for round_number in range(1, 4):  #NUMBER_OF_ROUNDS[round_info]):
            leftkey, rightkey = speck_round(rightkey, leftkey, round_number,
                                            modulus)

        print left, right
        for round in reversed(range(1, 4)):  #NUMBER_OF_ROUNDS[round_info])):
            left, right = invert_speck_round(left, right, leftkey, modulus)
            print left, right
            leftkey, rightkey = invert_speck_round(rightkey, leftkey, round,
                                                   modulus)
        left, right = invert_speck_round(left, right, leftkey, modulus)
        replacement_subroutine(data,
                               cast(left, "bytes") + cast(right, "bytes"))
示例#10
0
 def _test_encrypt():
     data = "\x00" * 14
     key = ("\x00" * 15)  
     outputs = []
     for key_count, key_byte in enumerate(range(256)):
         _key = bytearray(key + chr(key_byte))
         key_parity = xor_parity(_key)
         pride.functions.utilities.print_in_place(str(key_count / 256.0) + '% complete; Current bias: {}'.format(float(outputs.count(1)) / (outputs.count(0) or 1)))
         for count in range(65535):            
             _data = bytearray(data + cast(cast(count, "binary").zfill(16), "bytes"))
             plaintext = _data[:]
         #  print len(_data), count
             encrypt_block(_data, _key)
             ciphertext = _data[:]
             plaintext_parity = xor_parity(plaintext)        
             ciphertext_parity = xor_parity(ciphertext)
             outputs.append(1 if plaintext_parity ^ ciphertext_parity == key_parity else 0)
 
     zero_bits = outputs.count(0)
     one_bits = outputs.count(1)
     print float(one_bits) / zero_bits, one_bits, zero_bits    
示例#11
0
def p_box(input_bytes):    
    """ Data concentrating permutation box. Evenly distributes input bits amongst output. """
    bits = cast(bytes(input_bytes), "binary")      
    # if a 64 bit block was acceptable, the operation would be this simple:
    #   for index, byte in enumerate(int(bits[index::8], 2) for index in range(8)):
    #       input_bytes[index] = byte  
    
    bit_count = len(bits)
    word_size = bit_count / 8
    word_size_in_bytes = word_size / 8
    for index in range(8):
        bits_at_index = bits[index::word_size]
        _index = index * word_size_in_bytes    
        
        for offset, _bits in enumerate(slide(bits_at_index, 8)):   
            input_bytes[_index + offset] = int(_bits, 2)
示例#12
0
def p_box(input_bytes):
    """ Data concentrating permutation box. Evenly distributes input bits amongst output. """
    bits = cast(bytes(input_bytes), "binary")
    # if a 64 bit block was acceptable, the operation would be this simple:
    #   for index, byte in enumerate(int(bits[index::8], 2) for index in range(8)):
    #       input_bytes[index] = byte

    bit_count = len(bits)
    word_size = bit_count / 8
    word_size_in_bytes = word_size / 8
    for index in range(8):
        bits_at_index = bits[index::word_size]
        _index = index * word_size_in_bytes

        for offset, _bits in enumerate(slide(bits_at_index, 8)):
            input_bytes[_index + offset] = int(_bits, 2)
示例#13
0
文件: speck.py 项目: overgter/crypto
def word_to_integer(word):
    return cast(cast(bytes(word), "binary"), "integer")
示例#14
0
文件: speck.py 项目: erose1337/crypto
def word_to_integer(word):
    return cast(cast(bytes(word), "binary"), "integer")
示例#15
0
def exotic_padding(hash_input, rate):
    hash_input += '1'
    while len(hash_input) < rate: 
        hash_input = cast(unpack_factors(cast(hash_input, "binary")), "bytes")
    return hash_input
示例#16
0
def exotic_padding(hash_input, rate):
    hash_input += '1'
    while len(hash_input) < rate:
        hash_input = cast(unpack_factors(cast(hash_input, "binary")), "bytes")
    return hash_input
示例#17
0
def pbox(word):
    binary_word = cast(word, "binary")
    return int(''.join(binary_word[offset::8] for offset in range(8)), 2)