當前位置: 首頁>>代碼示例>>Python>>正文


Python AES.MODE_CTR屬性代碼示例

本文整理匯總了Python中Crypto.Cipher.AES.MODE_CTR屬性的典型用法代碼示例。如果您正苦於以下問題:Python AES.MODE_CTR屬性的具體用法?Python AES.MODE_CTR怎麽用?Python AES.MODE_CTR使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在Crypto.Cipher.AES的用法示例。


在下文中一共展示了AES.MODE_CTR屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: iter_transform

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import MODE_CTR [as 別名]
def iter_transform(filename, key):
    """Generate encrypted file with given key.

    This generator function reads the file
    in chunks and encrypts them using AES-CTR,
    with the specified key.

    :param filename: The name of the file to encrypt.
    :type filename: str
    :param key: The key used to encrypt the file.
    :type key: str
    :returns: A generator that produces encrypted file chunks.
    :rtype: generator
    """
    # We are not specifying the IV here.
    aes = AES.new(key, AES.MODE_CTR, counter=Counter.new(128))

    with open(filename, 'rb+') as f:
        for chunk in iter(lambda: f.read(CHUNK_SIZE), b''):
            yield aes.encrypt(chunk), f 
開發者ID:StorjOld,項目名稱:file-encryptor,代碼行數:22,代碼來源:convergence.py

示例2: dorecv

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import MODE_CTR [as 別名]
def dorecv(self):
        msg = self.request.recv(1024)
        assert len(msg)>0
        if self.k!='':
            self.r += 1
            ctr_e=Counter.new(128, initial_value=self.r)
            a=AES.new(key=self.k,mode=AES.MODE_CTR,counter=ctr_e)
            cmsg = msg
            msg = ''
            assert len(cmsg)%16==0
            for i in range(len(cmsg)/16):
                msg += a.decrypt(cmsg[i*16:(i+1)*16])
            tmp = ord(msg[-1])
            for i in range(len(msg)-tmp,len(msg)):
                assert ord(msg[i]) == tmp
            msg = msg[:-ord(msg[-1])]
        return msg 
開發者ID:sixstars,項目名稱:starctf2018,代碼行數:19,代碼來源:task2.py

示例3: decrypt_package

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import MODE_CTR [as 別名]
def decrypt_package(self, dec_material, data):
        if not self._good:
            logging.warning('well, it is hard to decrypt with a wrong key.')

        if not dec_material.encMsgV3:
            logging.error('cannot decrypt with an empty encMsgV3!')
            return None

        salt = dec_material.encMsgV3[:32]
        counter_iv = dec_material.encMsgV3[32:]

        key = PBKDF2(self._bkey, salt, Decryptor.dklen, Decryptor.count,
                     Decryptor.prf, hmac_hash_module=None)

        counter_obj = Counter.new(128, initial_value=int.from_bytes(
            counter_iv, byteorder='big'), little_endian=False)

        decryptor = AES.new(key, mode=AES.MODE_CTR, counter=counter_obj)
        return decryptor.decrypt(data) 
開發者ID:RealityNet,項目名稱:kobackupdec,代碼行數:21,代碼來源:kobackupdec.py

示例4: decrypt_large_package

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import MODE_CTR [as 別名]
def decrypt_large_package(self, dec_material, entry):
        if not self._good:
            logging.warning('well, it is hard to decrypt with a wrong key.')

        if not dec_material.encMsgV3:
            logging.error('cannot decrypt with an empty encMsgV3!')
            return None

        salt = dec_material.encMsgV3[:32]
        counter_iv = dec_material.encMsgV3[32:]

        key = PBKDF2(self._bkey, salt, Decryptor.dklen, Decryptor.count,
                     Decryptor.prf, hmac_hash_module=None)

        counter_obj = Counter.new(128, initial_value=int.from_bytes(
            counter_iv, byteorder='big'), little_endian=False)

        decryptor = AES.new(key, mode=AES.MODE_CTR, counter=counter_obj)
        data_len = entry.stat().st_size
        with open(entry, 'rb') as entry_fd:
            for x in range(0, data_len, self.chunk_size):
                logging.debug('decrypting chunk %d of %s', x, entry)
                data = entry_fd.read(self.chunk_size)
                yield decryptor.decrypt(data) 
開發者ID:RealityNet,項目名稱:kobackupdec,代碼行數:26,代碼來源:kobackupdec.py

示例5: decrypt_file

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import MODE_CTR [as 別名]
def decrypt_file(self, dec_material, data):
        if not self._good:
            logging.warning('well, it is hard to decrypt with a wrong key.')

        if not dec_material.iv:
            logging.error('cannot decrypt with an empty iv!')
            return None

        counter_obj = Counter.new(
            128,
            initial_value=int.from_bytes(dec_material.iv, byteorder='big'),
            little_endian=False)

        decryptor = AES.new(
            self._bkey_sha256, mode=AES.MODE_CTR, counter=counter_obj)
        return decryptor.decrypt(data)

# --- DecryptInfo ------------------------------------------------------------- 
開發者ID:RealityNet,項目名稱:kobackupdec,代碼行數:20,代碼來源:kobackupdec.py

示例6: test_initial_value_bytes_parameter

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import MODE_CTR [as 別名]
def test_initial_value_bytes_parameter(self):
        # Same result as when passing an integer
        cipher1 = AES.new(self.key_128, AES.MODE_CTR,
                          nonce=self.nonce_64,
                          initial_value=b"\x00"*6+b"\xFF\xFF")
        cipher2 = AES.new(self.key_128, AES.MODE_CTR,
                          nonce=self.nonce_64, initial_value=0xFFFF)
        pt = get_tag_random("plaintext", 65536)
        self.assertEqual(cipher1.encrypt(pt), cipher2.encrypt(pt))

        # Fail if the iv is too large
        self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CTR,
                          initial_value=b"5"*17)
        self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CTR,
                          nonce=self.nonce_64, initial_value=b"5"*9)

        # Fail if the iv is too short
        self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CTR,
                          initial_value=b"5"*15)
        self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CTR,
                          nonce=self.nonce_64, initial_value=b"5"*7) 
開發者ID:vcheckzen,項目名稱:FODI,代碼行數:23,代碼來源:test_CTR.py

示例7: test_wrap_around

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import MODE_CTR [as 別名]
def test_wrap_around(self):
        # Counter is only 8 bits, so we can only encrypt/decrypt 256 blocks (=4096 bytes)
        counter = Counter.new(8, prefix=bchr(9) * 15)
        max_bytes = 4096

        cipher = AES.new(self.key_128, AES.MODE_CTR, counter=counter)
        cipher.encrypt(b'9' * max_bytes)
        self.assertRaises(OverflowError, cipher.encrypt, b'9')

        cipher = AES.new(self.key_128, AES.MODE_CTR, counter=counter)
        self.assertRaises(OverflowError, cipher.encrypt, b'9' * (max_bytes + 1))

        cipher = AES.new(self.key_128, AES.MODE_CTR, counter=counter)
        cipher.decrypt(b'9' * max_bytes)
        self.assertRaises(OverflowError, cipher.decrypt, b'9')

        cipher = AES.new(self.key_128, AES.MODE_CTR, counter=counter)
        self.assertRaises(OverflowError, cipher.decrypt, b'9' * (max_bytes + 1)) 
開發者ID:vcheckzen,項目名稱:FODI,代碼行數:20,代碼來源:test_CTR.py

示例8: test_output_param

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import MODE_CTR [as 別名]
def test_output_param(self):

        pt = b'5' * 16
        cipher = AES.new(b'4'*16, AES.MODE_CTR, nonce=self.nonce_64)
        ct = cipher.encrypt(pt)

        output = bytearray(16)
        cipher = AES.new(b'4'*16, AES.MODE_CTR, nonce=self.nonce_64)
        res = cipher.encrypt(pt, output=output)
        self.assertEqual(ct, output)
        self.assertEqual(res, None)
        
        cipher = AES.new(b'4'*16, AES.MODE_CTR, nonce=self.nonce_64)
        res = cipher.decrypt(ct, output=output)
        self.assertEqual(pt, output)
        self.assertEqual(res, None) 
開發者ID:vcheckzen,項目名稱:FODI,代碼行數:18,代碼來源:test_CTR.py

示例9: test_output_param_neg

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import MODE_CTR [as 別名]
def test_output_param_neg(self):

        pt = b'5' * 16
        cipher = AES.new(b'4'*16, AES.MODE_CTR, nonce=self.nonce_64)
        ct = cipher.encrypt(pt)

        cipher = AES.new(b'4'*16, AES.MODE_CTR, nonce=self.nonce_64)
        self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0'*16)
        
        cipher = AES.new(b'4'*16, AES.MODE_CTR, nonce=self.nonce_64)
        self.assertRaises(TypeError, cipher.decrypt, ct, output=b'0'*16)

        shorter_output = bytearray(15)
        cipher = AES.new(b'4'*16, AES.MODE_CTR, nonce=self.nonce_64)
        self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output)
        cipher = AES.new(b'4'*16, AES.MODE_CTR, nonce=self.nonce_64)
        self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output) 
開發者ID:vcheckzen,項目名稱:FODI,代碼行數:19,代碼來源:test_CTR.py

示例10: test_aes_128

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import MODE_CTR [as 別名]
def test_aes_128(self):
        plaintext =     '6bc1bee22e409f96e93d7e117393172a' +\
                        'ae2d8a571e03ac9c9eb76fac45af8e51' +\
                        '30c81c46a35ce411e5fbc1191a0a52ef' +\
                        'f69f2445df4f9b17ad2b417be66c3710'
        ciphertext =    '874d6191b620e3261bef6864990db6ce' +\
                        '9806f66b7970fdff8617187bb9fffdff' +\
                        '5ae4df3edbd5d35e5b4f09020db03eab' +\
                        '1e031dda2fbe03d1792170a0f3009cee'
        key =           '2b7e151628aed2a6abf7158809cf4f3c'
        counter =       Counter.new(nbits=16,
                                    prefix=unhexlify('f0f1f2f3f4f5f6f7f8f9fafbfcfd'),
                                    initial_value=0xfeff)

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

        cipher = AES.new(key, AES.MODE_CTR, counter=counter)
        self.assertEqual(cipher.encrypt(plaintext), ciphertext)
        cipher = AES.new(key, AES.MODE_CTR, counter=counter)
        self.assertEqual(cipher.decrypt(ciphertext), plaintext) 
開發者ID:vcheckzen,項目名稱:FODI,代碼行數:24,代碼來源:test_CTR.py

示例11: test_aes_192

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import MODE_CTR [as 別名]
def test_aes_192(self):
        plaintext =     '6bc1bee22e409f96e93d7e117393172a' +\
                        'ae2d8a571e03ac9c9eb76fac45af8e51' +\
                        '30c81c46a35ce411e5fbc1191a0a52ef' +\
                        'f69f2445df4f9b17ad2b417be66c3710'
        ciphertext =    '1abc932417521ca24f2b0459fe7e6e0b' +\
                        '090339ec0aa6faefd5ccc2c6f4ce8e94' +\
                        '1e36b26bd1ebc670d1bd1d665620abf7' +\
                        '4f78a7f6d29809585a97daec58c6b050'
        key =           '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'
        counter =       Counter.new(nbits=16,
                                    prefix=unhexlify('f0f1f2f3f4f5f6f7f8f9fafbfcfd'),
                                    initial_value=0xfeff)

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

        cipher = AES.new(key, AES.MODE_CTR, counter=counter)
        self.assertEqual(cipher.encrypt(plaintext), ciphertext)
        cipher = AES.new(key, AES.MODE_CTR, counter=counter)
        self.assertEqual(cipher.decrypt(ciphertext), plaintext) 
開發者ID:vcheckzen,項目名稱:FODI,代碼行數:24,代碼來源:test_CTR.py

示例12: _set_key

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import MODE_CTR [as 別名]
def _set_key(self, key):
        self.key = key
        self._cipher = AES.new(key, AES.MODE_CTR, counter=self.counter) 
開發者ID:mortcanty,項目名稱:earthengine,代碼行數:5,代碼來源:FortunaGenerator.py

示例13: dosend

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import MODE_CTR [as 別名]
def dosend(self, msg):
        if self.k!='':
            self.r += 1
            ctr_e=Counter.new(128, initial_value=self.r)
            a=AES.new(key=self.k,mode=AES.MODE_CTR,counter=ctr_e)
            pad = 16 - len(msg)%16
            plain = msg + chr(pad)*pad
            msg = ''
            for i in range(len(plain)/16):
                msg += a.encrypt(plain[i*16:(i+1)*16])
        self.request.sendall(msg) 
開發者ID:sixstars,項目名稱:starctf2018,代碼行數:13,代碼來源:task.py

示例14: dorecv

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import MODE_CTR [as 別名]
def dorecv(self):
        msg = self.request.recv(1024)
        assert len(msg)>0
        if self.k!='':
            self.r += 1
            ctr_e=Counter.new(128, initial_value=self.r)
            a=AES.new(key=self.k,mode=AES.MODE_CTR,counter=ctr_e)
            cmsg = msg
            msg = ''
            assert len(cmsg)%16==0
            for i in range(len(cmsg)/16):
                msg += a.decrypt(cmsg[i*16:(i+1)*16])
            msg = msg[:-ord(msg[-1])]
        return msg 
開發者ID:sixstars,項目名稱:starctf2018,代碼行數:16,代碼來源:task.py

示例15: try_use_pycrypto_or_pycryptodome_module

# 需要導入模塊: from Crypto.Cipher import AES [as 別名]
# 或者: from Crypto.Cipher.AES import MODE_CTR [as 別名]
def try_use_pycrypto_or_pycryptodome_module():
    from Crypto.Cipher import AES
    from Crypto.Util import Counter

    def create_aes_ctr(key, iv):
        ctr = Counter.new(128, initial_value=iv)
        return AES.new(key, AES.MODE_CTR, counter=ctr)

    def create_aes_cbc(key, iv):
        return AES.new(key, AES.MODE_CBC, iv)

    return create_aes_ctr, create_aes_cbc 
開發者ID:alexbers,項目名稱:mtprotoproxy,代碼行數:14,代碼來源:mtprotoproxy.py


注:本文中的Crypto.Cipher.AES.MODE_CTR屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。