2022-03-14 14:34:30 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""Signature et vérification de signature avec RSA"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
"""Call functions dependending on args"""
|
|
|
|
|
|
|
|
if len(sys.argv) == 1:
|
2022-03-14 15:10:51 +01:00
|
|
|
command = input("command ('sign' or 'verify'): ")
|
2022-03-14 14:34:30 +01:00
|
|
|
else:
|
|
|
|
command = sys.argv[1]
|
|
|
|
if command == "sign":
|
|
|
|
ac = int(input("AC : "))
|
|
|
|
d = int(input("d : "))
|
|
|
|
nd = int(input("nd : "))
|
|
|
|
e = int(input("e : "))
|
|
|
|
ne = int(input("ne : "))
|
|
|
|
s = sign(ac, d, e, nd, ne)
|
|
|
|
print(s)
|
|
|
|
elif command == "verify":
|
|
|
|
s = int(input("S : "))
|
|
|
|
ac = int(input("AC : "))
|
|
|
|
d = int(input("d : "))
|
|
|
|
nd = int(input("nd : "))
|
|
|
|
e = int(input("e : "))
|
|
|
|
ne = int(input("ne : "))
|
|
|
|
if verify(s, ac, d, e, nd, ne):
|
|
|
|
print("YES")
|
|
|
|
else:
|
|
|
|
print("NO")
|
|
|
|
|
|
|
|
|
|
|
|
def sign(ac: int, d: int, e: int, nd: int, ne: int) -> int:
|
|
|
|
"""Produce a signature"""
|
|
|
|
|
|
|
|
if nd <= ne:
|
|
|
|
return pow(pow(ac, d, nd), e, ne)
|
|
|
|
return pow(pow(ac, e, ne), d, nd)
|
|
|
|
|
|
|
|
|
|
|
|
def verify(s: int, ac: int, d: int, e: int, nd: int, ne: int) -> bool:
|
|
|
|
"""Verify a signature"""
|
|
|
|
|
|
|
|
if nd >= ne:
|
|
|
|
maybe = pow(pow(s, d, nd), e, ne)
|
|
|
|
else:
|
|
|
|
maybe = pow(pow(s, e, ne), d, nd)
|
|
|
|
print(f"Found AC = {maybe}")
|
|
|
|
return maybe == ac
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|