我現在知道RSA公鑰/私鑰只能立即加密非常短的輸入,但任何人都可以提供加密任何類型文件(.txt,。 phf,.exe等)只有公鑰/私鑰?我不想擁有額外的AES密鑰。Python加密,RSA公鑰/私鑰,大文件
這裏是我的代碼,我沒有得到我的加密和解密與公鑰&私鑰後解密的原始內容。我不在乎我的加密或解密有多安全,我只是希望簡單的加密解密可以處理任何可能需要的輸入,無論它有多長或多長。
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\xc[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
是的,像這樣的短輸入也適用於我 – 2015-02-10 08:51:25
爲了使這個工作在python 3中,你需要使用'.encode('UTF-8')' – ScottMcC 2017-05-10 09:40:18
@ScottMcC編碼'src_data'感謝您的提醒。我已經更新了我的答案,以包含Python 2/3兼容代碼。我想這個答案並不真正回答OP的問題,但我想對於那些想在少量文本上使用RSA的人來說,它仍然是一個有用的演示。 – 2017-05-10 10:39:24