本文整理匯總了Python中Cryptodome.Cipher.AES.new方法的典型用法代碼示例。如果您正苦於以下問題:Python AES.new方法的具體用法?Python AES.new怎麽用?Python AES.new使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Cryptodome.Cipher.AES
的用法示例。
在下文中一共展示了AES.new方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: encrypt
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import new [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: decrypt_credential
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import new [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
示例3: aesEncrypt
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import new [as 別名]
def aesEncrypt(text, secKey):
pad = 16 - len(text) % 16
# aes加密需要byte類型。
# 因為調用兩次,下麵還要進行補充位數。
# 直接用try與if差不多。
try:
text = text.decode()
except:
pass
text = text + pad * chr(pad)
try:
text = text.encode()
except:
pass
encryptor = AES.new(secKey, 2, bytes('0102030405060708', 'utf-8'))
ciphertext = encryptor.encrypt(text)
ciphertext = base64.b64encode(ciphertext)
return ciphertext
示例4: decrypt_password
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import new [as 別名]
def decrypt_password(user_conf):
cipher_text = user_conf["password"]
encrypted_aes_session_key = user_conf["aes_session_key"]
cipher_aes_nonce = user_conf["cipher_aes_nonce"]
tag = user_conf["tag"]
# Read private key
with open(os.path.join(os.environ["AZTK_WORKING_DIR"], "id_rsa"), encoding="UTF-8") as f:
private_key = RSA.import_key(f.read())
# Decrypt the session key with the public RSA key
cipher_rsa = PKCS1_OAEP.new(private_key)
session_key = cipher_rsa.decrypt(encrypted_aes_session_key)
# Decrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX, cipher_aes_nonce)
password = cipher_aes.decrypt_and_verify(cipher_text, tag)
return password.decode("utf-8")
示例5: parse_SHAR
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import new [as 別名]
def parse_SHAR(chunk, encryption_key, rsa_key):
# TODO: Fake some data and make a test
io = BytesIO(chunk.payload)
id = read_item(io)
encrypted_key = decode_hex(read_item(io))
encrypted_name = read_item(io)
skip_item(io, 2)
key = read_item(io)
# Shared folder encryption key might come already in pre-decrypted form,
# where it's only AES encrypted with the regular encryption key.
# When the key is blank, then there's a RSA encrypted key, which has to
# be decrypted first before use.
if not key:
key = decode_hex(PKCS1_OAEP.new(rsa_key).decrypt(encrypted_key))
else:
key = decode_hex(decode_aes256_plain_auto(key, encryption_key))
name = decode_aes256_base64_auto(encrypted_name, key)
# TODO: Return an object, not a dict
return {'id': id, 'name': name, 'encryption_key': key}
示例6: decode_aes256
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import new [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)
示例7: msl_encrypt
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import new [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')
示例8: decryptData
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import new [as 別名]
def decryptData(key, encryptedData):
"""Decrypts the apk data using the specified AES key"""
aes = AES.new(key, AES.MODE_ECB)
return b''.join(util.unpad(aes.decrypt(c)) for c in util.chunk(encryptedData, constants.blockSize + constants.paddingSize))
示例9: encryptData
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import new [as 別名]
def encryptData(key, data):
"""Encrypts the apk data using the specified AES key"""
aes = AES.new(key, AES.MODE_ECB)
return b''.join(aes.encrypt(util.pad(c, constants.paddingSize)) for c in util.chunk(data, constants.blockSize))
示例10: generate_iv
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import new [as 別名]
def generate_iv() -> bytes:
return Random.new().read(AES.block_size)
示例11: aes_gcm_encrypt_with_iv
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import new [as 別名]
def aes_gcm_encrypt_with_iv(plain_text: bytes, hdr: bytes, key: bytes, iv: bytes):
cipher = AES.new(key=key, mode=AES.MODE_GCM, nonce=iv)
cipher.update(hdr)
cipher_text, mac_tag = cipher.encrypt_and_digest(plain_text)
return mac_tag, cipher_text
示例12: aes_gcm_decrypt_with_iv
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import new [as 別名]
def aes_gcm_decrypt_with_iv(cipher_text: bytes, hdr: bytes, mac_tag: bytes, key: bytes, iv: bytes) -> bytes:
cipher = AES.new(key=key, mode=AES.MODE_GCM, nonce=iv)
cipher.update(hdr)
try:
plain_text = cipher.decrypt_and_verify(cipher_text, mac_tag)
except ValueError:
plain_text = b""
except KeyError:
plain_text = b""
return plain_text
示例13: aes_gcm_encrypt
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import new [as 別名]
def aes_gcm_encrypt(plain_text: bytes, hdr: bytes, key: bytes):
cipher = AES.new(key=key, mode=AES.MODE_GCM)
cipher.update(hdr)
cipher_text, mac_tag = cipher.encrypt_and_digest(plain_text)
nonce = cipher.nonce
return nonce, mac_tag, cipher_text
示例14: aes_gcm_decrypt
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import new [as 別名]
def aes_gcm_decrypt(cipher_text: bytes, hdr: bytes, nonce: bytes, mac_tag: bytes, key: bytes):
cipher = AES.new(key=key, mode=AES.MODE_GCM, nonce=nonce)
cipher.update(hdr)
try:
plain_text = cipher.decrypt_and_verify(cipher_text, mac_tag)
except ValueError:
plain_text = b""
except KeyError:
plain_text = b""
return plain_text
示例15: aes_ctr_decrypt
# 需要導入模塊: from Cryptodome.Cipher import AES [as 別名]
# 或者: from Cryptodome.Cipher.AES import new [as 別名]
def aes_ctr_decrypt(cipher_text: bytes, nonce: bytes, key: bytes):
cipher = AES.new(key=key, mode=AES.MODE_CTR, nonce=nonce)
plain_text = cipher.decrypt(cipher_text)
return plain_text