123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- import random
- from point import Point
- from crypt import CipherAES
- class Curve:
-
- def __init__(self, p, a, b, gx, gy, n, h):
-
- self.p = p
-
- self.a = a
- self.b = b
-
- self.g = Point(
- curve = self,
- x = gx,
- y = gy
- )
-
- self.n = n
-
- self.h = h
- def point(self, x, y):
- return Point(
- curve = self,
- x = x,
- y = y
- )
-
- def inverseMod(self, k, point):
- if k == 0:
- raise ZeroDivisionError('Деление на 0 невозможно!!!')
- if k < 0:
-
- return point - self.inverseMod(-k, point)
-
- s, old_s = 0, 1
- t, old_t = 1, 0
- r, old_r = point, k
- while r != 0:
- quotient = old_r // r
- old_r, r = r, old_r - quotient * r
- old_s, s = s, old_s - quotient * s
- old_t, t = t, old_t - quotient * t
- gcd, x, y = old_r, old_s, old_t
- return x % point
- def randomKeypair(self):
- keyPriv = random.randrange(1, self.n)
- keyPub = self.g * keyPriv
- return keyPriv, keyPub
-
- def keyPub(self, keyPriv):
-
- return self.g * keyPriv
-
- def pointSecret(self, myKeyPrivate, friendKeyPublic):
- return friendKeyPublic * myKeyPrivate
-
- def encodeMessage(self, secretPoint, text):
- cipher = CipherAES(self, secretPoint)
- return cipher.encrypt(text)
-
- def decodeMessage(self, secretPoint, encodedText):
- cipher = CipherAES(self, secretPoint)
- return cipher.decrypt(encodedText)
-
- if __name__ == '__main__':
-
- cur = Curve(
-
- p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f,
-
- a = 0,
- b = 7,
-
- gx = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,
- gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8,
-
- n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141,
-
- h = 1
- )
-
-
-
-
- p1KeyPriv, p1KeyPub = cur.randomKeypair()
-
- p2KeyPriv, p2KeyPub = cur.randomKeypair()
-
-
-
-
-
-
- p1KeySecret = cur.pointSecret(p1KeyPriv, p2KeyPub)
-
- p2KeySecret = cur.pointSecret(p2KeyPriv, p1KeyPub)
-
- print(p1KeySecret.isEqual(p2KeySecret))
-
- p1MessageEncrypted = cur.encodeMessage(p1KeySecret, 'Привет, как дела?')
-
- print("п1: ", p1MessageEncrypted)
-
- p1MessageDecrypted = cur.decodeMessage(p2KeySecret, p1MessageEncrypted)
-
- print("п1: ", p1MessageDecrypted)
-
- p2MessageEncrypted = cur.encodeMessage(p2KeySecret, 'Спасибо, у меня всё хорошо :)')
-
- print("п2: ", p2MessageEncrypted)
-
- p2MessageDecrypted = cur.decodeMessage(p1KeySecret, p2MessageEncrypted)
-
- print("п2: ", p2MessageDecrypted)
|