Trusted Design

pyca/cryptographyをインストールする

前提

まず前提で,PCにPythonがインストールされていることを確認してください。Pythonの設定についてはこちらを参照してください。

Pythonのインストール先にパスを通して,pythonコマンドがPCで利用可能になっている必要があります。

設定手順

次の手順でpyca/cryptographyをインストールしていきます。とは言ってもpipコマンドを使うだけなんですけどね。

pyca/cryptographyをインストールするのはpipコマンドを使って行います。pip.exeは(Python3.7の場合)Python37/Scriptsフォルダにありますので,こちらもパスを通しておく必要があります。

pyca/cryptographyがインストールされているのかを確認するには,Pythonでインポートしてみるのが早いです。インストールされていないと,このようなエラーになってしまいます。


>>> from cryptography.fernet import Fernet
Traceback (most recent call last):
  File "", line 1, in 
ModuleNotFoundError: No module named 'cryptography'

それではpipコマンド使ってインストールします。


C:\>pip install cryptography
Collecting cryptography
  Downloading https://files.pythonhosted.org/packages/00/39/088ba8da28dd77582219d4b77263d5aedac37c5c1c31f75859f241b9fcd2/cryptography-2.6.1-cp37-cp37m-win_amd64.whl (1.5MB)
    100% |████████████████████████████████| 1.5MB 2.3MB/s
Collecting six>=1.4.1 (from cryptography)
  Downloading https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Collecting cffi!=1.11.3,>=1.8 (from cryptography)
  Downloading https://files.pythonhosted.org/packages/2f/ad/9722b7752fdd88c858be57b47f41d1049b5fb0ab79caf0ab11407945c1a7/cffi-1.12.3-cp37-cp37m-win_amd64.whl (171kB)
    100% |████████████████████████████████| 174kB 2.8MB/s
Collecting asn1crypto>=0.21.0 (from cryptography)
  Downloading https://files.pythonhosted.org/packages/ea/cd/35485615f45f30a510576f1a56d1e0a7ad7bd8ab5ed7cdc600ef7cd06222/asn1crypto-0.24.0-py2.py3-none-any.whl (101kB)
    100% |████████████████████████████████| 102kB 3.0MB/s
Collecting pycparser (from cffi!=1.11.3,>=1.8->cryptography)
  Downloading https://files.pythonhosted.org/packages/68/9e/49196946aee219aead1290e00d1e7fdeab8567783e83e1b9ab5585e6206a/pycparser-2.19.tar.gz (158kB)
    100% |████████████████████████████████| 163kB 2.8MB/s
Installing collected packages: six, pycparser, cffi, asn1crypto, cryptography
  Running setup.py install for pycparser ... done
Successfully installed asn1crypto-0.24.0 cffi-1.12.3 cryptography-2.6.1 pycparser-2.19 six-1.12.0
You are using pip version 19.0.3, however version 19.1.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

C:\>

それではもう一度cryptographyをインポートしてみます。


C:\>python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from cryptography.fernet import Fernet
>>>

大成功!

pyca/cryptographyの動作確認

それでは簡単なサンプルを動かしてみます。


>>> from cryptography.fernet import Fernet
>>> key = Fernet.generate_key()
>>> f = Fernet(key)
>>> token = f.encrypt(b"A really secret message. Not for prying eyes.")
>>> token
b'gAAAAABc6OLBLKOXmlJ3NQXafBcZVcOERki22fVezpSDWhS2fIljTQ4zNYGqJh-Kkr2_rAlgx8tYKl_JWUFoBm0eNoFiC5ADPPm9218X_vP0EPIFVQAkJkaLooYiVukR2x06knS3F89D'
>>> f.decrypt(token)
b'A really secret message. Not for prying eyes.'
>>>

確かにデータの暗号化/復号ができているようです。pyca/cryptographyはFernetというAES128bitプラスHMACを生成できる方式を利用して簡単に暗復号を実現しています。

2行目で共通鍵を作り,4行目で暗号化しています。暗号化したtokenを7行目で復号しています。