Published Date : 2021年1月7日11:32

Pythonでシーザー暗号 Part 4
Try the Caesar cipher in Python Part 4


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



前回の問題を解決する方法の一つとして、暗号生成機をスタンドアローンのアプリとして作成してみましょう。

One way to solve the previous problem is to create a stand-alone application for the encryption generator.

Android Studioのインストール方法とアプリ作成方法やエミュレーターのダウンロード方法等は以前に作成した動画を参考にしてください。

For instructions on how to install Android Studio, how to create apps, and how to download the emulator, please refer to the video I made before.

適当なフォルダを指定して、新しいプロジェクトを作成します。

Create a new project in the appropriate folder.

mainのXMLファイルを開き、デザインモードでテキストやボタンを配置していきます。

Open the main XML file, switch to design mode, and place the text and buttons.

秘密鍵を選択できるように、スピナーを配置します。

Position the spinner so you can select the private key.

細かいレイアウトの設定やID等はXMLに直接書くことにします。

I will write the detailed layout settings and IDs directly in XML.

文字の表示内容はリソースフォルダのvaluesフォルダ内のstrings.xmlに書くことが推奨されています。

It is recommended that you write the text in strings.xml in the values folder of the resource folder.

エディットテキストに外枠を付けるため、リソースフォルダのdrawableフォルダ内に新たにborder.xmlを作成して、その中に設定を書き込みます。

To outline the edit text, create a new border.xml file in the drawable folder of the resources folder and write the settings to it.

アンドロイドエミュレーターでアプリのレイアウトを確認してみましょう。

Let's check the layout of the app in the Android emulator.

ではメインとなるJavaファイルに処理の内容を作成していきましょう。

Let's write the process in the main Java file.

まず、adapterを作成して、スピナーに1から25までの数を設定しましょう。

First, create an adapter and set the spinner to a number between 1 and 25.

MainActivity.java
-----------------------------------------------
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
-----------------------------------------------

デフォルトの秘密鍵は3になるように設定します。

Set the default private key to 3.

MainActivity.java
-----------------------------------------------

for (int i = 0; i < 26; i++) {
    adapter.add(i);
}

spinner = findViewById(R.id.selectEncPvkey);
spinner.setAdapter(adapter);

defaultPvKeyPos = 3;
spinner.setSelection(defaultPvKeyPos);

-----------------------------------------------

実際に暗号文を生成するJavaクラスファイルを別で作成しましょう。

Let's create a separate Java class file that actually generates the ciphertext.

MainActivity.javaと同じ階層に作成します。

Create it in the same hierarchy as MainActivity.java.

Javascriptの時と同じで、多少のシンタックスの違いはありますが、基本的には処理内容に変わりはありません。

As with Javascript, there are some differences in syntax, but basically the process is the same.

ネット等でJavaの書き方を調べればすぐに作成できます。

If you look up how to write Java on the Internet, you can create it right away.

再度MainActivity.javaに戻り、エディットテキストやテキストビュー、ボタンに対する処理を記述していきます。

Return to MainActivity.java again and write the action for the edit text, text view, and button.

MainActivity.java
-----------------------------------------------
ecpt = new Encrypto();

editText = findViewById(R.id.inputPlaintext);
textView = findViewById(R.id.dectext);

encryptButton = findViewById(R.id.encryption);
-----------------------------------------------

androidアプリ作成に必要な、組み込みのライブラリ等はコードを書けばある程度自動的にインポートされるようになっています。

The built-in libraries required to create Android apps are imported to some extent automatically when you write code.

後は、暗号化ボタンが押された時の処理を書いていくだけです。

All you have to do is write what happens when the encryption button is pressed.

次は復号化のアクティビティを作成していきましょう。

Now let's create a decryption activity.

暗号化のアクティビティと同じように作成していけば大丈夫です。

Just create it as you would any encryption activity.

シフトボタンが押されたら、別のアクティビティに移動できるようにintentを利用しましょう。

When the shift button is pressed, use intent to move to another activity.

レイアウトも異なるので、activity_main.xmlと同じ階層にレイアウト用のXMLファイルを作成しましょう。

Since the layout is different, let's create an XML file for the layout in the same hierarchy as activity_main.xml.

strings.xmlにも変更の追加をします。

Add the changes to strings.xml.

後はMainActivity.javaの内容を復号化のアクティビティのJavaファイルにコピペして、ID名や変数名を変えるだけです。

All you have to do is copy and paste the contents of MainActivity.java into the decryption activity's Java file and change the ID and variable names.

復号化用のJavaファイルも同じように作成します。

Create the same contents in the decrypted Java file.

復号化する際は文字のシフト方向が逆になることに注意してください。

Note that the character shift is reversed when decrypting.

AndroidManifest.xmlに追加したアクティビティを登録します。

Register the activity you have added to the AndroidManifest.xml.

準備ができたら、アプリの動作を確かめてみましょう。

When you're ready, check out how the app works.

ではFlaskを使って簡単にテキストを送受信するだけのWEBアプリを作成してみましょう。

Now let's create a web app that simply sends and receives text using Flask.

送受信ができるならどんな内容でも構いません。適当に作ってください。

As long as I can send and receive, any contents will do. Please make it as you like.

Androidアプリを使用して暗号文を生成し、WEBアプリを通して暗号文を送信してみましょう。

Let's generate the ciphertext using the Android application and send it through the WEB application.

Wiresharkでも通信内容を確認してみましょう。

Let's check the communication in Wireshark.



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

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