Python Crypto.Cipher.DES.MODE_CBC Examples

The following are 30 code examples of Crypto.Cipher.DES.MODE_CBC(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module Crypto.Cipher.DES , or try the search function .
Example #1
Source File: encryptionencoding.py    From chepy with GNU General Public License v3.0 12 votes vote down vote up
def triple_des_encrypt(
        self,
        key: str,
        iv: str = "0000000000000000",
        mode: str = "CBC",
        hex_key: bool = False,
        hex_iv: bool = True,
    ):
        """Encrypt raw state with Triple DES

        Triple DES applies DES three times to each block to increase key size. Key: 
        Triple DES uses a key length of 24 bytes (192 bits).<br>DES uses a key length 
        of 8 bytes (64 bits).<br><br>You can generate a password-based key using one 
        of the KDF operations. IV: The Initialization Vector should be 8 bytes long. 
        If not entered, it will default to 8 null bytes. Padding: In CBC and ECB 
        mode, PKCS#7 padding will be used.
        
        Args:
            key (str): Required. The secret key
            iv (str, optional): IV for certain modes only. Defaults to '0000000000000000'.
            mode (str, optional): Encryption mode. Defaults to 'CBC'.
            hex_key (bool, optional): If the secret key is a hex string. Defaults to False.
            hex_iv (bool, optional): If the IV is a hex string. Defaults to True.
        
        Returns:
            Chepy: The Chepy object. 

        Examples:
            >>> Chepy("some data").triple_des_encrypt("super secret password !!", mode="ECB").o
            b"f8b27a0d8c837edc8fb00ea85f502fb4"
        """

        self.__check_mode(mode)

        key, iv = self._convert_key(key, iv, hex_key, hex_iv)

        if mode == "CBC":
            cipher = DES3.new(key, mode=DES3.MODE_CBC, iv=iv)
            self.state = cipher.encrypt(pad(self._convert_to_bytes(), 8))
            return self
        elif mode == "ECB":
            cipher = DES3.new(key, mode=DES3.MODE_ECB)
            self.state = cipher.encrypt(pad(self._convert_to_bytes(), 8))
            return self
        elif mode == "CTR":
            cipher = DES3.new(key, mode=DES3.MODE_CTR, nonce=b"")
            self.state = cipher.encrypt(self._convert_to_bytes())
            return self
        elif mode == "OFB":
            cipher = DES3.new(key, mode=DES3.MODE_OFB, iv=iv)
            self.state = cipher.encrypt(self._convert_to_bytes())
            return self 
Example #2
Source File: test_CBC.py    From android_universal with MIT License 6 votes vote down vote up
def test_aes_256(self):
        key =           '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'
        iv =            '000102030405060708090a0b0c0d0e0f'
        plaintext =     '6bc1bee22e409f96e93d7e117393172a' +\
                        'ae2d8a571e03ac9c9eb76fac45af8e51' +\
                        '30c81c46a35ce411e5fbc1191a0a52ef' +\
                        'f69f2445df4f9b17ad2b417be66c3710'
        ciphertext =    'f58c4c04d6e5f1ba779eabfb5f7bfbd6' +\
                        '9cfc4e967edb808d679f777bc6702c7d' +\
                        '39f23369a9d9bacfa530e26304231461' +\
                        'b2eb05e2c39be9fcda6c19078c6a9d1b'

        key = unhexlify(key)
        iv = unhexlify(iv)
        plaintext = unhexlify(plaintext)
        ciphertext = unhexlify(ciphertext)

        cipher = AES.new(key, AES.MODE_CBC, iv)
        self.assertEqual(cipher.encrypt(plaintext), ciphertext)
        cipher = AES.new(key, AES.MODE_CBC, iv)
        self.assertEqual(cipher.decrypt(ciphertext), plaintext) 
Example #3
Source File: test_CBC.py    From android_universal with MIT License 6 votes vote down vote up
def test_aes_192(self):
        key =           '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'
        iv =            '000102030405060708090a0b0c0d0e0f'
        plaintext =     '6bc1bee22e409f96e93d7e117393172a' +\
                        'ae2d8a571e03ac9c9eb76fac45af8e51' +\
                        '30c81c46a35ce411e5fbc1191a0a52ef' +\
                        'f69f2445df4f9b17ad2b417be66c3710'
        ciphertext =    '4f021db243bc633d7178183a9fa071e8' +\
                        'b4d9ada9ad7dedf4e5e738763f69145a' +\
                        '571b242012fb7ae07fa9baac3df102e0' +\
                        '08b0e27988598881d920a9e64f5615cd'

        key = unhexlify(key)
        iv = unhexlify(iv)
        plaintext = unhexlify(plaintext)
        ciphertext = unhexlify(ciphertext)

        cipher = AES.new(key, AES.MODE_CBC, iv)
        self.assertEqual(cipher.encrypt(plaintext), ciphertext)
        cipher = AES.new(key, AES.MODE_CBC, iv)
        self.assertEqual(cipher.decrypt(ciphertext), plaintext) 
Example #4
Source File: test_CBC.py    From android_universal with MIT License 6 votes vote down vote up
def test_aes_128(self):
        key =           '2b7e151628aed2a6abf7158809cf4f3c'
        iv =            '000102030405060708090a0b0c0d0e0f'
        plaintext =     '6bc1bee22e409f96e93d7e117393172a' +\
                        'ae2d8a571e03ac9c9eb76fac45af8e51' +\
                        '30c81c46a35ce411e5fbc1191a0a52ef' +\
                        'f69f2445df4f9b17ad2b417be66c3710'
        ciphertext =    '7649abac8119b246cee98e9b12e9197d' +\
                        '5086cb9b507219ee95db113a917678b2' +\
                        '73bed6b8e3c1743b7116e69e22229516' +\
                        '3ff1caa1681fac09120eca307586e1a7'

        key = unhexlify(key)
        iv = unhexlify(iv)
        plaintext = unhexlify(plaintext)
        ciphertext = unhexlify(ciphertext)

        cipher = AES.new(key, AES.MODE_CBC, iv)
        self.assertEqual(cipher.encrypt(plaintext), ciphertext)
        cipher = AES.new(key, AES.MODE_CBC, iv)
        self.assertEqual(cipher.decrypt(ciphertext), plaintext) 
Example #5
Source File: SecurePacket.py    From asterix with GNU Lesser General Public License v2.1 6 votes vote down vote up
def encrypt(self, data):
        """Encrypt sensitive data by KIK.
For (3)DES, data must be padded to BS.
For AES, if data not BS-alligned, they are padded by '80..00'"""
        l = len(data)
        if self.zAES:
            l %= 16
            if l > 0:
                data += '\x80' + '\0'*(15-l)
            key = AES.new(self.keyValue, AES.MODE_CBC, IV='\0'*16)
        else:
            # suppose 8B aligned data
            assert l % 8 == 0
            # for (3)DES KIK, ECB is used
            # KeyType.DES_IMPLICIT is supposed to be 3DES ECB
            if self.keyType in (KeyType.TDES_CBC, KeyType.DES_IMPLICIT):
                key = DES3.new(self.keyValue, DES.MODE_ECB)
            elif self.keyType in (KeyType.DES_ECB, KeyType.DES_CBC):
                key = DES.new(self.keyValue, DES.MODE_ECB)
            else:
                raise ValueError("Unknown key type %02X" % self.keyType)
        return key.encrypt(data) 
Example #6
Source File: SecurePacket.py    From asterix with GNU Lesser General Public License v2.1 6 votes vote down vote up
def sign(self, data):
        """
Sign data (as str) by KID key.
Return signature as str."""
        if self.zAES:
            data = [ord(x) for x in data]
            sLB = len(data) % self.BS
            if(sLB > 0 or len(data) == 0):
                data += [0x80] + [0]*(self.BS-sLB-1)
                xorkey = self.xorKey2
            else:
                xorkey = self.xorKey1
            for i in xrange(self.BS):
                data[-self.BS+i] ^= ord(xorkey[i])
            cipher = AES.new(self.keyval, AES.MODE_CBC, IV='\0'*16)
            data = ''.join([chr(x) for x in data])
            sig = cipher.encrypt(data)[-self.BS:]
            return sig[:self.TlenB]
        else:
            padlen = len(data) % self.BS
            if padlen > 0:
                padlen = self.BS - padlen
            sig = self.cipher(data + '\0'*padlen, True)
            return sig[-self.BS:] 
Example #7
Source File: SecurePacket.py    From asterix with GNU Lesser General Public License v2.1 6 votes vote down vote up
def __init__(self, iKICD):
        """ Constructor for KIC/KID object.
 iKICD - coding of KIC or KID (see ETSI 102.225, 5.1.2 and 5.1.3, u8)"""
        iKICD %= 0x100
        self.iKICD = iKICD
        self.keyval = None
        if iKICD & KICD.ALGO == KICD.ALGO_DES:           # DES/3DES key
            b43 = iKICD & 0x0C           # bits 4 and 3 of KIC/KID
            self.keysize = 8 + 2*(b43 % 12)  # mapping to DES, 3DES 2k, 3DES 3k
            self.cipModule = b43 % 12 and DES3 or DES
            self.MODE = b43 == KICD.DES_ECB and DES.MODE_EBC or DES.MODE_CBC
            self.BS = 8
            self.zAES = False
        elif iKICD & 0x0F == KICD.ALGO_AES:         # AES CBC / CMAC
            self.zAES = True
            self.cipModule = AES
            self.MODE = AES.MODE_CBC
            self.BS = 16
            self.TlenB = 8     # length of CMAC, you may manually change to 4
            self.irrPoly = 0x87            # for CMAC
        else:
            raise ValueError("Only DES/AES implemented for KIC/KID") 
Example #8
Source File: test_CBC.py    From FODI with GNU General Public License v3.0 6 votes vote down vote up
def test_aes_256(self):
        key =           '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'
        iv =            '000102030405060708090a0b0c0d0e0f'
        plaintext =     '6bc1bee22e409f96e93d7e117393172a' +\
                        'ae2d8a571e03ac9c9eb76fac45af8e51' +\
                        '30c81c46a35ce411e5fbc1191a0a52ef' +\
                        'f69f2445df4f9b17ad2b417be66c3710'
        ciphertext =    'f58c4c04d6e5f1ba779eabfb5f7bfbd6' +\
                        '9cfc4e967edb808d679f777bc6702c7d' +\
                        '39f23369a9d9bacfa530e26304231461' +\
                        'b2eb05e2c39be9fcda6c19078c6a9d1b'

        key = unhexlify(key)
        iv = unhexlify(iv)
        plaintext = unhexlify(plaintext)
        ciphertext = unhexlify(ciphertext)

        cipher = AES.new(key, AES.MODE_CBC, iv)
        self.assertEqual(cipher.encrypt(plaintext), ciphertext)
        cipher = AES.new(key, AES.MODE_CBC, iv)
        self.assertEqual(cipher.decrypt(ciphertext), plaintext) 
Example #9
Source File: test_CBC.py    From FODI with GNU General Public License v3.0 6 votes vote down vote up
def test_aes_192(self):
        key =           '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'
        iv =            '000102030405060708090a0b0c0d0e0f'
        plaintext =     '6bc1bee22e409f96e93d7e117393172a' +\
                        'ae2d8a571e03ac9c9eb76fac45af8e51' +\
                        '30c81c46a35ce411e5fbc1191a0a52ef' +\
                        'f69f2445df4f9b17ad2b417be66c3710'
        ciphertext =    '4f021db243bc633d7178183a9fa071e8' +\
                        'b4d9ada9ad7dedf4e5e738763f69145a' +\
                        '571b242012fb7ae07fa9baac3df102e0' +\
                        '08b0e27988598881d920a9e64f5615cd'

        key = unhexlify(key)
        iv = unhexlify(iv)
        plaintext = unhexlify(plaintext)
        ciphertext = unhexlify(ciphertext)

        cipher = AES.new(key, AES.MODE_CBC, iv)
        self.assertEqual(cipher.encrypt(plaintext), ciphertext)
        cipher = AES.new(key, AES.MODE_CBC, iv)
        self.assertEqual(cipher.decrypt(ciphertext), plaintext) 
Example #10
Source File: test_CBC.py    From FODI with GNU General Public License v3.0 6 votes vote down vote up
def test_aes_128(self):
        key =           '2b7e151628aed2a6abf7158809cf4f3c'
        iv =            '000102030405060708090a0b0c0d0e0f'
        plaintext =     '6bc1bee22e409f96e93d7e117393172a' +\
                        'ae2d8a571e03ac9c9eb76fac45af8e51' +\
                        '30c81c46a35ce411e5fbc1191a0a52ef' +\
                        'f69f2445df4f9b17ad2b417be66c3710'
        ciphertext =    '7649abac8119b246cee98e9b12e9197d' +\
                        '5086cb9b507219ee95db113a917678b2' +\
                        '73bed6b8e3c1743b7116e69e22229516' +\
                        '3ff1caa1681fac09120eca307586e1a7'

        key = unhexlify(key)
        iv = unhexlify(iv)
        plaintext = unhexlify(plaintext)
        ciphertext = unhexlify(ciphertext)

        cipher = AES.new(key, AES.MODE_CBC, iv)
        self.assertEqual(cipher.encrypt(plaintext), ciphertext)
        cipher = AES.new(key, AES.MODE_CBC, iv)
        self.assertEqual(cipher.decrypt(ciphertext), plaintext) 
Example #11
Source File: primefaces.py    From CVE-2017-1000486 with GNU General Public License v3.0 6 votes vote down vote up
def encrypt(data, password):
    # Padding clear-text using PKCS5 algo
    padding = 8 - len(data) % 8
    data += chr(padding) * padding
    # IV and "iterations count" extracted from primefaces sourcecode
    iterations = 19
    iv = b'\xa9\x9b\xc8\x32\x56\x34\xe3\x03'
    hasher = MD5.new()
    hasher.update(password)
    hasher.update(iv)
    result = hasher.digest()

    for i in range(1, iterations):
        hasher = MD5.new()
        hasher.update(result)
        result = hasher.digest()

    cipher = DES.new(result[:8], DES.MODE_CBC, result[8:16])
    encrypted = cipher.encrypt(data)
    print ("[*] Generated Encrypted Payload: " + str(base64.b64encode(encrypted)))
    return str(base64.b64encode(encrypted)) 
Example #12
Source File: des.py    From scalyr-agent-2 with Apache License 2.0 6 votes vote down vote up
def encryptData(self, encryptKey, privParameters, dataToEncrypt):
        if DES is None:
            raise error.StatusInformation(
                errorIndication=errind.encryptionError
                )

        snmpEngineBoots, snmpEngineTime, salt = privParameters
        
        # 8.3.1.1
        desKey, salt, iv = self.__getEncryptionKey(
            encryptKey, snmpEngineBoots
            )

        # 8.3.1.2
        privParameters = univ.OctetString(salt)

        # 8.1.1.2
        desObj = DES.new(desKey, DES.MODE_CBC, iv)
        plaintext =  dataToEncrypt + univ.OctetString((0,) * (8 - len(dataToEncrypt) % 8)).asOctets()
        ciphertext = desObj.encrypt(plaintext)

        # 8.3.1.3 & 4
        return univ.OctetString(ciphertext), privParameters
        
    # 8.2.4.2 
Example #13
Source File: encryptionencoding.py    From chepy with GNU General Public License v3.0 6 votes vote down vote up
def des_encrypt(
        self,
        key: str,
        iv: str = "0000000000000000",
        mode: str = "CBC",
        hex_key: bool = False,
        hex_iv: bool = True,
    ):
        """Encrypt raw state with DES

        DES is a previously dominant algorithm for encryption, and was published 
        as an official U.S. Federal Information Processing Standard (FIPS). It is 
        now considered to be insecure due to its small key size. DES uses a key 
        length of 8 bytes (64 bits).<br>Triple DES uses a key length of 24 bytes. 
        You can generate a password-based key using one of the KDF operations. 
        The Initialization Vector should be 8 bytes long. If not entered, it will 
        default to 8 null bytes. Padding: In CBC and ECB mode, PKCS#7 padding will be used.
        
        Args:
            key (str): Required. The secret key
            iv (str, optional): IV for certain modes only. Defaults to '0000000000000000'.
            mode (str, optional): Encryption mode. Defaults to 'CBC'.
            hex_key (bool, optional): If the secret key is a hex string. Defaults to False.
            hex_iv (bool, optional): If the IV is a hex string. Defaults to True.
        
        Returns:
            Chepy: The Chepy object. 

        Examples:
            >>> Chepy("some data").des_encrypt("70617373776f7264", hex_key=True).o
            b"1ee5cb52954b211d1acd6e79c598baac"

            To encrypt using a differnt mode

            >>> Chepy("some data").des_encrypt("password", mode="CTR").o
            b"0b7399049b0267d93d"
        """

        self.__check_mode(mode)

        key, iv = self._convert_key(key, iv, hex_key, hex_iv)

        if mode == "CBC":
            cipher = DES.new(key, mode=DES.MODE_CBC, iv=iv)
            self.state = cipher.encrypt(pad(self._convert_to_bytes(), 8))
            return self
        elif mode == "ECB":
            cipher = DES.new(key, mode=DES.MODE_ECB)
            self.state = cipher.encrypt(pad(self._convert_to_bytes(), 8))
            return self
        elif mode == "CTR":
            cipher = DES.new(key, mode=DES.MODE_CTR, nonce=b"")
            self.state = cipher.encrypt(self._convert_to_bytes())
            return self
        elif mode == "OFB":
            cipher = DES.new(key, mode=DES.MODE_OFB, iv=iv)
            self.state = cipher.encrypt(self._convert_to_bytes())
            return self 
Example #14
Source File: util.py    From OpenData with Apache License 2.0 6 votes vote down vote up
def aes_encrypt(key, iv, content):
    """
    AES加密
    key,iv使用同一个
    模式cbc
    填充pkcs7
    :param key: 密钥
    :param content: 加密内容
    :return:
    """
    cipher = AES.new(key, AES.MODE_CBC, iv)
    # 处理明文
    content_padding = pkcs7padding(content)
    # 加密
    encrypt_bytes = cipher.encrypt(bytes(content_padding, encoding='utf-8'))
    # 重新编码
    result = str(base64.b64encode(encrypt_bytes), encoding='utf-8')
    return result 
Example #15
Source File: util.py    From OpenData with Apache License 2.0 6 votes vote down vote up
def aes_decrypt(key, iv, content):
    """
    AES解密
     key,iv使用同一个
    模式cbc
    去填充pkcs7
    :param key:
    :param content:
    :return:
    """
    cipher = AES.new(key, AES.MODE_CBC, iv)
    # base64解码
    encrypt_bytes = base64.b64decode(content)
    # 解密
    decrypt_bytes = cipher.decrypt(encrypt_bytes)
    # 重新编码
    result = str(decrypt_bytes, encoding='utf8')
    # 去除填充内容
    result = pkcs7unpadding(result)
    return result 
Example #16
Source File: crypto.py    From CVE-2017-7494 with GNU General Public License v3.0 5 votes vote down vote up
def basic_encrypt(cls, key, plaintext):
        assert len(plaintext) % 8 == 0
        des3 = DES3.new(key.contents, AES.MODE_CBC, '\0' * 8)
        return des3.encrypt(plaintext) 
Example #17
Source File: 2.py    From OpenData with Apache License 2.0 5 votes vote down vote up
def aes_decrypt(key, iv, content):
    """
    AES解密
     key,iv使用同一个
    模式cbc
    去填充pkcs7
    :param key:
    :param content:
    :return:
    """
    cipher = AES.new(key, AES.MODE_CBC, iv)
    # base64解码
    encrypt_bytes = base64.b64decode(content)
    # 解密
    decrypt_bytes = cipher.decrypt(encrypt_bytes)
    # 重新编码
    result = str(decrypt_bytes, encoding='utf8')
    # 去除填充内容
    result = pkcs7unpadding(result)
    return result 
Example #18
Source File: nanocore.py    From bamfdetect with MIT License 5 votes vote down vote up
def decrypt_des(key, data):
    iv = key
    cipher = DES.new(key, DES.MODE_CBC, iv)
    return cipher.decrypt(data) 
Example #19
Source File: nanocore.py    From bamfdetect with MIT License 5 votes vote down vote up
def decrypt_aes(key, iv, data):
    mode = AES.MODE_CBC
    cipher = AES.new(key, mode, IV=iv)
    return cipher.decrypt(data) 
Example #20
Source File: 2.py    From OpenData with Apache License 2.0 5 votes vote down vote up
def aes_encrypt(key, iv, content):
    """
    AES加密
    key,iv使用同一个
    模式cbc
    填充pkcs7
    :param key: 密钥
    :param content: 加密内容
    :return:
    """
    cipher = AES.new(key, AES.MODE_CBC, iv)
    # 处理明文
    content_padding = pkcs7padding(content)
    # 加密
    encrypt_bytes = cipher.encrypt(bytes(content_padding, encoding='utf-8'))
    # 重新编码
    result = str(base64.b64encode(encrypt_bytes), encoding='utf-8')
    return result 
Example #21
Source File: crypto.py    From CVE-2017-7494 with GNU General Public License v3.0 5 votes vote down vote up
def basic_encrypt(cls, key, plaintext):
        assert len(plaintext) >= 16
        aes = AES.new(key.contents, AES.MODE_CBC, '\0' * 16)
        ctext = aes.encrypt(_zeropad(plaintext, 16))
        if len(plaintext) > 16:
            # Swap the last two ciphertext blocks and truncate the
            # final block to match the plaintext length.
            lastlen = len(plaintext) % 16 or 16
            ctext = ctext[:-32] + ctext[-16:] + ctext[-32:-16][:lastlen]
        return ctext 
Example #22
Source File: crypto.py    From CVE-2017-7494 with GNU General Public License v3.0 5 votes vote down vote up
def basic_decrypt(cls, key, ciphertext):
        assert len(ciphertext) % 8 == 0
        des3 = DES3.new(key.contents, AES.MODE_CBC, '\0' * 8)
        return des3.decrypt(ciphertext) 
Example #23
Source File: SCP02.py    From asterix with GNU Lesser General Public License v2.1 5 votes vote down vote up
def wrapAPDU(self, apdu):
        """ Wrap APDU for SCP02, i.e. calculate MAC and encrypt.
Input APDU and output APDU are list of uint8. """
        lc = len(apdu) - 5
        assert len(apdu) >= 5, "Wrong APDU length: %d" % len(apdu)
        assert len(apdu) == 5 or apdu[4] == lc, \
            "Lc differs from length of data: %d vs %d" % (apdu[4], lc)

        cla = apdu[0]
        b8 = cla & 0x80
        if cla & 0x03 > 0 or cla & 0x40 != 0:
            # nonzero logical channel in APDU, check that are the same
            assert cla == self.CLA(False, b8), "CLA mismatch"
        sapdu = l2s(apdu)
        # CLA without log. channel can be 80 or 00 only
        if self.isCMAC:
            if self.i & M_CMAC_MODIF:  # CMAC on unmodified APDU
                mlc = lc
                clac = chr(b8)
            else:                      # CMAC on modified APDU
                mlc = lc + 8
                clac = chr(b8 + 0x04)
            mac = self.calcMAC_1d(clac + sapdu[1:4] + chr(mlc) + sapdu[5:])
            mac = [ord(x) for x in mac]
            if self.isENC:
                k = DES3.new(self.ses_ENC, DES.MODE_CBC, ZERO8)
                data = s2l(k.encrypt(pad80(sapdu[5:], 8)))
                lc = len(data)
            else:
                data = apdu[5:]
            lc += 8
            apdu = [self.CLA(True, b8)] + apdu[1:4] + [lc] + data + mac
        return apdu 
Example #24
Source File: SCP02.py    From asterix with GNU Lesser General Public License v2.1 5 votes vote down vote up
def deriveKeys(self, card_challenge):
        """ Derive session keys and calculate host_ and card_ cryptograms."""

        # session keys derivation
        k = DES3.new(self.i & M_BASEKEY and self.base_S_MAC or self.base_KEY,
                     DES.MODE_CBC, IV=ZERO8)
        self.ses_C_MAC = k.encrypt(unhexlify("0101") + self.seqCounter +
                                   ZERO12)
        k = DES3.new(self.i & M_BASEKEY and self.base_S_MAC or self.base_KEY,
                     DES.MODE_CBC, IV=ZERO8)
        self.ses_R_MAC = k.encrypt(unhexlify("0102") + self.seqCounter +
                                   ZERO12)

        k = DES3.new(self.i & M_BASEKEY and self.base_DEK or self.base_KEY,
                     DES.MODE_CBC, IV=ZERO8)
        self.ses_DEK = k.encrypt(unhexlify("0181") + self.seqCounter + ZERO12)

        k = DES3.new(self.i & M_BASEKEY and self.base_S_ENC or self.base_KEY,
                     DES.MODE_CBC, IV=ZERO8)
        self.ses_ENC = k.encrypt(unhexlify("0182") + self.seqCounter + ZERO12)

        # key for MAC encryption
        if self.i & M_ICV_ENC:
            self.k_icv = DES.new(self.ses_C_MAC[:8], DES.MODE_ECB)

        # card cryptogram calculation
        if self.i & M_PSEUDO:
            self.card_challenge = self.calcMAC_1d(self.SD_AID, True)[:6]
        else:
            assert len(card_challenge) == 6,\
                "Wrong length or missing card challenge (mandatory)"
            self.card_challenge = card_challenge

        self.host_cryptogram = self.calcMAC_3d(self.seqCounter +
                                               self.card_challenge +
                                               self.host_challenge)
        self.card_cryptogram = self.calcMAC_3d(self.host_challenge +
                                               self.seqCounter +
                                               self.card_challenge) 
Example #25
Source File: Transform.py    From deprecated-binaryninja-python with GNU General Public License v2.0 5 votes vote down vote up
def populate_transform_menu(menu, obj, action_table):
	aes_menu = menu.addMenu("AES")
	aes_ecb_menu = aes_menu.addMenu("ECB mode")
	aes_cbc_menu = aes_menu.addMenu("CBC mode")
	action_table[aes_ecb_menu.addAction("Encrypt")] = lambda: obj.transform_with_key(lambda data, key: aes_encrypt_transform(data, key, AES.MODE_ECB, ""))
	action_table[aes_ecb_menu.addAction("Decrypt")] = lambda: obj.transform_with_key(lambda data, key: aes_decrypt_transform(data, key, AES.MODE_ECB, ""))
	action_table[aes_cbc_menu.addAction("Encrypt")] = lambda: obj.transform_with_key_and_iv(lambda data, key, iv: aes_encrypt_transform(data, key, AES.MODE_CBC, iv))
	action_table[aes_cbc_menu.addAction("Decrypt")] = lambda: obj.transform_with_key_and_iv(lambda data, key, iv: aes_decrypt_transform(data, key, AES.MODE_CBC, iv))

	blowfish_menu = menu.addMenu("Blowfish")
	blowfish_ecb_menu = blowfish_menu.addMenu("ECB mode")
	blowfish_cbc_menu = blowfish_menu.addMenu("CBC mode")
	action_table[blowfish_ecb_menu.addAction("Encrypt")] = lambda: obj.transform_with_key(lambda data, key: blowfish_encrypt_transform(data, key, Blowfish.MODE_ECB, ""))
	action_table[blowfish_ecb_menu.addAction("Decrypt")] = lambda: obj.transform_with_key(lambda data, key: blowfish_decrypt_transform(data, key, Blowfish.MODE_ECB, ""))
	action_table[blowfish_cbc_menu.addAction("Encrypt")] = lambda: obj.transform_with_key_and_iv(lambda data, key, iv: blowfish_encrypt_transform(data, key, Blowfish.MODE_CBC, iv))
	action_table[blowfish_cbc_menu.addAction("Decrypt")] = lambda: obj.transform_with_key_and_iv(lambda data, key, iv: blowfish_decrypt_transform(data, key, Blowfish.MODE_CBC, iv))

	cast_menu = menu.addMenu("CAST")
	cast_ecb_menu = cast_menu.addMenu("ECB mode")
	cast_cbc_menu = cast_menu.addMenu("CBC mode")
	action_table[cast_ecb_menu.addAction("Encrypt")] = lambda: obj.transform_with_key(lambda data, key: cast_encrypt_transform(data, key, CAST.MODE_ECB, ""))
	action_table[cast_ecb_menu.addAction("Decrypt")] = lambda: obj.transform_with_key(lambda data, key: cast_decrypt_transform(data, key, CAST.MODE_ECB, ""))
	action_table[cast_cbc_menu.addAction("Encrypt")] = lambda: obj.transform_with_key_and_iv(lambda data, key, iv: cast_encrypt_transform(data, key, CAST.MODE_CBC, iv))
	action_table[cast_cbc_menu.addAction("Decrypt")] = lambda: obj.transform_with_key_and_iv(lambda data, key, iv: cast_decrypt_transform(data, key, CAST.MODE_CBC, iv))

	des_menu = menu.addMenu("DES")
	des_ecb_menu = des_menu.addMenu("ECB mode")
	des_cbc_menu = des_menu.addMenu("CBC mode")
	action_table[des_ecb_menu.addAction("Encrypt")] = lambda: obj.transform_with_key(lambda data, key: des_encrypt_transform(data, key, DES.MODE_ECB, ""))
	action_table[des_ecb_menu.addAction("Decrypt")] = lambda: obj.transform_with_key(lambda data, key: des_decrypt_transform(data, key, DES.MODE_ECB, ""))
	action_table[des_cbc_menu.addAction("Encrypt")] = lambda: obj.transform_with_key_and_iv(lambda data, key, iv: des_encrypt_transform(data, key, DES.MODE_CBC, iv))
	action_table[des_cbc_menu.addAction("Decrypt")] = lambda: obj.transform_with_key_and_iv(lambda data, key, iv: des_decrypt_transform(data, key, DES.MODE_CBC, iv))

	des3_menu = menu.addMenu("Triple DES")
	des3_ecb_menu = des3_menu.addMenu("ECB mode")
	des3_cbc_menu = des3_menu.addMenu("CBC mode")
	action_table[des3_ecb_menu.addAction("Encrypt")] = lambda: obj.transform_with_key(lambda data, key: des3_encrypt_transform(data, key, DES3.MODE_ECB, ""))
	action_table[des3_ecb_menu.addAction("Decrypt")] = lambda: obj.transform_with_key(lambda data, key: des3_decrypt_transform(data, key, DES3.MODE_ECB, ""))
	action_table[des3_cbc_menu.addAction("Encrypt")] = lambda: obj.transform_with_key_and_iv(lambda data, key, iv: des3_encrypt_transform(data, key, DES3.MODE_CBC, iv))
	action_table[des3_cbc_menu.addAction("Decrypt")] = lambda: obj.transform_with_key_and_iv(lambda data, key, iv: des3_decrypt_transform(data, key, DES3.MODE_CBC, iv))

	rc2_menu = menu.addMenu("RC2")
	rc2_ecb_menu = rc2_menu.addMenu("ECB mode")
	rc2_cbc_menu = rc2_menu.addMenu("CBC mode")
	action_table[rc2_ecb_menu.addAction("Encrypt")] = lambda: obj.transform_with_key(lambda data, key: rc2_encrypt_transform(data, key, ARC2.MODE_ECB, ""))
	action_table[rc2_ecb_menu.addAction("Decrypt")] = lambda: obj.transform_with_key(lambda data, key: rc2_decrypt_transform(data, key, ARC2.MODE_ECB, ""))
	action_table[rc2_cbc_menu.addAction("Encrypt")] = lambda: obj.transform_with_key_and_iv(lambda data, key, iv: rc2_encrypt_transform(data, key, ARC2.MODE_CBC, iv))
	action_table[rc2_cbc_menu.addAction("Decrypt")] = lambda: obj.transform_with_key_and_iv(lambda data, key, iv: rc2_decrypt_transform(data, key, ARC2.MODE_CBC, iv))

	action_table[menu.addAction("RC4")] = lambda: obj.transform_with_key(lambda data, key: rc4_transform(data, key))
	action_table[menu.addAction("XOR")] = lambda: obj.transform_with_key(lambda data, key: xor_transform(data, key)) 
Example #26
Source File: crypto.py    From CVE-2017-7494 with GNU General Public License v3.0 5 votes vote down vote up
def basic_decrypt(cls, key, ciphertext):
        assert len(ciphertext) % 8 == 0
        des = DES.new(key.contents, DES.MODE_CBC, '\0' * 8)
        return des.decrypt(ciphertext) 
Example #27
Source File: crypto.py    From CVE-2017-7494 with GNU General Public License v3.0 5 votes vote down vote up
def basic_encrypt(cls, key, plaintext):
        assert len(plaintext) % 8 == 0
        des = DES.new(key.contents, DES.MODE_CBC, '\0' * 8)
        return des.encrypt(plaintext) 
Example #28
Source File: crypto.py    From PiBunny with MIT License 5 votes vote down vote up
def basic_encrypt(cls, key, plaintext):
        assert len(plaintext) % 8 == 0
        des = DES.new(key.contents, DES.MODE_CBC, '\0' * 8)
        return des.encrypt(plaintext) 
Example #29
Source File: crypto.py    From PiBunny with MIT License 5 votes vote down vote up
def basic_decrypt(cls, key, ciphertext):
        assert len(ciphertext) % 8 == 0
        des = DES.new(key.contents, DES.MODE_CBC, '\0' * 8)
        return des.decrypt(ciphertext) 
Example #30
Source File: crypto.py    From PiBunny with MIT License 5 votes vote down vote up
def basic_encrypt(cls, key, plaintext):
        assert len(plaintext) % 8 == 0
        des3 = DES3.new(key.contents, AES.MODE_CBC, '\0' * 8)
        return des3.encrypt(plaintext)