教員用WWWサービス - Mathematica 3...4 7 Literacy 2 Mathematica 暗号化の方式...

19
1 1 Literacy 2 Mathematica Mathematica 3 Hiroshi Toyoizumi Univ. of Aizu [email protected] REFERENCES[1] 量子コンピューティング C.P Williams [2] ハンドアウト 佐川先生 [3] コンピュータ理工学のすすめ講義資料 豊泉 2 Literacy 2 Mathematica 今日のテーマ Mathematicaを使って、公開鍵暗号方式による暗号 化をやってみよう。 Mathematicaを使って、公開鍵暗号方式による暗号 をクラックしてみよう。

Transcript of 教員用WWWサービス - Mathematica 3...4 7 Literacy 2 Mathematica 暗号化の方式...

1

1

Literacy 2 Mathematica

Mathematica 3

Hiroshi ToyoizumiUniv. of Aizu

[email protected]

[1] 量子コンピューティング C.P Williams[2] ハンドアウト 佐川先生

[3] コンピュータ理工学のすすめ講義資料 豊泉

2

Literacy 2 Mathematica

今日のテーマ

① Mathematicaを使って、公開鍵暗号方式による暗号化をやってみよう。

② Mathematicaを使って、公開鍵暗号方式による暗号をクラックしてみよう。

2

3

Literacy 2 Mathematica

暗号技術によるセキュリティ強化

① 暗号化:知られたくない重要な情報は暗号化する。

② 認証:自分が誰かを証明する。

③ 公開鍵暗号系④ PKI:Public Key Infrustructure

4

Literacy 2 Mathematica

非対称暗号鍵方式(公開鍵暗号方式)

3

5

Literacy 2 Mathematica

実際の暗号通信

6

Literacy 2 Mathematica

公開かぎと電子署名

4

7

Literacy 2 Mathematica

暗号化の方式非対称鍵

① RSA最も有名で最も広く使用されているアルゴリズム。アルゴリズムの名前は、3人の開発者のRonald Rivest氏、Adi Shamir氏、LeonaldAdleman氏の頭文字を取って付けられた。米国RSAセキュリティ社が特許を持っていたが、2000年9月特許の期限切れを迎えた。暗号、電子署名、鍵配送の機能を持つ

② DSAアメリカの電子署名標準アルゴリズム。電子署名のみの機能を持ち、暗号化の機能は持っていない

③ Diffie-HellmanWhitfield Diffie氏とMartin E. Hellman氏によって開発されたアルゴリズムで、鍵配送の機能のみ持っている。ちなみに「公開鍵暗号」は Diffie氏によって考案され、1976年に Hellman氏との共同論文「New Directions in Cryptography」で発表された。RSA社の3人は、この論文を読んで感嘆し、RSAの開発を始めた

8

Literacy 2 Mathematica

Mathematicaの整数論関数

① Mod② GCD③ FactorInteger④ Divisors⑤ Prime⑥ PrimeQ⑦ ExtendGCD⑧ EulerPhi

5

9

Literacy 2 Mathematica

CharacterCode

① ToCharacterCode["string"] gives a list ofthe integer codes corresponding to thecharacters in a string.

In[1]:= ToCharacterCode["Everything is anexpression."]

Out[1]={69,118,101,114,121,116,104,105,110,103,32,105,115,32,97,110,32,101,120,112,114,101,115,115,105,111,110,46}

In[2]:= FromCharacterCode[%]Out[2]= Everything is an expression.

10

Literacy 2 Mathematica

Mod

① Mod[m, n]gives theremainder ondivision of mby n.

6

11

Literacy 2 Mathematica

GCD

① GCDgives thegreatestcommondivisor oftheintegers

12

Literacy 2 Mathematica

FactorInteger[n]

① FactorInteger[n] gives a listof the primefactors of theinteger n,togetherwith theirexponents.

7

13

Literacy 2 Mathematica

Divisors[n]

① Divisors[n]gives a list ofthe integersthat divide n.

14

Literacy 2 Mathematica

Prime

① Prime[n]gives the n-th primenumber.

8

15

Literacy 2 Mathematica

PrimeQ[expr]

① PrimeQ[expr] yieldsTrue if expris a primenumber,and yieldsFalseotherwise.n

16

Literacy 2 Mathematica

ExtendedGCD

