Python模拟RSA算法
来源:互联网 发布:笔顺演示软件 下载 编辑:程序博客网 时间:2024/06/10 05:04
参考http://www.liaoxuefeng.com/article/00137389255913089a316bc2ccb48d3b2323759fecd4cf8000
维基百科给出的RSA算法简介如下:假设Alice想要通过一个不可靠的媒体接收Bob的一条私人讯息。她可以用以下的方式来产生一个公钥和一个私钥: 随意选择两个大的质数p和q,p不等于q,计算N=pq。 根据欧拉函数,不大于N且与N互质的整数个数为(p-1)(q-1) 选择一个整数e与(p-1)(q-1)互质,并且e小于(p-1)(q-1) 用以下这个公式计算d:d × e ≡ 1 (mod (p-1)(q-1)) 将p和q的记录销毁。(N,e)是公钥,(N,d)是私钥。(N,d)是秘密的。Alice将她的公钥(N,e)传给Bob,而将她的私钥(N,d)藏起来。
#!/usr/bin/env pythondef 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 ldef 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 n in numbers: if n < C and C % n > 0: e = n break if e==0: raise StandardError("e not found") d = 0 for n in range(2, C): if(e * n) % C == 1: d = n break if d==0: raise StandardError("d not found") return ((N, e), (N, d))def encrypt(m, key): C, x = key return (m ** x) % Cdecrypt = encryptif __name__ == '__main__': pub, pri = generate_keys(47, 79) L = range(20, 30) C = map(lambda x: encrypt(x, pub), L) D = map(lambda x: decrypt(x, pri), C) print "keys:", pub, pri print "message:", L print "encrypt:", C print "decrypt:", D
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: [20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L]
0 0
- Python模拟RSA算法
- RSA算法Python实现
- python实现rsa算法
- RSA算法的python实现
- rsa算法的python实现
- python模拟银行家算法
- python 模拟lru算法
- Python FIFO算法模拟
- RSA算法研究与PYTHON实现
- RSA加/解密算法--python实现
- PYTHON实现RSA算法之简洁代码
- 基于python的简单 RSA算法实现
- 密码学:基于python的rsa算法
- python模拟登陆新浪微博 并 关注好友 刷粉丝(rsa算法,2013年7月26日测试成功)
- RSA python
- [Python] RSA
- RSA算法
- RSA算法
- 如何设置'REUSE_ALV_GRID_DISPLAY'的单个单元格的颜色
- 17个新手常见的运行时错误
- 关于鼠标滚轴事件的检测问题
- 子集和
- eclipse 配置JDK
- Python模拟RSA算法
- 《编写可读代码的艺术》读后总结
- Ice_cream’s world II - HDU 2121 朱刘算法
- Android 实现记住用户名和密码的功能
- SAPlink V2.0
- 简单工厂模式---商场收费系统
- android 图片文字尺寸(dp和sp)
- 美团网App中显示项界面设计
- 【vtk】 裁剪