Published Date : 2021年6月13日18:09

001 Pythonでビットコインを学ぶ (cryptography rsa)
001 Use python to learn bitcoin (cryptography rsa)


This blog has an English translation


ニコニコ動画にアップした動画のまとめ記事です。

This is a summary blog post about a video I uploaded to NicoNico.

細かい部分は動画を参考にしてください。

Please refer to the video for details.


目次

Table of Contents




① 動画の説明
① Video Description



Pythonのcryptographyモジュールを使ったRSAとHash

RSA and Hash using Python's cryptography module

ブラウザで[cryptography.io]を検索してください。

In your browser, search for [cryptography.io].

[pip install cryptography]でこのモジュールをインストールします。

[pip install cryptography] to install this module.

pip install cryptography

[ipython]を使ってcryptographyモジュールを試してみましょう。

Try the cryptography module with [ipython].

ipython

まずはRSAを使用して、プライベートキーを作成してみましょう。

Let's start by creating a private key using RSA.

from cryptography.hazmat.primitives.asymmetric import rsa
private_key = rsa.generate_private_key(
    public_exponent=65537,
    key_size=2048,
)

公開鍵はプライベートキーオブジェクトの[public_keyメソッド]を使用して作成できます。

Public keys can be created using the [public_key method] of the private key object.

ではこの秘密鍵を使ってメッセージに対して署名をしてみましょう。

Let's sign a message with this private key.

署名とは、このメッセージが間違いなく本人から送信されたものである証明をする為に行います。

Signing is done to prove that this message was definitely sent by the person.

ここではメッセージをSHA256というハッシュ関数を使用してハッシュ値を導き出した後、秘密鍵を使ってそのハッシュ値を暗号化しています。

We use a hash function called SHA256 to derive a hash value for the message, and then use a secret key to encrypt the hash value.

このバイト列に含まれている[バックスラッシュX]はその後に続く二桁の英数字が十六進数であることを表しています。

The [backslash X] in the byte sequence indicates that the next two digits are hexadecimal.

署名のビット数を調べてみると、256ビットあることが確認できます。

If you look at the number of bits in the signature, you can see that there are 256 bits.

SHA256というハッシュ関数はどんな長さのデータ(最長で2の64乗-1ビットまで)も256ビットのハッシュ値を生成する為、語尾に256の数字が付けられています。

The SHA 256 hash function generates a 256 bit hash value for any length of data (But there is a maximum. It is 2 to the power of 64 minus one bits.), so it has a 256 digit suffix.

では公開鍵と秘密鍵を生成する関数と、それらを使って署名を行う関数を作成していきましょう。

Now let's create functions to generate public and private keys and use them for signing.

ところで、署名に使われるメッセージには頭に[b]が付いていることが分かりますか?

By the way, do you see that the message used for signing has [b] on its head?

これはこのメッセージが文字列ではなく、バイト列であることを表しています。

This indicates that the message is a byte string, not a string.

試しに文字列を関数に渡してみましょう。

Try passing a string to a function.

このように、メッセージはバイト列である必要があります。

Thus, the message must be a sequence of bytes.

ただの文字列をバイト列に変換するには、bytesメソッドを使用しますが、エンコードを指定しなければなりません。

To convert a plain string to a byte, you must specify the encoding by using the bytes method.

ところで、先ほどSHA256はどんな長さのデータでも256ビットのハッシュ値を生成すると説明しました。

By the way, I mentioned earlier that SHA 256 generates a 256 bit hash value for any length of data.

それを様々な長さのメッセージを使って確かめてみましょう。

Check it out with messages of various lengths.

それでは、署名されたメッセージが正しいかどうかを照合して検証してみましょう。

Now let's verify that the signed message is correct.

[cryptography.io]のドキュメントには、正しくない署名が検証された時に例外が発生されると書いてあります。

The [cryptography.io] documentation states that an exception is raised when an incorrect signature is verified.

なので、InvaildSignatureをインポートする必要があります。

Therefore, you must import InvaildSignature.

これをtry except文に使うことによって、署名の検証の状態によって処理を分けることができるようになります。

This can be used in a try except statement to separate the processing according to the verification status of the signature.

それでは間違ったメッセージを検証してみましょう。

Let's examine the wrong message.

もちろん、平文の暗号化や復号化もできます。

Of course, you can also encrypt and decrypt plaintext.

バイト列になっている文字列をデコードして普通の文字列にすることもできます。

You can also decode a string of bytes into an ordinary string.



以上です。お疲れ様です。

That's all. Thank you for your hard work.