RSA algorithm

RSA is the most important encryption algorithm,which is a Public-key algorithm. It was put forward by Ron Rivest, Adi Shamir and Len Adleman in 1977.
The content of ARS algorithm is demonstrate by the following picture:

这里写图片描述

The code of RSA is attached:

def range_prime(start, end):
    l = list()  
    for i in range(start, end+1):  
        flag = True  
        for j in range(2, i):  
            if i % j == 0:  
                flag = False  
                break  
        if flag:  
            l.append(i)  
    return l  

def generate_keys(p, q):  
    #numbers = (11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47)  
    numbers =range_prime(10, 100)  
    n = p * q
    C = (p-1) * (q-1)  
    e = 0  
    for x in numbers:
        if x < C and C % x > 0:
            e = x
            break  
    if e==0:  
        raise StandardError("e not found")  

    d = 0  
    for x in range(1, C):
        if(e * x) % C == 1:
            d = x
            break  
    if d==0:  
        raise StandardError("d not found")  
    return ((n, e), (n, d))

def encrypt(m, pkey):
    C, x = pkey
    return (m ** x) % C  

    decrypt = encrypt

if __name__ == '__main__':  
    pub, pri = generate_keys(47, 79)  
    L = range(20, 30)
    C = list(map(lambda x: encrypt(x, pub), L))
    D = list(map(lambda x: decrypt(x, pri), C))
    print ("keys:", pub, pri)  
    print ("message:", list(L))
    print ("encrypt:", list(C))
    print ("decrypt:", list(D))

The running result is:

keys: (3713, 11) (3713, 1631)
message: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
encrypt: [406, 3622, 3168, 134, 3532, 263, 1313, 2743, 2603, 1025]
decrypt: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值