root/python/TLSLite/dh/dh.py

リビジョン 30, 1.1 kB (コミッタ: nakiki, コミット時期: 2 年 前)

--

  • svn:executable 属性の設定値: *
Line 
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # 2006/06/30 --nakiki
5
6 # System
7 import tlslite.utils.cryptomath as cryptomath
8
9 # DHを利用した秘密鍵の安全な共有の仕方
10 if __name__ == '__main__':
11   g = 5 # generator
12   # pは128bitの素数を作るように指示している。
13   p = cryptomath.getRandomPrime(128) # prime modulas
14   x = 0x640F7967334E9872 # xはAさんの秘密鍵(伝送路では流さない) 適当な番号
15   y = 0x40F6BD5315A291DC # yはBさんの秘密鍵 (伝送路ではながさない) 適当な番号
16   print 'g=%x' % g
17   print 'p=%x' % p
18   print 'x=%x' % x
19   print 'y=%x' % y
20
21   # Aさんはg^x mod pを計算する
22   X = cryptomath.powMod(g, x, p)
23
24   # Bさんはg^y mod pを計算する
25   Y = cryptomath.powMod(g, y, p)
26
27   # AさんはXをBさんに渡す
28   print 'g^x mod p =%x' % X
29
30   # BさんはYをAさんに渡す。
31   print 'g^y mod p =%x' % Y
32
33   secretA = cryptomath.powMod(X, y, p)
34   secretB = cryptomath.powMod(Y, x, p)
35
36   print 'secretA= (g^x mod p)^y =%x' % secretA
37   print 'secretB=(g^y mod p)^x =%x' % secretB
38   if secretA == secretB:
39     print 'secretA==secretB ok'
40   else:
41     print 'secretA!=secretB ng'
42
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。