Last active
August 15, 2021 16:43
-
-
Save mesiriak/1f747a910217a0e537e3746123ff0652 to your computer and use it in GitHub Desktop.
so, i tried to do it. Hope in future it'll be easier to do cryptographic alghorithms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def GCD(a,b): | |
g = [max(a,b),min(a,b)] | |
mass = [[max(a,b),min(a,b)]] | |
while g[0] != 0 and g[1] != 0: | |
g[0], g[1] = g[1], g[0] % g[1] | |
mass.append([g[0],g[1]]) | |
return mass, g[0] | |
def euclid_ext(a, b): | |
if b == 0: | |
return a, 1, 0 | |
else: | |
d, x, y = euclid_ext(b, a % b) | |
return d, y, x - y * (a // b) | |
def endcrypt(m,ed,N): | |
return m**ed % N | |
p,q = map(int,input("Please, enter prime numbers p and q: ").split()) | |
N, phiN = p * q, (p-1) * (q-1) | |
print(f"Your N = {N}, your phi(N) = {phiN}.") | |
print("Now you need to choose open key e which satisfies the condiction 1 < e < phi(N) and e should be coprime with phi(GCD = 1)") | |
e = int(input()) | |
if GCD(e,phiN)[1] != 1: | |
e = int(input("Open key e isn't satisfie the condiction, choose new: ")) | |
_,_,c = euclid_ext(phiN,e) | |
if c > 0: | |
d = c | |
else: | |
d = phiN + c | |
print(d) | |
message = int(input("Enter message u wanna encode(only numbers now):")) | |
em = endcrypt(message,e,N) | |
dem = endcrypt(em,d,N) | |
print("Encrypted message: " + str(em)) | |
print("Decrypted message: " + str(dem)) | |
a = input() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment