• Skip to main content
  • Skip to primary sidebar

学習記録

河副 太智

暗号の基礎を恋愛短編集風に語る

2018年4月19日 by 河副 太智 Leave a Comment

 

恋愛短編集風にシーザー暗号の基礎を紹介します。
ブロックチェーンや暗号通貨の基礎に無理矢理繋げます

昔々ドイツの田舎にとある純真無垢な女の子がいました
ある日噂好きの友達から「好きな人は誰?」とメールで聞かれました

純真無垢な女の子は「andy君」と答えたくてもそのメールが
お父さんに見られたら大変です。
女の子のお父さんのスマホチェックはとても厳しいからです。

そこで純真無垢な女の子は[andy]という文字を暗号化して
噂好きの友達にメールしようと試みました。

そして無事[andy]という文字を[DQGB]に変換する事ができ、
メールに暗号で「私の好きな人は[DQGB]君よ」と言いました。

予め暗号解読のキーを知っている噂好きの友達は
すぐに[DQGB]から[andy]に変換する事ができました。

これで純真無垢な女の子はお父さんにばれないように
好きな男の子の名前を噂好きの友達に伝える事ができました。

 

しかし、友達は噂話が大好きです。
翌日学校中にばらされてしまい大きな噂になりました。

「これじゃあ暗号の意味がないじゃない…」
もはや純真無垢な女の子はショック過ぎて登校できなくなってしまいました。

しかし、それを聞いたandy君は元々純真無垢な女の子を好きでした
結果オーライで2人は恋人となりドイツの田舎で幸せになったとさ。

めでたしめでたし…

 

 

 

では本文に入ります。

 

andyという人名(アンディー君)を
暗号でDQGBになる仮定を紹介します。

andyというのは暗号化する前の文字なので「平文」と呼び
DQGBは暗号化された文字なので「暗号文」と呼びます

 

まずは暗号のベースとなるアルファベット一覧を作ります。
小文字アルファベットに数字1~26を割り当てていきます
(小文字アルファベット一覧の数字は赤にします)

a b c d ~ w x y z
1 2 3 4 ~ 23 24 25 26

 

同じように大文字アルファベットにも数字1~26を割り当てる
(大文字アルファベット一覧の数字は青にします)

A B C D ~ W X Y Z
1 2 3 4 ~ 23 24 25 26

 

そして

 

「小文字アルファベット一覧」の下段の数字に「3を足す」
(ポイントはwの26までいったら次のxは最初に戻って1になる)

a b c d ~ w x y z
4 5 6 7 ~ 26 1 2 3

 

「小文字アルファベット一覧(3を足した)」と
「大文字アルファベット一覧(何も足していない)」を合体させてみます

すると以下のようになります。

 

a b c d ~ w x y z
4 5 6 7 ~ 26 1 2 3
A B C D ~ W X Y Z
1 2 3 4 ~ 23 24 25 26

 

上記の図の中から探してみてください

aは4
Dは4
となっておりますよね?

 

これはaとDいう小文字と大文字のアルファベットは
4という数字を基にして繋がっているという事です

 

分かり易くする為に
aに合わせて大文字アルファベット一覧をずらします

a b c d ~ w x y z
4 5 6 7 ~ 26 1 2 3
D E F G ~ Z A B C
4 5 6 7 ~ 26 1 2 3

こうしてみるとaとDいう小文字と大文字のアルファベットは
4という数字を基にして繋がっているという事がわかると思います

赤数字対応のアルファベット小文字は暗号化前の「平文」
青文字対応のアルファベット大文字は暗号化後の「暗号文」

bはE

cはF

dはG

wはZ

xはA

yはB

zはC

 

こんな風に一方のアルファベットに数字を割り当てて
もう一方のアルファベットにずらした数字を割り当てて
andyという平文の人名を暗号でDQGBになります。

 

つまり噂好きの友達は「3文字ずらしルール」というキーを
事前に知っていたので暗号を平文に変換する事ができたのです。

 

よくわかんねぇよ!という方、
それ間違ってねぇ?という方
いらっしゃいましたらコメント下さい

Filed Under: 暗号学

周期表をbokehでhtml出力

2018年4月18日 by 河副 太智 Leave a Comment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure
from bokeh.sampledata.periodic_table import elements
from bokeh.transform import dodge, factor_cmap
 
output_file("period.html")
 
