当前位置: 首页>>代码示例>>Python>>正文


Python AES.new方法代码示例

本文整理汇总了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) 
开发者ID:CastagnaIT,项目名称:plugin.video.netflix,代码行数:19,代码来源:default_crypto.py

示例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 
开发者ID:CastagnaIT,项目名称:plugin.video.netflix,代码行数:25,代码来源:credentials.py

示例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 
开发者ID:HuberTRoy,项目名称:MusicBox,代码行数:25,代码来源:netEaseEncode.py

示例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") 
开发者ID:Azure,项目名称:aztk,代码行数:19,代码来源:create_user.py

示例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} 
开发者ID:Keeper-Security,项目名称:Commander,代码行数:24,代码来源:parser.py

示例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) 
开发者ID:Keeper-Security,项目名称:Commander,代码行数:18,代码来源:parser.py

示例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') 
开发者ID:truedread,项目名称:pymsl,代码行数:37,代码来源:utils.py

示例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)) 
开发者ID:ma1co,项目名称:Sony-PMCA-RE,代码行数:6,代码来源:__init__.py

示例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)) 
开发者ID:ma1co,项目名称:Sony-PMCA-RE,代码行数:6,代码来源:__init__.py

示例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) 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:4,代码来源:aes_handler.py

示例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 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:7,代码来源:aes_handler.py

示例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 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:12,代码来源:aes_handler.py

示例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 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:8,代码来源:aes_handler.py

示例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 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:12,代码来源:aes_handler.py

示例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 
开发者ID:ontio,项目名称:ontology-python-sdk,代码行数:6,代码来源:aes_handler.py


注:本文中的Cryptodome.Cipher.AES.new方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。