Help us understand the problem. What is going on with this article?

[GCP] Cloud KMS(CLOUD KEY MANAGEMENT SERVICE)で暗号化と復号

More than 1 year has passed since last update.

概要

GCPサービスのCloud KMSを使い、暗号化と復号を行う。gloudコマンドから行う。
ファイルの暗号化と復号が一般的だが、標準入力を使う方法も紹介する。

KMSのサービスについては、公式ドキュメントを参照。

事前準備

  • GCPのWebコンソールから(gcloudコマンドからでも良いが)、キーリングとキーを作成する。

  • 適当なファイルを作成する。

$ echo "Hello" > hello.txt

ファイルの暗号化と復号

# 暗号化
$ gcloud kms encrypt --ciphertext-file=hello.txt.enc --plaintext-file=hello.txt \
    --key my-key --keyring my-ring --location global --project my-project

# 復号
$ gcloud kms decrypt --ciphertext-file=hello.txt.enc --plaintext-file=hello.txt.dec \
    --key my-key --keyring my-ring --location global --project my-project

$ cat hello.txt.dec
Hello

標準入力からデータを受け取って、暗号化と復号

# 暗号化
$ echo -n "Hello" | gcloud kms encrypt \
  --location global \
  --keyring my-ring \
  --key my-key \
  --plaintext-file - \
  --ciphertext-file - \
  --project my-project
 | base64

# 復号(復号するデータは自分の環境のものに置き換える)
$ echo -n "my-encrypted-data" | base64 -D | gcloud kms decrypt \
 --location global \
 --keyring my-ring \
 --key my-key \
 --ciphertext-file - \
 --plaintext-file -

参考

https://www.terraform.io/docs/providers/google/d/google_kms_secret.html

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away