python学习实例(7)

#=========================================================
#第8章 信息安全(Information Security)的python程序
#=========================================================

#====================
#8.3 措施和技术
#====================

#++++++++++++++++++++
#8.3.1 密码学
#++++++++++++++++++++

#非对称加密

#<程序:把n分解成p*q>
import math
n = 221
m = int(math.ceil(math.sqrt(n)))
flag = 0
for i in range(2,m+1,1):
    if n % i == 0:
        print(i,int(n/i))
        flag = 1
        break
if flag == 0:
    print ("Cannot find!")



#<程序:RSA加密解密实现>
# All the functions are written by Edwin Sha
def change_number (x, b): #这个函数把一个十进制数x转换成一串二进制数
    if x < b: L=[x]; return(L)
    a=x % b; x=x//b
    return([a]+change_number(x,b))   #the least one goes first!
def mod (a,x,b): #计算 a^x mod b
    L=change_number(x,2)
    #print("x in binary = ",L)
    r=a % b; final=1
    for i in L:
        if i ==1: final= (final*r) % b
        r = (r*r) % b
    return(final)
def GCD(x,y): #计算 x与y的最大公约数
    if x>y: a=x;b=y
    else: a=y;b=x
    if a%b ==0: return(b)
    return(GCD(a%b,b))
def Extended_Euclid(x,y,Vx,Vy): #return [a, b] s.t. ax + by = GCD(x,y)
    #by Edwin Sha
        r=x%y; z=x//y
        if r==0: return(y,Vy)
        Vx[0]=Vx[0]-z*Vy[0]
        Vx[1]=Vx[1]-z*Vy[1]        
        return(Extended_Euclid(y, r, Vy, Vx))
def Mod_inverse(e, n): # return x : e*x mod n = 1  by Edwin Sha
    Vx=[1,0]
    Vy=[0,1]
    if e>n:
        G,X=Extended_Euclid(e,n,Vx,Vy)
        d=X[0]%n        
    else:
        G,X=Extended_Euclid(n,e,Vx,Vy)
        d=X[1]%n
    return(d)


import random
def RSA_key_generation(p,q): #p and q are primes, compute keys e and d
    phi=(p-1)*(q-1)
    e=random.randint(3,phi)
    if e%2==0: e+=1
    while(GCD(e,phi) !=1):
        e=random.randint(3,phi)
        if e%2==0: e+=1
    d=Mod_inverse(e,phi)
    if e*d % phi !=1: print("ERROR: e and d are not generated correctly")
    return (e,d)

def RSA_test(p,q):
    e,d=RSA_key_generation(p,q)
    n=p*q
    print("e, d, n: ", e, d, n)
    M=int(input("Please enter M (<n): "));
    while M>=n: M=int(input("Please enter M (< n)"))
    C=mod(M,e,n)
    print("Before transmission, original M=",M," is encrypted to Cipher=",C)
    M1=mod(C,d,n)
    if M!=M1:print("!!! Error  !!!")
    print("After transmission, Cipher",C, "is decrypted back to:",M1,"\n\n")
p=19
q=97
RSA_test(p,q) 

 

  • 222
    点赞
  • 204
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 38
    评论
评论 38
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兔老大RabbitMQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值