LoginSignup
0
0

More than 1 year has passed since last update.

暗号化ライブラリTinkをPythonで使ってみる

Posted at

Tinkとは

Google製の暗号化ライブラリです。GitHubのリポジトリはこちら
JavaやC++など、いくつかの言語に対応していますが、今回はPythonを使ってみました。

Tinkのインストール

Pythonの場合、pipを使ってインストールできます。

pip3 install tink

サンプルコード

暗号化と復号化を行う、簡単なサンプルコードを作成してみました。

#!/usr/bin/env python

import tink
from tink import aead

plaintext = b'your data...'
associated_data = b'context'

# 1.機能の初期化処理。目的の機能をregisterメソッドで有効化します。
aead.register()

# 2.鍵のテンプレートを使って、キーセットを生成
key_template = aead.aead_key_templates.AES256_GCM
keyset_handle = tink.new_keyset_handle(key_template)

# 3.プリミティブの生成。このプリミティブのI/Fを介して、暗号化操作を実行します。
aead_primitive = keyset_handle.primitive(aead.Aead)

# 4.暗号化操作。引数はバイト型で指定します。
ciphertext = aead_primitive.encrypt(plaintext, associated_data)
print(ciphertext)

# 5. 復号化操作
plain = aead_primitive.decrypt(ciphertext, associated_data)
print(plain)
0
0
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
0
0