Add script rsa_authentifcation
This commit is contained in:
parent
3a7f58dad6
commit
b3c0a7b351
1 changed files with 55 additions and 0 deletions
55
algebre-non-lineaire/rsa_authentication.py
Executable file
55
algebre-non-lineaire/rsa_authentication.py
Executable file
|
@ -0,0 +1,55 @@
|
|||
#!/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:
|
||||
command = input("command: ")
|
||||
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()
|
Loading…
Reference in a new issue