Private key $ x $, public key $ E, G, Y $
Let $ m $ be the message you want to send.
#random number
r = 3
#Private key
key = 3
#Base point
g = [2,2]
#Order
l = 5
# y^2 = x^3 + ax +b
a = 0
b = 1
def Mod(x,y):
if x < 0:
x = x+y
return x%y
def invMod(x,y):
count = 1
while True:
tmp = x*count
if tmp%y == 1:
return count
count += 1
def Ellipse(p,r):
for _ in range(r):
s = Mod(Mod((3*p[0]*p[0]+a),l)*invMod((2*p[1]),l),l)
x = Mod(s*s-p[0]-p[0],l)
y = Mod(s*(p[0]-x)-p[1],l)
return [x,y]
def encrypt(G,Y, m):
U =Ellipse(G,r)
V =Ellipse(Y,r)
#Take an exclusive OR
c = V[1] ^ m
return U,c
def decrypt(U, c, key):
V = Ellipse(U,key)
m = V[1] ^ c
return m
def main():
#Creating a public key
Y = g
Y = Ellipse(Y, key)
print("Public key:",[a,b], g,Y)
#Plaintext
message = 4
print("Plaintext:", message)
#encryption
U,c = encrypt(g, Y, message)
print("secret message:",U,c)
#Decryption
decrypt_message = decrypt(U, c, key)
print("Decryption message", decrypt_message)
if __name__ == "__main__":
main()
Recommended Posts