def_recvall(self): BUFF_SIZE = 2048 data = b'' whileTrue: part = self.request.recv(BUFF_SIZE) data += part iflen(part) < BUFF_SIZE: break return data.strip()
self.send(b'Here are your public key:') self.send(str(pub).encode()) whileTrue: #sign self.send(b'Pz tell me what you want to sign?') message = self.recv() if message == b'0xGame': self.send(b"Permission denied!") quit() self.send(b'Here are your sign:') r,s = sign(message,pub,pri) self.send(f'r={r}\ns={s}'.encode()) #ver self.send(b'Tell me your signature,if you want to get the flag.') r = int(self.recv()) s = int(self.recv())
if verity(b'0xGame',(r,s),pub): self.send(b'Here you are:'+flag) self.send(b'bye~') quit() else: self.send(b"sorry~you can't get it.")
from Crypto.Util.number import getPrime,GCD,inverse,bytes_to_long,long_to_bytes import random
defgetKey(bits): p = getPrime(bits) g = getPrime(bits//2) d = random.randint(1,p-2) y = pow(g,d,p) public,private = (p,g,y),d return public,private
defsign(m,public,private): m = bytes_to_long(m) p,g,y = public d = private whileTrue: k = random.randint(1,p-1) if GCD(k,p-1)==1:break r = pow(g,k,p) s = ((m-d*r)*inverse(k,p-1)) % (p-1) return (r,s)
defverity(m,sign,public): m = bytes_to_long(m) p,g,y = public r,s = sign ifpow(g,m,p) == (pow(y,r,p)*pow(r,s,p)) % p: returnTrue else: returnFalse
其他无关紧要的部分就不管了,直接看看怎么伪造签名:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#sign self.send(b'Pz tell me what you want to sign?') message = self.recv() if message == b'0xGame': self.send(b"Permission denied!") quit() self.send(b'Here are your sign:') r,s = sign(message,pub,pri) self.send(f'r={r}\ns={s}'.encode()) #ver self.send(b'Tell me your signature,if you want to get the flag.') r = int(self.recv()) s = int(self.recv())
if verity(b'0xGame',(r,s),pub): self.send(b'Here you are:'+flag) self.send(b'bye~')