本文整理匯總了Python中Cryptodome.Cipher.AES.MODE_CCM屬性的典型用法代碼示例。如果您正苦於以下問題:Python AES.MODE_CCM屬性的具體用法?Python AES.MODE_CCM怎麽用?Python AES.MODE_CCM使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類Cryptodome.Cipher.AES
的用法示例。
在下文中一共展示了AES.MODE_CCM屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_mac_len
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CCM [as 別名]
def test_mac_len(self):
# Invalid MAC length
for mac_len in range(3, 17 + 1, 2):
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
nonce=self.nonce_96, mac_len=mac_len)
# Valid MAC length
for mac_len in range(4, 16 + 1, 2):
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
mac_len=mac_len)
_, mac = cipher.encrypt_and_digest(self.data_128)
self.assertEqual(len(mac), mac_len)
# Default MAC length
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
_, mac = cipher.encrypt_and_digest(self.data_128)
self.assertEqual(len(mac), 16)
示例2: test_valid_init_encrypt_decrypt_digest_verify
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CCM [as 別名]
def test_valid_init_encrypt_decrypt_digest_verify(self):
# No authenticated data, fixed plaintext
for assoc_len in (None, 0):
for msg_len in (None, len(self.data_128)):
# Verify path INIT->ENCRYPT->DIGEST
cipher = AES.new(self.key_128, AES.MODE_CCM,
nonce=self.nonce_96,
assoc_len=assoc_len,
msg_len=msg_len)
ct = cipher.encrypt(self.data_128)
mac = cipher.digest()
# Verify path INIT->DECRYPT->VERIFY
cipher = AES.new(self.key_128, AES.MODE_CCM,
nonce=self.nonce_96,
assoc_len=assoc_len,
msg_len=msg_len)
cipher.decrypt(ct)
cipher.verify(mac)
示例3: test_valid_init_update_digest_verify
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CCM [as 別名]
def test_valid_init_update_digest_verify(self):
# No plaintext, fixed authenticated data
for assoc_len in (None, len(self.data_128)):
for msg_len in (None, 0):
# Verify path INIT->UPDATE->DIGEST
cipher = AES.new(self.key_128, AES.MODE_CCM,
nonce=self.nonce_96,
assoc_len=assoc_len,
msg_len=msg_len)
cipher.update(self.data_128)
mac = cipher.digest()
# Verify path INIT->UPDATE->VERIFY
cipher = AES.new(self.key_128, AES.MODE_CCM,
nonce=self.nonce_96,
assoc_len=assoc_len,
msg_len=msg_len)
cipher.update(self.data_128)
cipher.verify(mac)
示例4: test_valid_full_path
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CCM [as 別名]
def test_valid_full_path(self):
# Fixed authenticated data, fixed plaintext
for assoc_len in (None, len(self.data_128)):
for msg_len in (None, len(self.data_128)):
# Verify path INIT->UPDATE->ENCRYPT->DIGEST
cipher = AES.new(self.key_128, AES.MODE_CCM,
nonce=self.nonce_96,
assoc_len=assoc_len,
msg_len=msg_len)
cipher.update(self.data_128)
ct = cipher.encrypt(self.data_128)
mac = cipher.digest()
# Verify path INIT->UPDATE->DECRYPT->VERIFY
cipher = AES.new(self.key_128, AES.MODE_CCM,
nonce=self.nonce_96,
assoc_len=assoc_len,
msg_len=msg_len)
cipher.update(self.data_128)
cipher.decrypt(ct)
cipher.verify(mac)
示例5: decrypt_payload
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CCM [as 別名]
def decrypt_payload(encrypted_payload, key, nonce):
"""Decrypt payload."""
aad = b"\x11"
token = encrypted_payload[-4:]
payload_counter = encrypted_payload[-7:-4]
nonce = b"".join([nonce, payload_counter])
cipherpayload = encrypted_payload[:-7]
cipher = AES.new(key, AES.MODE_CCM, nonce=nonce, mac_len=4)
cipher.update(aad)
plaindata = None
try:
plaindata = cipher.decrypt_and_verify(cipherpayload, token)
except ValueError as error:
_LOGGER.error("Decryption failed: %s", error)
_LOGGER.error("token: %s", token.hex())
_LOGGER.error("nonce: %s", nonce.hex())
_LOGGER.error("encrypted_payload: %s", encrypted_payload.hex())
_LOGGER.error("cipherpayload: %s", cipherpayload.hex())
return None
return plaindata
示例6: test_mac_len
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CCM [as 別名]
def test_mac_len(self):
# Invalid MAC length
for mac_len in xrange(3, 17 + 1, 2):
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
nonce=self.nonce_96, mac_len=mac_len)
# Valid MAC length
for mac_len in xrange(4, 16 + 1, 2):
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
mac_len=mac_len)
_, mac = cipher.encrypt_and_digest(self.data_128)
self.assertEqual(len(mac), mac_len)
# Default MAC length
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
_, mac = cipher.encrypt_and_digest(self.data_128)
self.assertEqual(len(mac), 16)
示例7: test_valid_multiple_encrypt_or_decrypt
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CCM [as 別名]
def test_valid_multiple_encrypt_or_decrypt(self):
# Only possible if msg_len is declared in advance
for method_name in "encrypt", "decrypt":
for auth_data in (None, b("333"), self.data_128,
self.data_128 + b("3")):
if auth_data is None:
assoc_len = None
else:
assoc_len = len(auth_data)
cipher = AES.new(self.key_128, AES.MODE_CCM,
nonce=self.nonce_96,
msg_len=64,
assoc_len=assoc_len)
if auth_data is not None:
cipher.update(auth_data)
method = getattr(cipher, method_name)
method(self.data_128)
method(self.data_128)
method(self.data_128)
method(self.data_128)
示例8: _decrypt
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CCM [as 別名]
def _decrypt(self,
encrypted_data: bytes = b'',
source_mac_address: str = '01:23:45:67:89:0a',
temporal_key: bytes = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
key_initialization_vector: bytes = b'\x00\x00\x00\x00\x00\x01') -> Union[None, bytes]:
"""
Decrypt the data
:param encrypted_data: Bytes of Encrypted data
:param source_mac_address: Source MAC address (example: '01:23:45:67:89:0a')
:param temporal_key: 128 bits – Temporal Key (default: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
:param key_initialization_vector: Key Initialization Vector (default: b'\x00\x00\x00\x00\x00\x01')
:return: Bytes of Decrypted data or None if error
"""
try:
assert self._base.mac_address_validation(source_mac_address), 'Bad source MAC address'
assert len(key_initialization_vector) == 6, 'Bad Key Initialization Vector length'
assert len(temporal_key) == 16, 'Bad Temporal Key length'
nonce: bytes = b'\x00' + self._convert_mac(source_mac_address) + key_initialization_vector
cipher: AES = AES.new(temporal_key, AES.MODE_CCM, nonce, mac_len=8)
decrypted_data: bytes = cipher.decrypt(encrypted_data)
assert decrypted_data.startswith(b'\xaa\xaa\x03'), 'Decrypt error'
return decrypted_data
except AssertionError:
return None
# endregion
# region Analyze 802.11 packet
示例9: test_loopback_128
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CCM [as 別名]
def test_loopback_128(self):
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
pt = get_tag_random("plaintext", 16 * 100)
ct = cipher.encrypt(pt)
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
pt2 = cipher.decrypt(ct)
self.assertEqual(pt, pt2)
示例10: test_nonce
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CCM [as 別名]
def test_nonce(self):
# If not passed, the nonce is created randomly
cipher = AES.new(self.key_128, AES.MODE_CCM)
nonce1 = cipher.nonce
cipher = AES.new(self.key_128, AES.MODE_CCM)
nonce2 = cipher.nonce
self.assertEqual(len(nonce1), 11)
self.assertNotEqual(nonce1, nonce2)
cipher = AES.new(self.key_128, AES.MODE_CCM, self.nonce_96)
ct = cipher.encrypt(self.data_128)
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
self.assertEqual(ct, cipher.encrypt(self.data_128))
示例11: test_nonce_must_be_bytes
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CCM [as 別名]
def test_nonce_must_be_bytes(self):
self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CCM,
nonce='test12345678')
示例12: test_nonce_length
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CCM [as 別名]
def test_nonce_length(self):
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
nonce=b(""))
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
nonce=bchr(1) * 6)
self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
nonce=bchr(1) * 14)
for x in range(7, 13 + 1):
AES.new(self.key_128, AES.MODE_CCM, nonce=bchr(1) * x)
示例13: test_block_size
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CCM [as 別名]
def test_block_size(self):
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
self.assertEqual(cipher.block_size, AES.block_size)
示例14: test_unknown_parameters
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CCM [as 別名]
def test_unknown_parameters(self):
self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CCM,
self.nonce_96, 7)
self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CCM,
nonce=self.nonce_96, unknown=7)
# But some are only known by the base cipher
# (e.g. use_aesni consumed by the AES module)
AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
use_aesni=False)
示例15: test_null_encryption_decryption
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CCM [as 別名]
def test_null_encryption_decryption(self):
for func in "encrypt", "decrypt":
cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
result = getattr(cipher, func)(b(""))
self.assertEqual(result, b(""))