Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import random
- def millerTest(a, i, n):
- if i == 0:
- return 1
- x = millerTest(a, i / 2, n)
- if x == 0:
- return 0
- y = (x * x) % n
- if ((y == 1) and (x != 1) and (x != (n - 1))):
- return 0
- if (i % 2) != 0:
- y = (a * y) % n
- return y
- #-----------------------------------------------
- def Check(n):
- if millerTest(random.randint(2, n - 2), n - 1, n) == 1:
- return True
- else:
- return False
- def euclidean(a,b):
- return a if b == 0 else euclidean(b, a % b)
- def searchexponent(fn):
- f = range(fn)
- f=f[2:]
- for i in f:
- if euclidean(fn,i) == 1:
- e = i
- break
- return e #достает минимальное
- def extendedeuclidean(a, b):
- u1 = 1
- u2 = 0
- u3 = a
- v1 = 0
- v2 = 1
- v3 = b
- while v3 != 0:
- q = u3 / v3
- t1 = u1 - q * v1
- t2 = u2 - q * v2
- t3 = u3 - q * v3
- u1 = v1
- u2 = v2
- u3 = v3
- v1 = t1
- v2 = t2
- v3 = t3
- return u1, u2, u3
- def PrimeNumber():
- t = False
- random.random()
- while t == False:
- p = random.randrange(500)
- t = Check(p)
- t = False
- random.random()
- while t == False:
- q = random.randrange(500)+10
- t = Check(q)
- return p,q
- def code(text,e,n):
- text = int(text)
- if text > n:
- print 'Ваше текст не подходит'
- else:
- codetext = pow(text, e) % n
- print 'шифрованый текст',codetext
- return codetext
- def decode(text,d,n):
- decodetext = pow(text, d) % n
- print 'дешифрованый текст',decodetext
- p,q = PrimeNumber()
- #p,q = 17,31 #тут заменил
- print 'Простые числа:',p,q
- n = p*q
- print 'n = ',n
- fn = (p-1)*(q-1)
- print 'f(n)= ',fn
- e = searchexponent(fn)
- print 'Число е: ', e
- a,b,c = extendedeuclidean(fn,e)
- d = b % fn
- print 'число d: ', d
- text = '111'
- print 'Сообщение', text
- codetext = code(text,e,n)
- decode(codetext,d,n)
Add Comment
Please, Sign In to add comment