periods = ["I", "II", "III", "IV", "V", "VI", "VII"]
groups = [str(x) for x in range(1, 19)]
 
df = elements.copy()
df["atomic mass"] = df["atomic mass"].astype(str)
df["group"] = df["group"].astype(str)
df["period"] = [periods[x-1] for x in df.period]
df = df[df.group != "-"]
df = df[df.symbol != "Lr"]
df = df[df.symbol != "Lu"]
 
cmap = {
    "alkali metal"         : "#a6cee3",
    "alkaline earth metal" : "#1f78b4",
    "metal"                : "#d93b43",
    "halogen"              : "#999d9a",
    "metalloid"            : "#e08d49",
    "noble gas"            : "#eaeaea",
    "nonmetal"             : "#f1d4Af",
    "transition metal"     : "#599d7A",
}
 
source = ColumnDataSource(df)
 
p = figure(title="Periodic Table (omitting LA and AC Series)", plot_width=1000, plot_height=450,
           tools="", toolbar_location=None,
           x_range=groups, y_range=list(reversed(periods)))
 
p.rect("group", "period", 0.95, 0.95, source=source, fill_alpha=0.6, legend="metal",
       color=factor_cmap('metal', palette=list(cmap.values()), factors=list(cmap.keys())))
 
text_props = {"source": source, "text_align": "left", "text_baseline": "middle"}
 
x = dodge("group", -0.4, range=p.x_range)
 
r = p.text(x=x, y="period", text="symbol", **text_props)
r.glyph.text_font_style="bold"
 
r = p.text(x=x, y=dodge("period", 0.3, range=p.y_range), text="atomic number", **text_props)
r.glyph.text_font_size="8pt"
 
r = p.text(x=x, y=dodge("period", -0.35, range=p.y_range), text="name", **text_props)
r.glyph.text_font_size="5pt"
 
r = p.text(x=x, y=dodge("period", -0.2, range=p.y_range), text="atomic mass", **text_props)
r.glyph.text_font_size="5pt"
 
p.text(x=["3", "3"], y=["VI", "VII"], text=["LA", "AC"], text_align="center", text_baseline="middle")
 
p.add_tools(HoverTool(tooltips = [
    ("Name", "@name"),
    ("Atomic number", "@{atomic number}"),
    ("Atomic mass", "@{atomic mass}"),
    ("Type", "@metal"),
    ("CPK color", "$color[hex, swatch]:CPK"),
    ("Electronic configuration", "@{electronic configuration}"),
]))
 
p.outline_line_color = None
p.grid.grid_line_color = None
p.axis.axis_line_color = None
p.axis.major_tick_line_color = None
p.axis.major_label_standoff = 0
p.legend.orientation = "horizontal"
p.legend.location ="top_center"
 
show(p)

 

Filed Under: Bokeh, グラフ Tagged With: Bokeh

from PIL import Imageから出てくるエラー文 ”Pillow or PIL”

2018年4月14日 by 河副 太智 Leave a Comment

from PIL import Imageから出てくるエラー文
ImportError: The _imaging extension was built for another version of Pillow or PIL
で長時間引っかかった

調べてみるとpython 3.6 自体に問題があるようで
C:\Anaconda\lib\site-packages\PIL\Image.pyの

