Python RSA 加密
Python RSA encryption
我正在尝试编写一个每次都使用相同密钥的 RSA 加密软件工具。这是我目前所拥有的。
import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
key = <_RSAobj @0x24b6348 n<1024>,e,d,p,q,u,private>
publickey = key.publickey()
encrypted = publickey.encrypt('hi', 32)
print(encrypted)
我在第 5 行遇到指向 < 符号的语法错误。我知道这是一个有效的私钥。问题是什么,我该如何解决。我也在使用 python 2.7.3
[编辑] 我从这段代码中得到了密钥
import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
import os
random_generator = Random.new().read
key = RSA.generate(1024, random_generator)
print(key)
raw_input()
此外,我在 'raw_input()'
之后从此代码中得到了 'RSA key format not supported error'
import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
text_file = open("keyfile.txt", "w")
text_file.write('<_RSAobj @0x24b6348 n<1024>,e,d,p,q,u,private>')
text_file.close()
raw_input()
with open('keyfile.txt', 'r') as f:
externKey = f.readline()
key = RSA.importKey(externKey, passphrase=None)
publickey = key.publickey()
encrypted = publickey.encrypt('hi', 32)
print(encrypted)
首先,<_RSAobj @0x24b6348 n<1024>,e,d,p,q,u,private>
不是一个有效的密钥,不知道你是怎么得到这个的,但它是你的密钥的字符串表示,仅作为 Python
对象,实际的密钥内容并未呈现,另请注意,您无法使用此字符串表示形式重建密钥对象.
在使用密钥进行 RSA 加密之前,您应该从某个地方导入密钥,例如 File、Generating In Memory 等。
所以你应该做的是:
key = RSA.importKey(externKey, passphrase=None)
其中 externKey
是一个字符串,因此您可以通过这种方式从密钥 File 加载密钥字符串。
或:
key = RSA.generate(bits, randfunc=None, progress_func=None, e=65537)
其中 bits
是您密钥的强度,例如 2048。
无论哪种方式,您都会返回一个 RSA 密钥对象 (_RSAobj
),然后您可以像其余代码一样进行加密。
[编辑] 完整代码
import Crypto
from Crypto.PublicKey import RSA
#Quick way to generate a new key
private_key = RSA.generate(1024)
#Show the real content of the private part to console, be careful with this!
print(private_key.exportKey())
#Get the public part
public_key = private_key.publickey()
#Show the real content of the public part to console
print(public_key.exportKey())
#Save both keys into some file for future usage if needed
with open("rsa.pub", "w") as pub_file:
pub_file.write(public_key.exportKey())
with open("rsa.pvt", "w") as pvt_file:
pvt_file.write(private_key.exportKey())
#Load public key back from file and we only need public key for encryption
with open('rsa.pub', 'r') as pub_file:
pub_key = RSA.importKey(pub_file.read())
#Encrypt something with public key and print to console
encrypted = pub_key.encrypt('hello world', None) # the second param None here is useless
print(encrypted)
#Load private key back from file and we must need private key for decryption
with open('rsa.pvt', 'r') as pvt_file:
pvt_key = RSA.importKey(pvt_file.read())
#Decrypt the text back with private key and print to console
text = pvt_key.decrypt(encrypted)
print(text)
我正在尝试编写一个每次都使用相同密钥的 RSA 加密软件工具。这是我目前所拥有的。
import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
key = <_RSAobj @0x24b6348 n<1024>,e,d,p,q,u,private>
publickey = key.publickey()
encrypted = publickey.encrypt('hi', 32)
print(encrypted)
我在第 5 行遇到指向 < 符号的语法错误。我知道这是一个有效的私钥。问题是什么,我该如何解决。我也在使用 python 2.7.3
[编辑] 我从这段代码中得到了密钥
import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
import os
random_generator = Random.new().read
key = RSA.generate(1024, random_generator)
print(key)
raw_input()
此外,我在 'raw_input()'
之后从此代码中得到了 'RSA key format not supported error'import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
text_file = open("keyfile.txt", "w")
text_file.write('<_RSAobj @0x24b6348 n<1024>,e,d,p,q,u,private>')
text_file.close()
raw_input()
with open('keyfile.txt', 'r') as f:
externKey = f.readline()
key = RSA.importKey(externKey, passphrase=None)
publickey = key.publickey()
encrypted = publickey.encrypt('hi', 32)
print(encrypted)
首先,<_RSAobj @0x24b6348 n<1024>,e,d,p,q,u,private>
不是一个有效的密钥,不知道你是怎么得到这个的,但它是你的密钥的字符串表示,仅作为 Python
对象,实际的密钥内容并未呈现,另请注意,您无法使用此字符串表示形式重建密钥对象.
在使用密钥进行 RSA 加密之前,您应该从某个地方导入密钥,例如 File、Generating In Memory 等。
所以你应该做的是:
key = RSA.importKey(externKey, passphrase=None)
其中 externKey
是一个字符串,因此您可以通过这种方式从密钥 File 加载密钥字符串。
或:
key = RSA.generate(bits, randfunc=None, progress_func=None, e=65537)
其中 bits
是您密钥的强度,例如 2048。
无论哪种方式,您都会返回一个 RSA 密钥对象 (_RSAobj
),然后您可以像其余代码一样进行加密。
[编辑] 完整代码
import Crypto
from Crypto.PublicKey import RSA
#Quick way to generate a new key
private_key = RSA.generate(1024)
#Show the real content of the private part to console, be careful with this!
print(private_key.exportKey())
#Get the public part
public_key = private_key.publickey()
#Show the real content of the public part to console
print(public_key.exportKey())
#Save both keys into some file for future usage if needed
with open("rsa.pub", "w") as pub_file:
pub_file.write(public_key.exportKey())
with open("rsa.pvt", "w") as pvt_file:
pvt_file.write(private_key.exportKey())
#Load public key back from file and we only need public key for encryption
with open('rsa.pub', 'r') as pub_file:
pub_key = RSA.importKey(pub_file.read())
#Encrypt something with public key and print to console
encrypted = pub_key.encrypt('hello world', None) # the second param None here is useless
print(encrypted)
#Load private key back from file and we must need private key for decryption
with open('rsa.pvt', 'r') as pvt_file:
pvt_key = RSA.importKey(pvt_file.read())
#Decrypt the text back with private key and print to console
text = pvt_key.decrypt(encrypted)
print(text)