Ejemplo n.º 1
0
def do_counter_mode(k, iv, x, blocksize, ivsize):

    aes = AES_ENCRYPT(k)
    long_k = bytearray()
    n = ceil(len(x) / blocksize)

    for i in range(0, n):
        c = aes((iv + i).to_bytes(ivsize, "big"))
        long_k += c

    return xor(x, long_k[:len(x)])
Ejemplo n.º 2
0
def build_key(key, c1, c2, c3):

    xm12, xm13,  xm23 = xor(c1, c2), xor(c1, c3), xor(c2, c3)

    for i in range(0, len(c1)):

        if key[i] != 0:
            continue

        if c1[i] == c2[i] or c1[i] == c3[i] or c2[i] == c3[i]:
            continue

        m12, m13, m23 = xm12[i], xm13[i], xm23[i]

        if within_range(m13) and within_range(m23):
            key[i] = ord(" ") ^ c3[i]
        elif within_range(m12) and within_range(m23):
            key[i] = ord(" ") ^ c2[i]
        elif within_range(m12) and within_range(m13):
            key[i] = ord(" ") ^ c1[i]
Ejemplo n.º 3
0
def decrypt_cbc(msg, key, iv):
    cipher = AES.new(key, AES.MODE_ECB)
    blocks = [msg[i:i + 32] for i in range(0, len(msg), 32)]
    decrypted = ''
    prev = str2hex(iv)

    for block in blocks:
        dec = cipher.decrypt(block.decode('hex')).encode('hex')
        d = xor(prev, dec)
        prev = block
        decrypted += d

    return decrypted
Ejemplo n.º 4
0
def encrypt_cbc(msg, key, iv):
    cipher = AES.new(key, AES.MODE_ECB)
    msg = pad(msg, 16)
    msg = str2hex(msg)
    blocks = [msg[i:i + 32] for i in range(0, len(msg), 32)]
    encrypted = ''
    en = str2hex(iv)

    for block in blocks:
        ip = xor(en, block)
        en = cipher.encrypt(ip.decode('hex')).encode('hex')
        encrypted += en

    return encrypted
Ejemplo n.º 5
0
def encrypt_cbc(msg, key, iv='\x00' * 16, is_base64=False):
    cipher = AES.new(key, AES.MODE_ECB)

    if is_base64:
        msg = base64.b64decode(msg)

    msg = pad(msg, 16)
    blocks = [msg[i:i + 16] for i in range(0, len(msg), 16)]
    encrypted = ''
    en = iv

    for block in blocks:
        ip = xor(en, block)
        en = cipher.encrypt(ip)
        encrypted += en

    return encrypted
Ejemplo n.º 6
0
def decrypt_cbc(ct_string, k_string):

    ct, k = bytes.fromhex(ct_string), bytes.fromhex(k_string)
    assert len(ct) % BLOCKSIZE == 0, "Invalid format"

    n = len(ct) // BLOCKSIZE
    aes_decrypt = AES_DECRYPT(k)
    m = bytearray()

    for i in range(n - 1):
        start, mid, end = i * BLOCKSIZE, (i + 1) * BLOCKSIZE, (i +
                                                               2) * BLOCKSIZE
        cx, cy = ct[start:mid], ct[mid:end]
        d = aes_decrypt(cy)
        m += xor(cx, d)

    return unpad(m)
Ejemplo n.º 7
0
def decrypt_cbc(msg, key, iv='\x00' * 16, is_base64=False):
    cipher = AES.new(key, AES.MODE_ECB)

    if is_base64:
        print('Decoding base64...')
        msg = base64.b64decode(msg)

    blocks = [msg[i:i + 16] for i in range(0, len(msg), 16)]
    decrypted = ''
    prev = iv

    for block in blocks:
        dec = cipher.decrypt(block)
        d = xor(prev, dec)
        prev = block
        decrypted += d

    return decrypted
Ejemplo n.º 8
0
def encrypt_cbc(pt_string, k_string, blocksize=16):

    pt, k = bytearray(pt_string, 'utf-8'), bytes.fromhex(k_string)
    pad(pt)
    print(pt)
    n = len(pt) // BLOCKSIZE
    current = urandom(BLOCKSIZE)  #IV
    aes_encrypt = AES_ENCRYPT(k)
    ct = bytearray(current)

    for i in range(n):
        start, end = i * blocksize, (i + 1) * blocksize
        m = pt[start:end]
        d = xor(m, current)
        current = aes_encrypt(d)
        ct += current

    print(len(ct))
    return ct.hex()
Ejemplo n.º 9
0
    def __init__(self, buffer):
        self.packet = buffer[0:74]
        self.frame_sync = self.packet[0:11]
        self.data_block = self.packet[11:56]
        self.batt_cond = (self.packet[13:15][::-1])
        self.message_type = self.packet[15:18]
        self.unit_addr = int((self.packet[18:35][::-1]), 2)
        self.pressure = int((self.packet[35:42][::-1]), 2)
        self.batt_charge = \
            ("{}%".format(int(int((self.packet[42:49][::-1]), 2) / 127 * 100)))
        self.spare = self.packet[49]
        self.valve_ckt_stat = self.packet[50]
        self.conf_ind = self.packet[51]
        self.turbine = self.packet[52]
        self.motion = self.packet[53]
        self.mkr_batt = self.packet[54]
        self.mkr_light = self.packet[55]
        self.checkbitsRx = self.packet[56:74]

        self.batt_cond_dict = {
            "11": "OK",
            "10": "Low",
            "01": "Very Low",
            "00": "Not Monitored"
        }
        self.batt_cond_text = self.batt_cond_dict[self.batt_cond]

        if (self.message_type == "111"):
            if (self.conf_ind == "0"):
                self.arm_status = "Arming"
            else:
                self.arm_status = "Armed"
        else:
            self.arm_status = "Normal"

        self.generator = '1111001101000001111'  # BCH generator polynomial
        self.cipher_key = '101011011101110000'  # XOR cipher key
        self.data_block = helpers.reverse(self.data_block)
        self.checkbits = helpers.checkbits(self.data_block, self.generator)
        self.checkbits_cipher = helpers.xor(self.checkbits, self.cipher_key)
        self.valid = (self.checkbits_cipher == self.checkbitsRx)  # a match?
Ejemplo n.º 10
0
#!/usr/bin/env python3
import sys
import binascii
sys.path.append('..')
from helpers import abort, xor

if __name__ == "__main__":
  if len(sys.argv) == 3 and not sys.argv[1] == '-h':
    i1 = binascii.unhexlify(sys.argv[1])
    i2 = binascii.unhexlify(sys.argv[2])
    xored = xor(i1, i2)
    print(str(binascii.hexlify(xored), 'ascii'))
  elif len(sys.argv) == 4 and sys.argv[1] == '-h':
    i1 = binascii.unhexlify(sys.argv[2])
    i2 = binascii.unhexlify(sys.argv[3])
    xored = xor(i1, i2)
    print(str(xored, 'ascii'))
  else:
    abort(f'{sys.argv[0]}: [-h] input1 input2')