加密解密之RSA
2016-07-01 16:58
627 查看
1. RSA加密解密key可以由generate生成,或者由construct构造,或者由importkey导入;
2. RSA可以加密的单段数据长度受key的长度所限制,大量数据需分段加密;
3. 目前推荐使用PKCS1_OAEP加密,PKCS1_V1_5可用于兼容老代码,但已不推荐使用;
4. 根据RFC 3447描述,若使用PKCS1_OAEP加密,单段数据最大长度为下图红框标识。
例如,若使用RSA 2048,则k = 2048 / 8 = 256,hLen为使用的hash算法所输出的字节数,若未指定,则默认为SHA1,占用20个字节。因此,最终所能够加密的明文的最大长度mLen <= 256 - 2*20 - 2 = 214.
若采用RSA 1024,则该长度为 128 - 42 = 86.
若采用PKCS1_V1_5加密,则能够加密的明文最大长度为 k-11。
参考:
https://www.dlitz.net/software/pycrypto/api/current/Crypto.Cipher.PKCS1_OAEP-module.html http://www.rfc-editor.org/pdfrfc/rfc3447.txt.pdf http://www.emc.com/emc-plus/rsa-labs/standards-initiatives/pkcs-rsa-cryptography-standard.htm
2. RSA可以加密的单段数据长度受key的长度所限制,大量数据需分段加密;
3. 目前推荐使用PKCS1_OAEP加密,PKCS1_V1_5可用于兼容老代码,但已不推荐使用;
4. 根据RFC 3447描述,若使用PKCS1_OAEP加密,单段数据最大长度为下图红框标识。
例如,若使用RSA 2048,则k = 2048 / 8 = 256,hLen为使用的hash算法所输出的字节数,若未指定,则默认为SHA1,占用20个字节。因此,最终所能够加密的明文的最大长度mLen <= 256 - 2*20 - 2 = 214.
若采用RSA 1024,则该长度为 128 - 42 = 86.
若采用PKCS1_V1_5加密,则能够加密的明文最大长度为 k-11。
from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP key = RSA.generate(2048) print 'key: ', key publickey = key.publickey() print 'publickey: ', publickey msg = 'encrypt this message' print 'orginal data: ', msg cipher = PKCS1_OAEP.new(key) encrypted = cipher.encrypt(msg) print 'encrypted data: ', encrypted decrypted = cipher.decrypt(encrypted) print 'decrypted data: ', decrypted
参考:
https://www.dlitz.net/software/pycrypto/api/current/Crypto.Cipher.PKCS1_OAEP-module.html http://www.rfc-editor.org/pdfrfc/rfc3447.txt.pdf http://www.emc.com/emc-plus/rsa-labs/standards-initiatives/pkcs-rsa-cryptography-standard.htm
相关文章推荐
- linux grep命令
- MyBatis(IncompleteElementException: Could not find result map com.hao.mapper.PersonTestMapper.=Base)
- 《Thinkinginjava》第13章-字符串
- Python初学习
- Android Observer的demo
- 新手必备的常用代码片段整理(二)
- java 正则表达式 邮箱(着重尾部格式)
- 简易版九宫格相加数值相等
- 一个带动画,带可传入比例值的css3圆环
- 信号的抽取和插值
- Linux NFS服务器的安装与配置
- 使用GTK+获取屏幕分辨率并设置满屏显示效果
- js如何判断一个对象是array ,instanceof 是基于什么实现的呢?
- Google 的开源技术protobuf 简介与例子
- oracle DML错误日志查看
- 《Thinkinginjava》第12章-异常处理
- Android 自定义View
- ListView显示最后一条Item不完整问题分析
- Altium Designer高级功能初探之:差分布线
- I/O控制方式