本文整理汇总了Python中cryptography.hazmat.primitives.ciphers.algorithms.AES属性的典型用法代码示例。如果您正苦于以下问题:Python algorithms.AES属性的具体用法?Python algorithms.AES怎么用?Python algorithms.AES使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cryptography.hazmat.primitives.ciphers.algorithms
的用法示例。
在下文中一共展示了algorithms.AES属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: aes_encrypt_b64
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import algorithms [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.algorithms import AES [as 别名]
def aes_encrypt_b64(key, data):
"""
This function encrypts the data using AES-128-CBC. It generates
and adds an IV.
This is used for PSKC.
:param key: Encryption key (binary format)
:type key: bytes
:param data: Data to encrypt
:type data: bytes
:return: base64 encrypted output, containing IV and encrypted data
:rtype: str
"""
# pad data
padder = padding.PKCS7(algorithms.AES.block_size).padder()
padded_data = padder.update(data) + padder.finalize()
iv = geturandom(16)
encdata = aes_cbc_encrypt(key, iv, padded_data)
return b64encode_and_unicode(iv + encdata)
示例2: aes_decrypt_b64
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import algorithms [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.algorithms import AES [as 别名]
def aes_decrypt_b64(key, enc_data_b64):
"""
This function decrypts base64 encoded data (containing the IV)
using AES-128-CBC. Used for PSKC
:param key: binary key
:param enc_data_b64: base64 encoded data (IV + encdata)
:type enc_data_b64: str
:return: encrypted data
"""
data_bin = base64.b64decode(enc_data_b64)
iv = data_bin[:16]
encdata = data_bin[16:]
padded_data = aes_cbc_decrypt(key, iv, encdata)
# remove padding
unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
output = unpadder.update(padded_data) + unpadder.finalize()
return output
# @log_with(log)
示例3: encrypt
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import algorithms [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.algorithms import AES [as 别名]
def encrypt(message, receiver_public_key):
sender_private_key = ec.generate_private_key(ec.SECP256K1(), backend)
shared_key = sender_private_key.exchange(ec.ECDH(), receiver_public_key)
sender_public_key = sender_private_key.public_key()
point = sender_public_key.public_numbers().encode_point()
iv = '000000000000'
xkdf = x963kdf.X963KDF(
algorithm = hashes.SHA256(),
length = 32,
sharedinfo = '',
backend = backend
)
key = xkdf.derive(shared_key)
encryptor = Cipher(
algorithms.AES(key),
modes.GCM(iv),
backend = backend
).encryptor()
ciphertext = encryptor.update(message) + encryptor.finalize()
return point + encryptor.tag + ciphertext
示例4: decrypt
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import algorithms [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.algorithms import AES [as 别名]
def decrypt(message, receiver_private_key):
point = message[0:65]
tag = message[65:81]
ciphertext = message[81:]
sender_public_numbers = ec.EllipticCurvePublicNumbers.from_encoded_point(ec.SECP256K1(), point)
sender_public_key = sender_public_numbers.public_key(backend)
shared_key = receiver_private_key.exchange(ec.ECDH(), sender_public_key)
iv = '000000000000'
xkdf = x963kdf.X963KDF(
algorithm = hashes.SHA256(),
length = 32,
sharedinfo = '',
backend = backend
)
key = xkdf.derive(shared_key)
decryptor = Cipher(
algorithms.AES(key),
modes.GCM(iv,tag),
backend = backend
).decryptor()
message = decryptor.update(ciphertext) + decryptor.finalize()
return message
示例5: unwrap
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import algorithms [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.algorithms import AES [as 别名]
def unwrap(self, key, bitsize, ek, headers):
rk = self._get_key(key, 'decrypt')
if 'iv' not in headers:
raise ValueError('Invalid Header, missing "iv" parameter')
iv = base64url_decode(headers['iv'])
if 'tag' not in headers:
raise ValueError('Invalid Header, missing "tag" parameter')
tag = base64url_decode(headers['tag'])
cipher = Cipher(algorithms.AES(rk), modes.GCM(iv, tag),
backend=self.backend)
decryptor = cipher.decryptor()
cek = decryptor.update(ek) + decryptor.finalize()
if _bitsize(cek) != bitsize:
raise InvalidJWEKeyLength(bitsize, _bitsize(cek))
return cek
示例6: decrypt
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import algorithms [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.algorithms import AES [as 别名]
def decrypt(self, k, a, iv, e, t):
""" Decrypt according to the selected encryption and hashing
functions.
:param k: Encryption key (optional)
:param a: Additional Authenticated Data
:param iv: Initialization Vector
:param e: Ciphertext
:param t: Authentication Tag
Returns plaintext or raises an error
"""
hkey = k[:_inbytes(self.keysize)]
dkey = k[_inbytes(self.keysize):]
# verify mac
if not constant_time.bytes_eq(t, self._mac(hkey, a, iv, e)):
raise InvalidSignature('Failed to verify MAC')
# decrypt
cipher = Cipher(algorithms.AES(dkey), modes.CBC(iv),
backend=self.backend)
decryptor = cipher.decryptor()
d = decryptor.update(e) + decryptor.finalize()
unpadder = PKCS7(self.blocksize).unpadder()
return unpadder.update(d) + unpadder.finalize()
示例7: _layer_cipher
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import algorithms [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.algorithms import AES [as 别名]
def _layer_cipher(constant: bytes, revision_counter: int, subcredential: bytes, blinded_key: bytes, salt: bytes) -> Tuple['cryptography.hazmat.primitives.ciphers.Cipher', Callable[[bytes], bytes]]: # type: ignore
try:
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
except ImportError:
raise ImportError('Layer encryption/decryption requires the cryptography module')
kdf = hashlib.shake_256(blinded_key + subcredential + struct.pack('>Q', revision_counter) + salt + constant)
keys = kdf.digest(S_KEY_LEN + S_IV_LEN + MAC_LEN)
secret_key = keys[:S_KEY_LEN]
secret_iv = keys[S_KEY_LEN:S_KEY_LEN + S_IV_LEN]
mac_key = keys[S_KEY_LEN + S_IV_LEN:]
cipher = Cipher(algorithms.AES(secret_key), modes.CTR(secret_iv), default_backend())
mac_prefix = struct.pack('>Q', len(mac_key)) + mac_key + struct.pack('>Q', len(salt)) + salt
return cipher, lambda ciphertext: hashlib.sha3_256(mac_prefix + ciphertext).digest()
示例8: encrypt_data
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import algorithms [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.algorithms import AES [as 别名]
def encrypt_data(data, key, version=0):
"""
Encrypt data using the given key
:param data: data to encrypt
:param key: encryption key (should be 120, 192, 256 bits)
:param version: encryption payload version
:return: encrypted data (version + nonce + tag + cipher text)
"""
validate_key(key)
nonce = _generate_nonce()
cipher = ciphers.Cipher(algorithms.AES(key), modes.GCM(nonce), backend=backends.default_backend())
encryptor = cipher.encryptor()
cipher_text = encryptor.update(data) + encryptor.finalize()
tag = encryptor.tag
return struct.pack('>B', version) + nonce + tag + cipher_text
示例9: _CTR_DRBG_AES128_update
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import algorithms [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.algorithms import AES [as 别名]
def _CTR_DRBG_AES128_update(data, key, v):
assert len(data) == 32
assert len(key) == 16
assert len(v) == 16
cipher = Cipher(algorithms.AES(key), modes.CBC(str_zero(16)),
backend=default_backend())
v = str_inc(v)
encryptor = cipher.encryptor()
new_key = encryptor.update(v) + encryptor.finalize()
v = str_inc(v)
encryptor = cipher.encryptor()
new_v = encryptor.update(v) + encryptor.finalize()
return str_xor(new_key, data[:16]), str_xor(new_v, data[16:])
# Counter mode Deterministic Random Byte Generator
# Specialized for SPAN based on NIST 800-90A
示例10: generate
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import algorithms [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.algorithms import AES [as 别名]
def generate(self, count, data=None):
out = b""
v = self._v
key = self._key
if data is not None:
key, v = _CTR_DRBG_AES128_update(data, key, v)
cipher = Cipher(algorithms.AES(key), modes.CBC(str_zero(16)),
backend=default_backend())
while len(out) < count:
encryptor = cipher.encryptor()
v = str_inc(v)
out += encryptor.update(v) + encryptor.finalize()
if data is None:
data = str_zero(32)
self._key, self._v = _CTR_DRBG_AES128_update(data, key, v)
return out[:count]
示例11: aes_cbc_decrypt
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import algorithms [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.algorithms import AES [as 别名]
def aes_cbc_decrypt(key, iv, enc_data):
"""
Decrypts the given cipherdata with AES (CBC Mode) using the key/iv.
Attention: This function returns the decrypted data as is, without removing
any padding. The calling function must take care of this!
:param key: The encryption key
:type key: bytes
:param iv: The initialization vector
:type iv: bytes
:param enc_data: The cipher text
:type enc_data: binary string
:param mode: The AES MODE
:return: plain text in binary data
:rtype: bytes
"""
backend = default_backend()
mode = modes.CBC(iv)
cipher = Cipher(algorithms.AES(key), mode=mode, backend=backend)
decryptor = cipher.decryptor()
output = decryptor.update(enc_data) + decryptor.finalize()
return output
示例12: aes_cbc_encrypt
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import algorithms [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.algorithms import AES [as 别名]
def aes_cbc_encrypt(key, iv, data):
"""
encrypts the given data with AES (CBC Mode) using key/iv.
Attention: This function expects correctly padded input data (multiple of
AES block size). The calling function must take care of this!
:param key: The encryption key
:type key: binary string
:param iv: The initialization vector
:type iv: binary string
:param data: The cipher text
:type data: bytes
:param mode: The AES MODE
:return: plain text in binary data
:rtype: bytes
"""
assert len(data) % (algorithms.AES.block_size // 8) == 0
# do the encryption
backend = default_backend()
mode = modes.CBC(iv)
cipher = Cipher(algorithms.AES(key), mode=mode, backend=backend)
encryptor = cipher.encryptor()
output = encryptor.update(data) + encryptor.finalize()
return output
示例13: verifykey
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import algorithms [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.algorithms import AES [as 别名]
def verifykey(key, encryptedVerifier, encryptedVerifierHash):
r'''
Return True if the given intermediate key is valid.
>>> key = b'@\xb1:q\xf9\x0b\x96n7T\x08\xf2\xd1\x81\xa1\xaa'
>>> encryptedVerifier = b'Qos.\x96o\xac\x17\xb1\xc5\xd7\xd8\xcc6\xc9('
>>> encryptedVerifierHash = b'+ah\xda\xbe)\x11\xad+\xd3|\x17Ft\\\x14\xd3\xcf\x1b\xb1@\xa4\x8fNo=#\x88\x08r\xb1j'
>>> ECMA376Standard.verifykey(key, encryptedVerifier, encryptedVerifierHash)
True
'''
# TODO: For consistency with Agile, rename method to verify_password or the like
logger.debug([key, encryptedVerifier, encryptedVerifierHash])
# https://msdn.microsoft.com/en-us/library/dd926426(v=office.12).aspx
aes = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend())
decryptor = aes.decryptor()
verifier = decryptor.update(encryptedVerifier)
expected_hash = sha1(verifier).digest()
decryptor = aes.decryptor()
verifierHash = decryptor.update(encryptedVerifierHash)[:sha1().digest_size]
return expected_hash == verifierHash
示例14: get_encryptor
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import algorithms [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.algorithms import AES [as 别名]
def get_encryptor(key, iv=None):
algoer = algorithms.AES(key) #这里的AES算法(若要换成des算法,这里换成TripleDES,该加密库中,DES 事实上等于 TripleDES 的密钥长度为 64bit 时的加解密)
cipher = Cipher(algoer, modes.CBC(iv), backend=default_backend()) #这里的CBC模式
def enc(bitstring):
padder = padding.PKCS7(algoer.block_size).padder()
bitstring = padder.update(bitstring) + padder.finalize()
encryptor = cipher.encryptor()
return encryptor.update(bitstring) + encryptor.finalize()
def dec(bitstring):
decryptor = cipher.decryptor()
ddata = decryptor.update(bitstring) + decryptor.finalize()
unpadder = padding.PKCS7(algoer.block_size).unpadder()
return unpadder.update(ddata) + unpadder.finalize()
class f:pass
f.encrypt = enc
f.decrypt = dec
return f
示例15: chrome_decrypt
# 需要导入模块: from cryptography.hazmat.primitives.ciphers import algorithms [as 别名]
# 或者: from cryptography.hazmat.primitives.ciphers.algorithms import AES [as 别名]
def chrome_decrypt(
encrypted_value: bytes, key: bytes, init_vector: bytes
) -> str:
"""Decrypt Chrome/Chromium's encrypted cookies.
Args:
encrypted_value: Encrypted cookie from Chrome/Chromium's cookie file
key: Key to decrypt encrypted_value
init_vector: Initialization vector for decrypting encrypted_value
Returns:
Decrypted value of encrypted_value
"""
# Encrypted cookies should be prefixed with 'v10' or 'v11' according to the
# Chromium code. Strip it off.
encrypted_value = encrypted_value[3:]
cipher = Cipher(
algorithm=AES(key), mode=CBC(init_vector), backend=default_backend()
)
decryptor = cipher.decryptor()
decrypted = decryptor.update(encrypted_value) + decryptor.finalize()
return clean(decrypted)