如何将公有密钥加密与 Python 中的 AWS 加密开发工具包结合使用?



我在这个问题上有点出海,所以我想知道是否有人可以提供帮助。

有谁知道如何使用公钥加密/解密,使用PEM格式的RSA密钥?

如果我在两个方向上使用私钥,我可以让它工作,我可以让公钥加密,但如果我想使用公钥加密和私钥解密,我不知道如何构建脚本来让它工作。我看到基于 Java 的 SDK 版本中有一个示例,但我什至无法从中弄清楚。

谁能带领我走向正确的方向?

我正在使用公钥的加密过程的一些示例代码:

import os
import aws_encryption_sdk
from aws_encryption_sdk.internal.crypto import WrappingKey
from aws_encryption_sdk.key_providers.raw import RawMasterKeyProvider
from aws_encryption_sdk.identifiers import WrappingAlgorithm, EncryptionKeyType

class StaticPublicMasterKeyProvider(RawMasterKeyProvider):
provider_id = 'static-public'
def __init__(self, **kwargs):
self._public_keys = {}
def _get_raw_key(self, key_id):

with open("public_key.pem", "rb") as key_file:
public_key = key_file.read()
self._public_keys[key_id] = public_key
return WrappingKey(
wrapping_algorithm=WrappingAlgorithm.RSA_OAEP_SHA512_MGF1,
wrapping_key=public_key,
wrapping_key_type=EncryptionKeyType.PUBLIC
)

if __name__ == '__main__':

source_file = r'myfile.jpg'
source_file_enc = source_file + '.encrypt'    

public_key_id = os.urandom(8)
master_key_provider = StaticPublicMasterKeyProvider()
master_key_provider.add_master_key(public_key_id)
with open(source_file, 'rb') as sf, open(source_file_enc, 'wb') as sfe:
with aws_encryption_sdk.stream(
mode='e',
source=sf,
key_provider=master_key_provider
) as encryptor:
for chunk in encryptor:
sfe.write(chunk)

我已经查看了 AWS 上的 python 示例,它们在两个方向上都使用私有密钥。

任何帮助将不胜感激。

编辑:文档链接:

AWS 加密开发工具包开发人员指南

生成 RSA 密钥但使用私钥的 Python 示例

使用 RSA 公钥的 Java 示例

注意:这两个示例使用多个密钥提供程序,但仍包含 RSA 密钥

好的,我终于得到了我需要的例子。 对于当前上下文,当前示例仅驻留在 github 上的功能分支中(因此将来要小心,因为此链接可能会断开。 您可能需要在 master 中搜索以找到所需的示例(:

https://github.com/aws/aws-encryption-sdk-python/blob/keyring/examples/src/master_key_provider/multi/aws_kms_with_escrow.py

它的内涵可以描述如下(直接来自上面的例子(:

# Create the encrypt master key that only has access to the public key.
escrow_encrypt_master_key = RawMasterKey(
# The provider ID and key ID are defined by you
# and are used by the raw RSA master key
# to determine whether it should attempt to decrypt
# an encrypted data key.
provider_id="some managed raw keys",  # provider ID corresponds to key namespace for keyrings
key_id=b"my RSA wrapping key",  # key ID corresponds to key name for keyrings
wrapping_key=WrappingKey(
wrapping_key=public_key_pem,
wrapping_key_type=EncryptionKeyType.PUBLIC,
# The wrapping algorithm tells the raw RSA master key
# how to use your wrapping key to encrypt data keys.
#
# We recommend using RSA_OAEP_SHA256_MGF1.
# You should not use RSA_PKCS1 unless you require it for backwards compatibility.
wrapping_algorithm=WrappingAlgorithm.RSA_OAEP_SHA256_MGF1,
),
)
# Create the decrypt master key that has access to the private key.
escrow_decrypt_master_key = RawMasterKey(
# The key namespace and key name MUST match the encrypt master key.
provider_id="some managed raw keys",  # provider ID corresponds to key namespace for keyrings
key_id=b"my RSA wrapping key",  # key ID corresponds to key name for keyrings
wrapping_key=WrappingKey(
wrapping_key=private_key_pem,
wrapping_key_type=EncryptionKeyType.PRIVATE,
# The wrapping algorithm MUST match the encrypt master key.
wrapping_algorithm=WrappingAlgorithm.RSA_OAEP_SHA256_MGF1,
),
)

如果需要,可以将escrow_encrypt_master_key添加到密钥环中,以提供多个密钥来加密有效负载。

我希望这对将来的某人有所帮助。

谢谢

最新更新