Skip to content

Instantly share code, notes, and snippets.

@r3gor
Created February 8, 2020 08:51
Show Gist options
  • Save r3gor/a6611349f4cc59f9570bd62a6a574ca1 to your computer and use it in GitHub Desktop.
Save r3gor/a6611349f4cc59f9570bd62a6a574ca1 to your computer and use it in GitHub Desktop.
Modular Exponentiation used in the RSA public key cryptographic system
"""
Modular Exponentiation (Rapid Exponentiation)
---------------------------------------------
Efficient algorithm used in the RSA public key cryptographic system
More info:
https://en.wikipedia.org/wiki/Modular_exponentiation
"""
from time import time
def modexp(b,e,m):
r=1
while(e):
if(e&1): r=(r*b)%m
b,e=(b**2)%m,e>>1
return r
def main():
print("calculate: (b**e) mod m")
b=int(input("b (base): "))
e=int(input("e (exponent): "))
m=int(input("m (module): "))
print("-"*5,"[result]","-"*5)
ti=time()
print(modexp(b,e,m))
tf=time()
print(f"calculation time: {tf-ti}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment