LoginSignup
3
7

More than 3 years have passed since last update.

pythonでAWSのKMSで暗号化・複合化

Posted at

はじめに

pythonでAWSのKMS(Key Management Service)を使用して暗号化・複合化をしたのでメモ。

暗号化

下記の関数を使用する。
keyIdは、マネジメントコンソールから確認できる「キーID」。
plaintextが暗号化したい文字列。

from boto3 import client
import base64

# 暗号化した文字列を取得
def getEncryptStr(keyId, plaintext):
    kms = boto3.client('kms')
    enc = kms.encrypt(
                    KeyId = keyId,
                    Plaintext = plaintext
                )['CiphertextBlob']

    return base64.b64encode(enc).decode('utf-8')

複合化

下記関数を使用する。

from boto3 import client
import base64

# 複合化した文字列を取得
def getDecryptStr(plaintext):
    plaintext = base64.b64decode(plaintext)
    kms = boto3.client('kms')
    return kms.decrypt(
                CiphertextBlob = plaintext
            )['Plaintext'].decode('utf-8')
3
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
7