| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
import tlslite.utils.cryptomath as cryptomath |
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
if __name__ == '__main__': |
|---|
| 11 |
g = 5 |
|---|
| 12 |
|
|---|
| 13 |
p = cryptomath.getRandomPrime(128) |
|---|
| 14 |
x = 0x640F7967334E9872 |
|---|
| 15 |
y = 0x40F6BD5315A291DC |
|---|
| 16 |
print 'g=%x' % g |
|---|
| 17 |
print 'p=%x' % p |
|---|
| 18 |
print 'x=%x' % x |
|---|
| 19 |
print 'y=%x' % y |
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
X = cryptomath.powMod(g, x, p) |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
Y = cryptomath.powMod(g, y, p) |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
print 'g^x mod p =%x' % X |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 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 |
|
|---|