Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- from pygost import gost34112012256
- from asn1 import Decoder, Encoder, Numbers
- from Cryptodome.Util.number import inverse
- # const vars
- A = -1
- B = 53520245325288251180656443226770638951803337703360722011463033447827147086694
- p = 57896044625414088412406986721186632159605151965036429316594800028484330862739
- r = 28948022312707044206203493360593316079803694388568974763893400879284219004579
- xP = 36066034950041118412594006918367965339490267219250288222432003968962962331642
- yP = 54906983586985298119491343295734802658016371303757622466870297979342757624191
- q = r
- d = 28843936935357833154095140201945387686710795615857734070222172448292711098429
- CONST_MENU = "Enter the command number:\n" \
- "1 - Make a signature\n" \
- "2 - Check file signature\n" \
- "3 - Exit\n"
- def write_asn1(s, r, Q):
- qX = lift(Q[0])
- qY = lift(Q[1])
- f = Encoder()
- f.start()
- f.enter(Numbers.Sequence)#заголовок
- f.enter(Numbers.Set)#множество ключей, 1 задействован
- f.enter(Numbers.Sequence)# первый «ключ»
- f.write(b'\x80\x06\x07\x00', Numbers.OctetString)
- f.write(b'GOST R 34.10-2018', Numbers.UTF8String)
- f.enter(Numbers.Sequence)# значение открытого ключа
- f.write(qX, Numbers.Integer)
- f.write(qY, Numbers.Integer)
- f.leave()
- f.enter(Numbers.Sequence)#параметры криптосистемы
- f.enter(Numbers.Sequence)#параметры поля
- f.write(p, Numbers.Integer)
- f.leave()
- f.enter(Numbers.Sequence)#параметры кривой
- f.write(A, Numbers.Integer)
- f.write(B, Numbers.Integer)
- f.leave()
- f.enter(Numbers.Sequence)#образующая группы точек кривой
- f.write(xP, Numbers.Integer)
- f.write(yP, Numbers.Integer)
- f.leave()
- f.write(q, Numbers.Integer)
- f.leave()#– параметры криптосистемы"
- f.enter(Numbers.Sequence)#подпись сообщения
- f.write(r, Numbers.Integer)
- f.write(s, Numbers.Integer)
- f.leave()
- f.leave()
- f.enter(Numbers.Sequence)
- f.leave()
- f.leave()
- f.leave()
- with open("signed.txt", "wb") as output:
- output.write(f.output())
- def make_signature(filename):
- global p, r, A, B, xP, yP, q, d
- E = EllipticCurve(GF(p), [A, B])
- P = E(xP, yP)
- text = read_file(filename)
- alph = gost34112012256.new(text).digest()
- h = int.from_bytes(alph, "big")
- e = h % q
- if e == 0:
- e = 1
- Q = d * P
- while 1:
- k = randint(0, q)
- C = k * P
- xC = C[0]
- xC = lift(xC)
- yC = C[1]
- r = xC % q
- if r == 0:
- continue
- s = (r * d + k * e) % q
- if s == 0:
- continue
- write_asn1(s, r, Q)
- print("Made a signature")
- break
- def check_signature(filename):
- data = read_file("signed.txt")
- f = Decoder()
- f.start(data)
- f.enter()#заголовок
- f.enter()#множество ключей, 1 задействован
- f.enter()# первый «ключ»
- f.read()
- f.read()
- f.enter()# значение открытого ключа
- qX = f.read()[1]
- qY = f.read()[1]
- f.leave()
- f.enter()#параметры криптосистемы
- f.enter()#параметры поля
- p = f.read()[1]
- f.leave()
- f.enter()#параметры кривой
- A = f.read()[1]
- B = f.read()[1]
- f.leave()
- f.enter()#образующая группы точек кривой
- xP = f.read()[1]
- yP = f.read()[1]
- f.leave()
- q = f.read()[1]
- f.leave()#– параметры криптосистемы"
- f.enter()#подпись сообщения
- r = f.read()[1]
- s = f.read()[1]
- f.leave()
- f.leave()
- f.enter()
- f.leave()
- f.leave()
- f.leave()
- if r <= 0 or r >= q or s <= 0 or s >= q:
- print("File was changed!")
- return False
- mess = read_file(filename)
- hah = gost34112012256.new(mess).digest()
- h = int.from_bytes(hah, "big")
- e = h % q
- if e == 0:
- e = 1
- v = inverse(e, q)
- z2 = (-r * v) % q
- z1 = (s * v) % q
- E = EllipticCurve(GF(p), [A,B])
- P = E(xP, yP)
- Q = E(qX, qY)
- C = z1 * P + z2 * Q
- xc = C[0]
- xc = lift(xc)
- R = xc % q
- if R == r:
- print("Message wasn't changed")
- else:
- print("Message was changed")
- def read_file(filename):
- data = b''
- with open(filename, "rb") as inp:
- for line in inp:
- data += line
- return data
- def get_filename():
- return input("Enter filename: ")
- def main():
- while 1:
- user_choice = int(input(CONST_MENU))
- if user_choice == 1:
- make_signature(get_filename())
- break
- elif user_choice == 2:
- check_signature(get_filename())
- break
- elif user_choice == 3:
- return
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement