def generateKey(params): """ Generate a new random decrypt key for RSA based on the given params. :param RsaKeyParams params: The key params with the key size (in bits). :return: The new decrypt key (PKCS8-encoded private key). :rtype: DecryptKey """ privateKey = TpmPrivateKey.generatePrivateKey(params) return DecryptKey(privateKey.toPkcs8())
def generateKey(params): """ Generate a new random decrypt key for AES based on the given params. :param AesKeyParams params: The key params with the key size (in bits). :return: The new decrypt key. :rtype: DecryptKey """ # Convert the key bit size to bytes. nBytes = int(round(params.getKeySize() / 8)) key = bytearray(nBytes) for i in range(nBytes): key[i] = _systemRandom.randint(0, 0xff) decryptKey = DecryptKey(Blob(key, False)) return decryptKey
def generateKey(params): """ Generate a new random decrypt key for RSA based on the given params. :param RsaKeyParams params: The key params with the key size (in bits). :return: The new decrypt key (PKCS8-encoded private key). :rtype: DecryptKey """ privateKey = rsa.generate_private_key(public_exponent=65537, key_size=params.getKeySize(), backend=default_backend()) privateKeyDer = privateKey.private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption()) return DecryptKey(Blob(privateKeyDer, False))