① The first element in the output fromExtendedGCD[n, m] is the greatest commondivisor of the integers n and m.

In[1]:={g,{r,s}}=ExtendedGCD[n=45,m=36]Out[1]= {9,{1,-1}}In[2]:= GCD[45,36]Out[2]= 9

② The second element is a pair of integers. Thelinear combination of n and m with these ascoefficients gives the gcd.

In[3]:= nr+msOut[3]= 9

9

17

Literacy 2 Mathematica

互いに素な場合

① These two numbers are relatively prime.In[4]:= {g, {r, s}} = ExtendedGCD[2^100 + 3, 3^50 + 8])Out[4]={1,{62013782892351778750374,-109502757290992473821761130785}}

② Therefore this linear combination gives1.

In[5]:= (2^100 + 3) r + (3^50 + 8) sOut[5]= 1

18

Literacy 2 Mathematica

EulerPhi[n]

① EulerPhi[n] gives the Euler totient function φ(n) .② EulerPhi[n] gives the number of positive integers less

than or equal to n which are relatively prime to n.

Up to 10 there are four numbers relatively prime to 10.① In[1]:= EulerPhi[10]② Out[1]= 4③ In[2]:= Select[Range[10],GCD[#1,10]==1&]④ Out[2]= {1,3,7,9}

10

19

Literacy 2 Mathematica

Eulerの定理

自然数nと整数aで、GCD(a,n)=1

のとき a^φ(n) = 1 mod n

20

Literacy 2 Mathematica

Eulerの定理の例

In[1]:=GCD[5, 17]Out[1]=1

In[2]:=EulerPhi[17]Out[2]=16

In[3]:=Mod[5^EulerPhi[17], 17]Out[3]=1In[5]:=Mod[10^EulerPhi[17], 17]Out[5]=1

11

21

Literacy 2 Mathematica

RSA暗号方式

① 2つの鍵(公開鍵・秘密鍵)をつかう。② 暗号文の解読が困難である。③ 公開鍵から秘密鍵を推測することが困難である。

④ キーポイント:大きな数の因数分解が困難である。

22

Literacy 2 Mathematica

非対称暗号鍵方式(公開鍵暗号方式)

12

23

Literacy 2 Mathematica

公開鍵と秘密鍵の作り方(1)

e:一つ目の鍵① 二つ大きな素数を(p、q)を選ぶ。

p=5, q=17In[2]:=PrimeQ[5]Out[2]=TrueIn[3]:=PrimeQ[17]Out[3]=True

② pとqの積nを計算する。 n = p q = 85

③ φ(n)=(p-1)(q-1)を計算する (p-1)(q-1) = 64

④ (p-1)(q-1)と互いに素な整数eをみつける e = 3

24

Literacy 2 Mathematica

公開鍵と秘密鍵の作り方(2)

d:二つ目の鍵① e d = 1 mod (p-1)(q-1)となるように

dを選ぶ。 d=43と選ぶと e d = 3*43 = 129 = 64 *2 +1 = 1 mod 64

このようなdを選ぶアルゴリズムは後述

13

25

Literacy 2 Mathematica

dの選び方

① e d = 1 mod (p-1)(q-1)となるdは以下のように選ぶことができる。

e d = 1- k (p-1)(q-1)e d +k (p-1)(q-1) = 1

① これを満たすdはExtendedGCDによって求めることができる。

② {1,{d,k}}=ExtendedGCD[e, (p-1)(q-1)]③ dには、(p-1)(q-1)を法とする不定性があるので、適当に、 (p-1)(q-1)のα倍を足して調整して、dを正に選ぶ。

26

Literacy 2 Mathematica

dの選び方の例

e = 3, (p-1)(q-1)=64のときIn[7]:={g, {d, k}} = ExtendedGCD[3, 64]

Out[7]={1,{-21,1}}In[8]:=d + 64Out[8]=43

よって、dは43とすればよい

14

27

Literacy 2 Mathematica

暗号の通信の準備

① Bobが公開暗号鍵として(e,n)を公開する。

② AliceはBobの公開暗号鍵を入手する。

28

Literacy 2 Mathematica

Aliceの暗号文の作り方

① Mを暗号にしたい文章とする。(但しM<n)② Bobの公開鍵(e,n)を使い、暗号文Cを作る。

C = M^e mod n

例 (e,n) = (3, 85), M=77

In[1]:=Mod[77^3, 85]Out[1]=83

したがって、暗号文Cは83となる。

15

29

Literacy 2 Mathematica

Bobの暗号文の解読の仕方① Aliceから暗号文Cを受け取る② 自分だけが知っている秘密鍵dを使って、暗号文を解読す

る。C^d = M^(ed) mod n

= M^(1+k (p-1)(q-1)) mod pq= M*{M^k}^(p-1)(q-1) mod pq

Mがpまたはqの倍数でない場合はEulerの定理を使い

= M mod pq Mがpの倍数のときには、適当な自然数jが存在して、M=jpとな

り、

= j {j^k(p-1)}^q mod qここで、Eulerの定理を使うと

= j mod q = M mod pqMがqの倍数のときにも同様。

いずれの場合も C^d=M mod n となることがわかる。

30

Literacy 2 Mathematica

暗号文解読の例

例 (d,n) = (43, 85), C=83

In[1]:=Mod[83^43, 85]Out[1]=77

したがって、暗号化されたメッセージMは77であることがわかる。

16

31

Literacy 2 Mathematica

クラックの仕方

① Bobの公開鍵(e,n)を手に入れる。② nを因数分解して、p,qを推測する。③ p,qとeを使ってdを推測する。④ Aliceが作った暗号文を(d,n)を使って解読する。

32

Literacy 2 Mathematica

クラックの例

① (e,n)=(3,85) :公開鍵 C=83:暗号文とする。② nの因数分解

In[4]:=Divisors[85]Out[4]={1,5,17,85}

85は5と17に因数分解できる。③ ExtendedGCDを使ってdを推定する

In[9]:={g, {d, k}} = ExtendedGCD[3, (5 - 1)(17 - 1)]Out[9]={1,{-21,1}}In[8]:=d + 64Out[8]=43

④ dを使って、Cを解読する。In[1]:=Mod[83^43, 85]Out[1]=77

↓ d

17

33

Literacy 2 Mathematica

Exercise 1.

Check Euler’s Theorem by Mathematica.① Make a list of the function

f(m)=Mod[a^(m),n] from m=1 to theEuler number of n, where a is anarbitrary number, n is a prime number.

② Check when does f(m)=1 hold fordifferent values of a and n?

34

Literacy 2 Mathematica

Exercise 2. Encrypt your message

① Set a particular phrase as your message.② Convert your message into a sequence of

integers M.③ Choose a pair of 2-digit prime numbers (p,q)

and derive the keys (d,e,n).④ Encrypt M into C by the key (e,n).⑤ Check if C can be decrypted by the key (d,n).⑥ Option: Ask your neighbors to decrypt C by

disclosing your public key (e,n).

18

35

Literacy 2 Mathematica

Exercise 3. Crack mymessage

Crack me!Public key: (e,n) = (937, 46127)

Encrypted message: {14942, 16840, 39519, 6259, 6259, 12812,33935, 2736, 6259, 30900, 39211, 32709, 4943, 39211, 34710, 4943,45747, 6259, 19266, 19461, 25615, 27469, 6259, 6259, 14942, 1494,39519, 6259, 7357, 12408, 39211, 6259, 40186, 2736, 10334, 6259,45747, 4943, 44980, 15131, 40186, 19991, 34710, 6259, 34710,33935, 30900, 37515, 6259, 42569, 4943, 37515, 37515, 12408,41505, 4943, 6259, 22680, 30900, 34710, 33935, 2736, 10334,34710, 6259, 10334, 37515, 30900, 39211, 41505, 6259, 34710,33935, 4943, 6259, 24879, 4943, 40186, 37515, 27469}

Guess my original message and my secret key.

Option: Answer my questions in the original message.

36

Literacy 2 Mathematica

Exercise 4 (Option).Performance Evaluation of

Cracking① Use Timing[ ] function and

estimate the CPU time required tocrack RSA with given n.

② Draw the graph (log-log plot?) ofCPU time with different n.

③ Discuss the possibility of crackingwhen n is large.

19

37

Literacy 2 Mathematica

Timing

① Timing[expr] evaluates expr, andreturns a list of time used, together withthe result obtained.

② Timing gives the CPU time in seconds,multiplied by the symbol Second.

Example:Here is the CPU time needed tocompute a large factorial number.

In[11]:=Timing[10^10!]Out[11]={5.8 Second,Null}