本文整理匯總了Python中Cryptodome.Cipher.AES.MODE_CBC屬性的典型用法代碼示例。如果您正苦於以下問題:Python AES.MODE_CBC屬性的具體用法?Python AES.MODE_CBC怎麽用?Python AES.MODE_CBC使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類Cryptodome.Cipher.AES
的用法示例。
在下文中一共展示了AES.MODE_CBC屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: encrypt
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CBC [as 別名]
def encrypt(self, plaintext, esn):
"""
Encrypt the given Plaintext with the encryption key
:param plaintext:
:return: Serialized JSON String of the encryption Envelope
"""
init_vector = get_random_bytes(16)
cipher = AES.new(self.encryption_key, AES.MODE_CBC, init_vector)
ciphertext = base64.standard_b64encode(
cipher.encrypt(Padding.pad(plaintext.encode('utf-8'), 16))).decode('utf-8')
encryption_envelope = {
'ciphertext': ciphertext,
'keyid': '_'.join((esn, str(self.sequence_number))),
'sha256': 'AA==',
'iv': base64.standard_b64encode(init_vector).decode('utf-8')
}
return json.dumps(encryption_envelope)
示例2: encrypt_credential
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CBC [as 別名]
def encrypt_credential(raw):
"""
Encodes data
:param data: Data to be encoded
:type data: str
:returns: string -- Encoded data
"""
# pylint: disable=invalid-name,import-error
import base64
try: # The crypto package depends on the library installed (see Wiki)
from Crypto import Random
from Crypto.Cipher import AES
from Crypto.Util import Padding
except ImportError:
from Cryptodome import Random
from Cryptodome.Cipher import AES
from Cryptodome.Util import Padding
raw = bytes(Padding.pad(data_to_pad=raw.encode('utf-8'), block_size=__BLOCK_SIZE__))
iv = Random.new().read(AES.block_size)
cipher = AES.new(get_crypt_key(), AES.MODE_CBC, iv)
return base64.b64encode(iv + cipher.encrypt(raw)).decode('utf-8')
示例3: decrypt_credential
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CBC [as 別名]
def decrypt_credential(enc, secret=None):
"""
Decodes data
:param data: Data to be decoded
:type data: str
:returns: string -- Decoded data
"""
# pylint: disable=invalid-name,import-error
import base64
try: # The crypto package depends on the library installed (see Wiki)
from Crypto.Cipher import AES
from Crypto.Util import Padding
except ImportError:
from Cryptodome.Cipher import AES
from Cryptodome.Util import Padding
enc = base64.b64decode(enc)
iv = enc[:AES.block_size]
cipher = AES.new(secret or get_crypt_key(), AES.MODE_CBC, iv)
decoded = Padding.unpad(
padded_data=cipher.decrypt(enc[AES.block_size:]),
block_size=__BLOCK_SIZE__)
return decoded
示例4: decode_aes256
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CBC [as 別名]
def decode_aes256(cipher, iv, data, encryption_key):
"""
Decrypt AES-256 bytes.
Allowed ciphers are: :ecb, :cbc.
If for :ecb iv is not used and should be set to "".
"""
if cipher == 'cbc':
aes = AES.new(encryption_key, AES.MODE_CBC, iv)
elif cipher == 'ecb':
aes = AES.new(encryption_key, AES.MODE_ECB)
else:
raise ValueError('Unknown AES mode')
d = aes.decrypt(data)
# http://passingcuriosity.com/2009/aes-encryption-in-python-with-m2crypto/
unpad = lambda s: s[0:-ord(d[-1:])]
return unpad(d)
示例5: encode
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CBC [as 別名]
def encode(the_id, sub_key):
assert 0 <= the_id < 2 ** 64
version = 1
crc = binascii.crc32(str(the_id).encode('utf-8')) & 0xffffffff
message = struct.pack(b"<IQI", crc, the_id, version)
assert len(message) == 16
key = settings.SECRET_KEY
iv = hashlib.sha256((key + sub_key).encode('ascii')).digest()[:16]
cypher = AES.new(key[:32].encode('utf-8'), AES.MODE_CBC, iv)
eid = base64.urlsafe_b64encode(cypher.encrypt(message)).replace(b"=", b"")
return eid.decode("utf-8")
示例6: _openssl_encrypt
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CBC [as 別名]
def _openssl_encrypt(password, plaintext):
"""
:type password: str
:type plaintext: str
:rtype: str
"""
# Thanks to Joe Linoff, taken from https://stackoverflow.com/a/42773185
salt = get_random_bytes(8)
key, iv = PayloadFactory._get_key_and_iv(password, salt)
# PKCS#7 padding
padding_len = 16 - (len(plaintext) % 16)
padded_plaintext = plaintext + (chr(padding_len) * padding_len)
# Encrypt
cipher = AES.new(key, AES.MODE_CBC, iv)
cipher_text = cipher.encrypt(padded_plaintext.encode())
# Make OpenSSL compatible
openssl_cipher_text = b"Salted__" + salt + cipher_text
return b64encode(openssl_cipher_text).decode()
示例7: test_aes_128
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES 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)
示例8: test_aes_192
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES 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)
示例9: test_aes_256
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES 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)
示例10: init_aes
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CBC [as 別名]
def init_aes(shared_secret, nonce):
""" Initialize AES instance
:param hex shared_secret: Shared Secret to use as encryption key
:param int nonce: Random nonce
:return: AES instance
:rtype: AES
"""
" Shared Secret "
ss = hashlib.sha512(unhexlify(shared_secret)).digest()
" Seed "
seed = bytes(str(nonce), "ascii") + hexlify(ss)
seed_digest = hexlify(hashlib.sha512(seed).digest()).decode("ascii")
" AES "
key = unhexlify(seed_digest[0:64])
iv = unhexlify(seed_digest[64:96])
return AES.new(key, AES.MODE_CBC, iv)
示例11: msl_encrypt
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CBC [as 別名]
def msl_encrypt(msl_session, plaintext):
"""
msl_encrypt()
@param msl_session: Dict of msl_session created by the client
upon initialization
@param plaintext: Plaintext to encrypt
@return: JSON byte string of encryption envelope
"""
cbc_iv = os.urandom(16)
encryption_envelope = {
'keyid': '%s_%s' % (
msl_session['esn'],
msl_session['session_keys']['sequence_number']
),
'sha256': 'AA==',
'iv': base64.b64encode(cbc_iv).decode('utf8')
}
plaintext = Padding.pad(plaintext.encode('utf8'), 16)
cipher = AES.new(
msl_session['session_keys']['encryption_key'],
AES.MODE_CBC,
cbc_iv
)
ciphertext = cipher.encrypt(plaintext)
encryption_envelope['ciphertext'] = base64.b64encode(
ciphertext
).decode('utf8')
return json.dumps(encryption_envelope).encode('utf8')
示例12: aes_cbc_encrypt
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CBC [as 別名]
def aes_cbc_encrypt(plain_text: bytes, key: bytes, iv: bytes = b''):
if len(iv) == 0:
iv = AESHandler.generate_iv()
cipher = AES.new(key=key, mode=AES.MODE_CBC, iv=iv)
return cipher.IV, cipher.encrypt(pad(plain_text, AES.block_size))
示例13: aes_cbc_decrypt
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CBC [as 別名]
def aes_cbc_decrypt(cipher_text: bytes, iv: bytes, key: bytes):
cipher = AES.new(key=key, mode=AES.MODE_CBC, iv=iv)
return unpad(cipher.decrypt(cipher_text), AES.block_size, style='pkcs7')
示例14: aes_encrypt
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CBC [as 別名]
def aes_encrypt(text, sec_key):
pad = 16 - len(text) % 16
text = text + chr(pad) * pad
encryptor = AES.new(sec_key.encode('utf-8'), AES.MODE_CBC, b'0102030405060708')
cipher_text = encryptor.encrypt(text.encode('utf-8'))
cipher_text = base64.b64encode(cipher_text).decode('utf-8')
return cipher_text
示例15: encrypt
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import MODE_CBC [as 別名]
def encrypt(key, raw):
assert type(raw) == bytes, "input data is bytes"
if isinstance(key, str):
key = b64decode(key.encode())
raw = pad(raw, AES.block_size)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
return iv + cipher.encrypt(raw)