我尝试在将敏感数据移动到S3上的云存储并将其转移到redshift之前使用客户端加密对其进行加密。我试着使用AWS提供的示例代码,在使用了它之后,我让它运行而没有返回错误,但是,它没有做任何我能知道的事情,因为没有任何东西可以打印出来。
def cycle_string(key_arn, source_plaintext, botocore_session=None):
"""Encrypts and then decrypts a string using a KMS customer master key (CMK)
:param str key_arn: [encryption key]
(http://docs.aws.amazon.com/kms/latest/developerguide/viewing-keys.html)
:param bytes source_plaintext:
:param botocore_session: Existing botocore session
:type botocore_session: botocore.session.Session
"""
# Create a KMS master key provider
kms_kwargs = dict(key_ids=[key_arn])
if botocore_session is not None:
kms_kwargs['botocore_session'] = botocore_session
master_key_provider =
aws_encryption_sdk.KMSMasterKeyProvider(**kms_kwargs)
# Encrypt the plaintext source data
ciphertext, encryptor_header = aws_encryption_sdk.encrypt(
source=source_plaintext,
key_provider=master_key_provider
)
print('Ciphertext: ', ciphertext)
# Decrypt the ciphertext
cycled_plaintext, decrypted_header = aws_encryption_sdk.decrypt(
source=ciphertext,
key_provider=master_key_provider
)
# Verify that the "cycled" (encrypted, then decrypted) plaintext is identical to the source
# plaintext
assert cycled_plaintext == source_plaintext
# Verify that the encryption context used in the decrypt operation includes all key pairs from
# the encrypt operation. (The SDK can add pairs, so don't require an exact match.)
#
# In production, always use a meaningful encryption context. In this sample, we omit the
# encryption context (no key pairs).
assert all(
pair in decrypted_header.encryption_context.items()
for pair in encryptor_header.encryption_context.items()
)
print('Decrypted: ', cycled_plaintext)
我是Python和加密的新手,所以我可能缺少一些语法,或者只是缺乏对其工作原理的了解。这是在python中对AWS使用客户端加密的最佳方法吗?如果是这样,为什么这个代码不返回任何内容?
更新: 我用了一种稍微不同的方法
^{pr2}$现在它打印出来了,但我不知道如何判断数据是否真的加密了
您可以使用pycrypto:
哪个返回:
^{pr2}$该函数不返回任何内容,因为它不包含返回调用。在
该函数的目的是演示如何使用库对明文进行加密,然后对生成的密文消息进行解密,从而证明该循环产生了相同的明文。在
如果您在实践中使用这种方法,那么在任何给定的时间都需要该周期的一半(即:加密或解密,但不能同时使用)。在
相关问题 更多 >
编程相关推荐