密码学 椭圆曲线加密 记录

1)画出椭圆曲线 E11(1,1) 离散点图形。

代码:

def Ellipse_dot(x_,y_):
    for i in range(0,11):
        x_.append((pow(i,3)+i+1) %11)
        y_.append((pow(i,2))%11)
#print (x_,y_)
    for i in range(0,11):
        for j in range(0,11):
            if x_[i]==y_[j]:
                x.append(i)
                y.append(j)
def setplot():
    plt.xlabel('X axis')
    plt.ylabel('Y axis')  # 设置坐标轴标签
    plt.xlim(-1, 11)
    plt.ylim(-1, 11)

    ax = plt.gca()
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')  # 设置 上、右 两条边框不显示

    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')  # 将下、左 两条边框分别设置为 x y 轴

    ax.spines['bottom'].set_position(('data', 0))  # 将两条坐标轴的交点进行绑定
    ax.spines['left'].set_position(('data', 0))

    ax.xaxis.set_major_locator(MultipleLocator(1))
    ax.yaxis.set_major_locator(MultipleLocator(1))

Ellipse_dot(x_,y_)
setplot()
plt.scatter(x,y,marker='o',color='black',s=40,label='E11(1,1)上散点图')
plt.show()

效果图
在这里插入图片描述

2)取P=(1,5),Q=(3,3),计算P+Q。图上标记。

代码:

def Dot_add(x,y):
    if (x!=y):
        k=(((y[1]-x[1])%11)*ModReverse((y[0]-x[0])%11,11))%11
    else:
        k=(((3*pow(x[0],2) + 1)%11)*ModReverse((2*x[1])%11,11))%11
    Rx = (pow(k, 2) - x[0] - y[0])%11
    Ry = (k * (x[0] - Rx) - x[1])%11
    z=[Rx,Ry]
    return z
P=[1,5]
Q=[3,3]
R=Dot_add(P,Q)
setplot()
plt.scatter(x,y,color='black',label='E11(1,1)上散点图')
plt.scatter(R[0],R[1],color='red',label='R')
plt.text(R[0]+0.15, R[1]-0.75, 'R', ha='center', va='bottom', fontsize=10.5)
plt.show()

效果图
P+Q=(8,2)
在这里插入图片描述

3)计算2P,3P,4P ,图上标记。

代码:

setplot()
plt.scatter(x,y,color='black',label='E11(1,1)上散点图')
p2=Dot_add(P,P)
plt.scatter(p2[0],p2[1],color='green')
plt.text(p2[0]+0.15, p2[1]-0.75, '2P', ha='center', va='bottom', fontsize=10.5)
p3=Dot_add(p2,P)
plt.scatter(p3[0],p3[1],color='green')
plt.text(p3[0]+0.15, p3[1]-0.75, '3P', ha='center', va='bottom', fontsize=10.5)
p4=Dot_add(p2,p2)
plt.scatter(p4[0],p4[1],color='green')
plt.text(p4[0]+0.15, p4[1]-0.75, '4P', ha='center', va='bottom', fontsize=10.5)
plt.show()

效果图:
2P=(3,3) 3P=(8,2) 4P=(6,5)
在这里插入图片描述

4)计算10P,图上标记。

代码:

p8=Dot_add(p4,p4)
p10=Dot_add(p2,p8)
setplot()
plt.scatter(x,y,color='black',label='E11(1,1)上散点图')
plt.scatter(p10[0],p10[1],color='blue')
plt.text(p10[0]+0.15, p10[1]-0.75, '10P', ha='center', va='bottom', fontsize=10.5)
plt.show()

示意图:
10P=(6,6)
在这里插入图片描述

5)ECDH协议中,假设生成元是P,Alice随机取a=6,发送给Bob什么?Bob随机取b=7,发送Alice什么?最后协商的密钥是什么?

代码:

A_to_B=Dot_add(p2,p4)
B_to_A=Dot_add(p4,p3)
K=Dot_add(A_to_B,B_to_A)
print('Alice send to Bob is (',A_to_B[0],',',A_to_B[1],')')
print('Bob send to Alice is (',B_to_A[0],',',B_to_A[1],')')
print('Final Key is (',K[0],',',K[1],')')

示意图:
在这里插入图片描述

6)ElGamal的ECC版本,,Alice的私钥是d=11,则公钥是多少。

7)Bob用Alice的公钥进行加密,如果明文Pm=(4,5),则加密后的明文是多少?

8)Alice收到密文后,如何进行解密?

代码:

import random
from numpy import *
O=[-1,-1]
def gcd(a, b, s):
    if a % b == 0:
        return b, s
    q = a // b
    temp = s[1]
    s[1] = s[0] - q * s[1]
    s[0] = temp
    return gcd(b, a % b, s)

def ModReverse(a,n):
    s = [1,0]
    gcd(a,n,s)
    if s[1]==0:
        return -1
    elif s[1]<0:
        return n+s[1]
    else:
        return s[1]

def Dot_add(x,y):
    if (x==[-1,-1]):
        return y
    if (y==[-1,-1]):
        return x
    if (x!=y):
        if (x[0] == y[0]):
            return [-1, -1]
        k=(((y[1]-x[1])%11)*ModReverse((y[0]-x[0])%11,11))%11
    else:
        k=(((3*pow(x[0],2) + 1)%11)*ModReverse((2*x[1])%11,11))%11
    Rx = (pow(k, 2) - x[0] - y[0])%11
    Ry = (k * (x[0] - Rx) - x[1])%11
    z=[Rx,Ry]
    return z

def Dot_multi(n,P):
    P_queue=[]
    n=(n+14)%14
    while (n!=0) :
        n_=n%2
        n=n//2
        if n_==1 :
            P_queue.append(P)
        P=Dot_add(P,P)
    Q=P_queue[0]
    for i in range(len(P_queue)-1):
        Q=Dot_add(Q,P_queue[i+1])
    return Q

def KEYGEN(G,d):
    P=Dot_multi(d,G)
    return P

def DEC(d,C):
    Cd=Dot_multi(d,C[0])
    C2=C[1]
    Pm=Dot_add(C2,Dot_multi(-1,Cd))
    return Pm

def ENC(P , G , n , Pm):
    r=random.randint(1,n-1)
    c1 = Dot_multi(r, G)
    Pr=Dot_multi(r, P)
    c2 =Dot_add(Pm,Pr)
    C = [c1, c2]
    return C

def qiujie(P):
    i=2
    Q=Dot_add(P,P);
    while (Q!=[-1,-1]):
        Q=Dot_add(Q,P)
        i=i+1
    return i

G=[1,5]
n=qiujie(G)
d=11
P=KEYGEN(G,d)
print('公钥为(P=',P,',','G=',G,',E= E11(1,1), n = ',n,')')

Pm=[4,5]
C=ENC(P,G,n,Pm)
print('明文为:',Pm)
print('密文为:'+'C1=',C[0],' C2=',C[1])

Pm_=DEC(d,C)
print('解密后密文为:',Pm_)

实验结果为:
在这里插入图片描述

  • 9
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值