技术标签: python rsa库
我现在知道RSA公钥/私钥只能一次加密非常短的输入,但任何人都可以提供一种方法来加密任何类型的文件(.txt,.phf,.exe等)只有公钥/私钥?我不想要额外的AES密钥.
这是我的代码,我的加密和解密后,我没有得到原始内容与公共&私钥.我不关心加密或解密的安全性,我只想让简单的加密解密工作在它可能需要的任何输入上,无论它有多长或多大.
from Crypto.PublicKey import RSA
from Crypto import Random
random_generator = Random.new().read
key = RSA.generate(1024, random_generator)
public_key = key.publickey()
f = open('C:\Users\Administrator\Desktop\jack.txt','r').read()
print 'original content: '+ f
enc_data = public_key.encrypt(f, 32)
print 'encrypted data: '
print enc_data
dec_data = key.decrypt(enc_data)
print 'decrypted data: '+ dec_data
这是输出:
original content: Python Cryptography Toolkit
A collection of cryptographic modules implementing various algorithms and protocols.
Subpackages:
Crypto.Cipher
Secret-key (AES, DES, ARC4) and public-key encryption (RSA PKCS#1) algorithms
Crypto.Hash
Hashing algorithms (MD5, SHA, HMAC)
Crypto.Protocol
Cryptographic protocols (Chaffing, all-or-nothing transform, key derivation functions). This package does not contain any network protocols.
Crypto.PublicKey
Public-key encryption and signature algorithms (RSA, DSA)
Crypto.Signature
Public-key signature algorithms (RSA PKCS#1)
Crypto.Util
Various useful modules and functions (long-to-string conversion, random number generation, number theoretic functions)
encrypted data:
('\x08\xe3\x9d\x03\x1e\xe9(\xe2\xc7\xc6e\x0b5\x02\xc0\xd8G\x1f\xf5\xb8\x9cMC\x93Z\x982\xa5\x97\xec\xab4\x18\xc2\xc8\xd9\xd3\x99aX\xd96b\x19\x96\xdc\x1d|F\xe0\xa9\xa9\xea\x03\x10>0g\x83\xdb\xeb\xdb\x13\x91\xc6\xd8\xf6\x95\[email protected]\x0bc\xae\xbe\xbe\xf0\xde\xcc\xcexk\x10\xb3\x86\xd3\xdd\xd0\[email protected]\x9a\x8a6ut\xb1\xaf\x07\x1f\xa2M\r\xf0D\xa2`h\xc3\x89\x18\x0e\xd4\xca\xee\xf5\xfc\x01\xed\x95}X\x1f\x13 1',)
decrypted data: ���J�rPX �����ju�a,�xm�'�]��ٟ�?y;�)��tĹ�,�D4^�ba�8����9q
+�i��l �q]Kd�Y���u��S�B���Ϲ�^�A3
.7��j��m�
�6�dl� qU
解决方法:
RSA只能加密有限的输入.多少取决于RSA的密钥大小(在您的情况下为1024位)和使用的填充.比这更大的东西(没有使用填充时为128字节,如果使用填充则更少)并且您无法再恢复它.
解决方案是使用混合加密.
>生成一个随机字节串16,24或32字节用作AES密钥,
>使用以前生成的密钥和AES使用AES加密实际数据
>使用RSA加密AES密钥.
AES加密:
from Crypto.Cipher import AES
from Crypto import Random
aeskey = Random.new().read(32)
iv = Random.new().read(AES.block_size)
cipher = AES.new(aeskey, AES.MODE_CFB, iv)
msg = iv + cipher.encrypt(b'Attack at dawn')
RSA加密(使用像OAEP这样的正确填充,因为教科书RSA被严重破坏):
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
message = aeskey
random_generator = Random.new().read
rsakey = RSA.generate(1024, random_generator)
cipher = PKCS1_OAEP.new(rsakey.publickey())
ciphertext = cipher.encrypt(message)
并且只发送msg和密文.解密类似,但后退,因为您首先必须从RSA密文恢复AES密钥.在使用AES解密时,不要忘记切掉IV.
标签:pycrypto,python,encryption,cryptography,rsa
来源: https://codeday.me/bug/20190722/1505364.html
本文记录如何在 UIStoryboard 或者 xib 中进行百分比布局,包括View.Width 占其 Superview.Width 的百分比View.Leading 与 Superview.Leading 间距占 Superview.Width 的百分比View.Trailing 与 Superview.Trailing 间距占 Superview.Width 的百分比View.Leadin...
最近在做一个maven项目,spring+mybatis,配置到tomcat上,另外还有gihub版本管理。出现了一些问题。将登录方法的method从Get改为Post,再次运行,用postman测试,显示Post方法不支持,Get方法仍能运行。写好mapper里的sql语句,运行报错,Invalid bound statement (not found): 后跟mapper接口的方法名,大意就是通过接口找不到mapper.xml里面的sql语句。这两个问题都可以归结为idea更改代码后重启项目更改.
数据竞赛过程中若仅采用一种方法是不能得到很好的效果的,通常要进行模型融合。模型融合通常包括3种方式:简单加权融合:(1)回归:算数平均融合、几何平均融合;(2)分类:投票;(3)综合:排序融合、log融合。stacking/blending:构建多层模型,并利用预测结果再拟合预测。boosting/bagging:多树的提升方法。stacking理论stacking是将用初始训练数据...
AlertDialog大小的改变//1)更改AlertDialog窗口大小的方法: AlertDialog dialog = new AlertDialog.Builder(this).create(); dialog.show(); WindowManager.LayoutParams params = dialog.getWindow().getAttributes()
一·新建项目注意下图处一定要《none》二·导入jar包(1) c3p0是支撑JDBC的;(2)jfina是支撑jfinal的,jfinal默认使用freemarker作为MVC的View,所以也要导入;(3)sqlite是支撑Mysql连接的(4)jetty-all等等三·编写代码新建项目Smarthome新建项目SmartHomeForPC...
5大主流浏览器 内核IE tridentChrome webkit blink 谷歌safari webkit 苹果firefox gecko 火狐opera presto webkit blink 欧朋浏览器的历史 和 js的诞生1、1990 html1.0Tim Berners-Lee 蒂姆·博纳斯·李 超文本分享资讯的人world wide web 移植到c libww...
jsoup Java HTML解析器:使用选择器语法来查找元素使用选择器语法来查找元素问题你想使用类似于CSS或jQuery的语法来查找和操作元素。方法可以使用Element.select(String selector) 和 Elements.select(String selector) 方法实现:File input = new File("/tmp/input.ht...
Abstract:Hardcoded 加密密钥可能会削弱系统安全性,一旦出现安全问题将无法轻易修正。Explanation:请勿对加密密钥进行硬编码,因为这样所有项目开发人员都能查看该加密密钥,而且还会大大增加解决问题的难度。一旦代码被使用,除非对软件进行修补,否则加密密钥将再也不能更改。如果受加密密钥保护的帐户遭受入侵,系统所有者将被迫在安全性和可用性之间做出选择。例 1:下列代码使用了硬编码加密密钥:private static final String encryptionKey = "l.
单身狗(25)时间限制300 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue“单身狗”是中文对于单身人士的一种爱称。本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱。输入格式:输入第一行给出一个正整数N(<=50000),是已知夫妻/伴侣的对数;随后N行,每行给出一对夫妻/伴侣——为方便起见,每人对...
xlrd导入模块import xlrd2、打开指定的excel文件,返回一个data对象data = xlrd.open_workbook(file) #打开excel表,返回data对象3、通过data对象可以得到各个sheet对象(一个excel文件可以有多个sheet,每个sheet就是一张表格)Sheet1 = data.sheet_by_index...
单个数据var li = List( "{\"name\":\"张三\",\"age\":30,\"addr\":\"北京\"}", "{\"name\":\"李四\",\"age\":30,\"addr\":\"北京\"}", "{\"name\":\"王五\", age:30,\"addr\":\"北京\"}", "{\"name\":\"赵六\",\"age\":30,\"addr\":\"北京\"", "{\"name\":\"田七\
单机多卡pytorch训练模型