1
2
3
if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
raise ImportError("The _imaging extension was built for another " "
version of Pillow or PIL")

の部分を

1
2
3
if <strong><span style="color: #ff0000;">core.</span></strong>PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
raise ImportError("The _imaging extension was built for another " "
version of Pillow or PIL")

に変更したら直りました。

Filed Under: python3 Tagged With: PIL, Pillow, The _imaging extension was built for another version of Pillow or PIL

Bokehで動かせるグラフを作成

2018年4月10日 by 河副 太智 Leave a Comment

Bokehはインタラクティブなグラフを作成できるデータ可視化ライブラリです。

 

インストール

インストール方法Anadonda使用

1
conda install bokeh

pipの場合

1
pip install bokeh

詳細は、公式ドキュメントを御覧下さい。

 

更に容量の大きなサンプルデータをダウンロード

1
$bokeh sampledata

あるいは、Pythonのインタープリタ上でもダウンロード可能

1
2
import bokeh.sampledata
bokeh.sampledata.download()

 

テスト

Jupyter notebookで実行 ※現時点ではJupyter labでは使用不可

1
2
3
4
from bokeh.io import output_notebook
from bokeh.io import show
output_notebook()

BokehJS 0.12.15 successfully loaded.と出ればOK

 

 

スライダー

次にスライダーの設定をしますIpywidgetsをインポート

pip

1
2
pip install ipywidgets
jupyter nbextension enable --py widgetsnbextension

conda

1
conda install -c conda-forge ipywidgets

 

コード

1
2
3
from ipywidgets import IntSlider
slider = IntSlider(value=50)#valueの値は可変
slider

 

bokehスライダー

 

これでJupyter上でスライダーが動きます

この状態では中心が50となりスライダーを右端に移動させると
100になります。

画面上でvalueを変更する以外にも以下のように記述すれば
スライダーの位置の変更が可能です。

1
slider.value = 100

 

手動でsliderを移動させた場合

1
slider.value

逆にスライダーの位置の値を取得する事も可能

 

 

HTMLでリアルタイム表示

1
2
3
4
from ipywidgets import HTML
 
text = HTML("スライダーの値は{}".format(slider.value))
text

スライダーの値は50と表示されます


http://harmonizedai.com/article/wp-content/uploads/2018/04/bokenhtml.mp4

 

 

 

bqplot(二次元プロット)

bqplotドキュメント

 

pip

1
2
pip install bqplot
jupyter nbextension enable --py --sys-prefix bqplot

conda

1
conda install -c conda-forge bqplot

 

Filed Under: グラフ

プログラミング作業休憩タイマー

2018年4月10日 by 河副 太智 Leave a Comment

プログラミングをやりすぎると首、肩がこってパフォーマンスが最悪になるので
一定時間経つと音を鳴らして、その間に目を閉じてリラックスするようにした

10分作業したら休憩開始の音
2分休憩したら作業開始の音

これを10回繰り返したらタイマー終了

1
2
3
4
5
6
7
8
9
10
11
12
import winsound as ws
from time import sleep
 
a = 0
while a < 10:
    sleep(600)
    ws.Beep(523, 500)
    sleep(120)
    ws.PlaySound( 'SystemAsterisk', ws.SND_ALIAS )
    a += 1
    
ws.PlaySound( 'SystemExclamation', ws.SND_ALIAS )

作業音の種類はこちらを参照させていただきました

Filed Under: python3

sparse matrixで0以外の値の位置を表示

2018年2月27日 by 河副 太智 Leave a Comment

Netflixのユーザーの視聴データがあったとして、
どの映画を見たのかを表すデータを使うと映画タイトルの
全てが表示され、凄まじい量になる。

基本的にNetflixユーザーは全部の映画を見るわけではないので
当然データセットは0だらけになる

sparse matrixではデータセットが0以外のものの
arrayの位置を返す

1
2
3
4
5
6
7
8
9
10
11
12
13
# Load libraries
import numpy as np
from scipy import sparse
 
# Create a matrix
matrix = np.array([[0, 0],
                   [0, 1],
                   [3, 0]])
 
# Create compressed sparse row (CSR) matrix
matrix_sparse = sparse.csr_matrix(matrix)
 
print(matrix_sparse)

結果

1
2
(1, 1)    1
(2, 0)    3

 

Filed Under: Numpy, scipy

  • « Go to Previous Page
  • Page 1
  • Interim pages omitted …
  • Page 20
  • Page 21
  • Page 22
  • Page 23
  • Page 24
  • Interim pages omitted …
  • Page 66
  • Go to Next Page »

Primary Sidebar

カテゴリー

  • AWS
  • Bootstrap
  • Dash
  • Django
  • flask
  • GIT(sourcetree)
  • Plotly/Dash
  • VPS
  • その他tool
  • ブログ
  • プログラミング
    • Bokeh
    • css
    • HoloViews
    • Jupyter
    • Numpy
    • Pandas
    • PosgreSQL
    • Python 基本
    • python3
      • webアプリ
    • python3解説
    • scikit-learn
    • scipy
    • vps
    • Wordpress
    • グラフ
    • コマンド
    • スクレイピング
    • チートシート
    • データクレンジング
    • ブロックチェーン
    • 作成実績
    • 時系列分析
    • 機械学習
      • 分析手法
      • 教師有り
    • 異常値検知
    • 自然言語処理
  • 一太郎
  • 数学
    • sympy
      • 対数関数(log)
      • 累乗根(n乗根)
    • 暗号学

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in