当前位置: 首页>>代码示例>>Python>>正文


Python DES.MODE_CBC属性代码示例

本文整理汇总了Python中Crypto.Cipher.DES.MODE_CBC属性的典型用法代码示例。如果您正苦于以下问题:Python DES.MODE_CBC属性的具体用法?Python DES.MODE_CBC怎么用?Python DES.MODE_CBC使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在Crypto.Cipher.DES的用法示例。


在下文中一共展示了DES.MODE_CBC属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: aes_encrypt

# 需要导入模块: from Crypto.Cipher import DES [as 别名]
# 或者: from Crypto.Cipher.DES import MODE_CBC [as 别名]
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 
开发者ID:PKUJohnson,项目名称:OpenData,代码行数:20,代码来源:util.py

示例2: aes_decrypt

# 需要导入模块: from Crypto.Cipher import DES [as 别名]
# 或者: from Crypto.Cipher.DES import MODE_CBC [as 别名]
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 
开发者ID:PKUJohnson,项目名称:OpenData,代码行数:22,代码来源:util.py

示例3: encryptData

# 需要导入模块: from Crypto.Cipher import DES [as 别名]
# 或者: from Crypto.Cipher.DES import MODE_CBC [as 别名]
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 
开发者ID:scalyr,项目名称:scalyr-agent-2,代码行数:27,代码来源:des.py

示例4: encrypt

# 需要导入模块: from Crypto.Cipher import DES [as 别名]
# 或者: from Crypto.Cipher.DES import MODE_CBC [as 别名]
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)) 
开发者ID:pimps,项目名称:CVE-2017-1000486,代码行数:23,代码来源:primefaces.py

示例5: test_aes_128

# 需要导入模块: from Crypto.Cipher import DES [as 别名]
# 或者: from Crypto.Cipher.DES import MODE_CBC [as 别名]
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) 
开发者ID:vcheckzen,项目名称:FODI,代码行数:23,代码来源:test_CBC.py

示例6: test_aes_192

# 需要导入模块: from Crypto.Cipher import DES [as 别名]
# 或者: from Crypto.Cipher.DES import MODE_CBC [as 别名]
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) 
开发者ID:vcheckzen,项目名称:FODI,代码行数:23,代码来源:test_CBC.py

示例7: test_aes_256

# 需要导入模块: from Crypto.Cipher import DES [as 别名]
# 或者: from Crypto.Cipher.DES import MODE_CBC [as 别名]
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) 
开发者ID:vcheckzen,项目名称:FODI,代码行数:23,代码来源:test_CBC.py

示例8: __init__

# 需要导入模块: from Crypto.Cipher import DES [as 别名]
# 或者: from Crypto.Cipher.DES import MODE_CBC [as 别名]
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") 
开发者ID:suma12,项目名称:asterix,代码行数:24,代码来源:SecurePacket.py

示例9: sign

# 需要导入模块: from Crypto.Cipher import DES [as 别名]
# 或者: from Crypto.Cipher.DES import MODE_CBC [as 别名]
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:] 
开发者ID:suma12,项目名称:asterix,代码行数:26,代码来源:SecurePacket.py

示例10: encrypt

# 需要导入模块: from Crypto.Cipher import DES [as 别名]
# 或者: from Crypto.Cipher.DES import MODE_CBC [as 别名]
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) 
开发者ID:suma12,项目名称:asterix,代码行数:24,代码来源:SecurePacket.py

示例11: decrypt_des_cbc

# 需要导入模块: from Crypto.Cipher import DES [as 别名]
# 或者: from Crypto.Cipher.DES import MODE_CBC [as 别名]
def decrypt_des_cbc(key, data, iv=None):
    mode = DES.MODE_CBC
    if iv:
        cipher = DES.new(key, mode, iv)
    else:
        cipher = DES.new(key, mode)
    return cipher.decrypt(data)


# DES3 
开发者ID:kevthehermit,项目名称:RATDecoders,代码行数:12,代码来源:crypto.py

示例12: decrypt_aes_cbc_iv

# 需要导入模块: from Crypto.Cipher import DES [as 别名]
# 或者: from Crypto.Cipher.DES import MODE_CBC [as 别名]
def decrypt_aes_cbc_iv(key, iv, data):
    mode = AES.MODE_CBC
    cipher = AES.new(key, mode, IV=iv)
    return cipher.decrypt(data)


# Blowfish 
开发者ID:kevthehermit,项目名称:RATDecoders,代码行数:9,代码来源:crypto.py

示例13: basic_encrypt

# 需要导入模块: from Crypto.Cipher import DES [as 别名]
# 或者: from Crypto.Cipher.DES import MODE_CBC [as 别名]
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) 
开发者ID:joxeankoret,项目名称:CVE-2017-7494,代码行数:6,代码来源:crypto.py

示例14: basic_decrypt

# 需要导入模块: from Crypto.Cipher import DES [as 别名]
# 或者: from Crypto.Cipher.DES import MODE_CBC [as 别名]
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) 
开发者ID:joxeankoret,项目名称:CVE-2017-7494,代码行数:6,代码来源:crypto.py

示例15: decrypt_response

# 需要导入模块: from Crypto.Cipher import DES [as 别名]
# 或者: from Crypto.Cipher.DES import MODE_CBC [as 别名]
def decrypt_response(des_key, des_iv, aes_key, aes_iv, content):
    """
    AES解密
     key,iv使用同一个
    模式cbc
    去填充pkcs7
    :param key:
    :param content:
    :return:
    """
    aes = AES.new(aes_key, AES.MODE_CBC, aes_iv)
    des = DES.new(des_key, DES.MODE_CBC, des_iv)
    # base64解码
    encrypt_bytes = base64.b64decode(content)
    # 解密
    decrypt_bytes = des.decrypt(encrypt_bytes)
    decrypt_bytes = base64.b64decode(decrypt_bytes)
    #decrypt_bytes = pkcs7padding(decrypt_bytes.decode()).encode("utf8")
    decrypt_bytes = aes.decrypt(decrypt_bytes)

    #base64解码
    decrypt_bytes = base64.b64decode(decrypt_bytes)
    # 重新编码
    result = str(decrypt_bytes, encoding='utf8')
    # 去除填充内容
    #result = pkcs7unpadding(result)
    return result 
开发者ID:PKUJohnson,项目名称:OpenData,代码行数:29,代码来源:2.py


注:本文中的Crypto.Cipher.DES.MODE_CBC属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。