英文:
Python equivalent of password encryption from RSA key Java code
问题
以下是使用RSA私钥(例如:MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvBXTe278Lwg2MoI7iGKolSYuF+sNFKrsZplxCN9x0kItU3KIf8+1q60ILLwLewCEf7foxzpWp32j9YYU9vNBghuJ7BHcDYTffTRcv+QdNno491j701Hq7DIw13AGCQQTRcnfclvblnytIEWoQsiUvPJcdiWgqJIX3IQGA47a+uwIDAQAB
)进行加密,并使用上述RSA私钥生成的公钥加密纯文本字符串test123
的Java代码:
byte[] array = javax.xml.bind.DatatypeConverter.parseBase64Binary(key);
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey publicKey = kf.generatePublic(new X509EncodedKeySpec(array));
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] result = cipher.doFinal("test123".getBytes("UTF-8"));
String output = javax.xml.bind.DatatypeConverter.printBase64Binary(result);
我尝试使用Python中的m2crypto
库执行相同操作的代码:
import base64
from M2Crypto import BIO, RSA
pubkey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvBXTe278Lwg2MoI7iGKolSYuF+sNFKrsZplxCN9x0kItU3KIf8+1q60ILLwLewCEf7foxzpWp32j9YYU9vNBghuJ7BHcDYTffTRcv+QdNno491j701Hq7DIw13AGCQQTRcnfclvblnytIEWoQsiUvPJcdiWgqJIX3IQGA47a+uwIDAQAB'
# 加密
text = "test123".encode('utf-8') # 明文
pub_bio = BIO.MemoryBuffer(pubkey.encode('utf-8')) # 公钥字符串
pub_rsa = RSA.load_pub_key_bio(pub_bio) # 加载公钥
secret = pub_rsa.public_encrypt(text, RSA.pkcs1_padding) # 公钥加密
sign = base64.b64encode(secret) # 密文进行base64编码
print(sign)
我遇到的错误是:
Traceback (most recent call last):
File "encrypt_rsa_public_key.py", line 13, in <module>
pub_rsa = RSA.load_pub_key_bio(pub_bio) # 加载公钥
File "/Users/umeshpathak/env/py3env/lib/python3.7/site-packages/M2Crypto/RSA.py", line 444, in load_pub_key_bio
rsa_error()
File "/Users/umeshpathak/env/py3env/lib/python3.7/site-packages/M2Crypto/RSA.py", line 333, in rsa_error
raise RSAError(Err.get_error_message())
M2Crypto.RSA.RSAError: no start line
如何解决这个问题?
英文:
Below is the Java code that uses rsa private key (ex: MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvBXTe278Lwg2MoI7iGKolSYuF+sNFKrsZplxCN9x0kItU3KIf8+1q60ILLwLewCEf7foxzpWp32j9YYU9vNBghuJ7BHcDYTffTRcv+QdNno491j701Hq7DIw13AGCQQTRcnfclvblnytIEWoQsiUvPJcdiWgqJIX3IQGA47a+uwIDAQAB
)
and encrypts a plain string test123
using rsa public key (public key generated from rsa private key above)
byte[] array = javax.xml.bind.DatatypeConverter.parseBase64Binary(key); KeyFactorykf = KeyFactory.getInstance(“RSA”);
publicKey = kf.generatePublic(new X509EncodedKeySpec(array)); Cipher = Cipher.getInstance(“RSA”);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] result = cipher.doFinal (“test123”.getBytes(“UTF-8”));
String output = javax.xml.bind.DatatypeConverter.printBase64Binary(result);
I tried m2crypto library to do so in python:
import base64
from M2Crypto import BIO, RSA
pubkey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvBXTe278Lwg2MoI7iGKolSYuF+sNFKrsZplxCN9x0kItU3KIf8+1q60ILLwLewCEf7foxzpWp32j9YYU9vNBghuJ7BHcDYTffTRcv+QdNno491j701Hq7DIw13AGCQQTRcnfclvblnytIEWoQsiUvPJcdiWgqJIX3IQGA47a+uwIDAQAB'
# encryption
text = "test123".encode('utf-8') # Plaintext
pub_bio = BIO.MemoryBuffer(pubkey.encode('utf-8')) # Public key string
pub_rsa = RSA.load_pub_key_bio(pub_bio) # Load public key
secret = pub_rsa.public_encrypt(text, RSA.pkcs1_padding) # Public key encryption
sign = base64.b64encode(secret) # Ciphertext base64 encoding
print(sign)
The error I am getting is:
Traceback (most recent call last):
File "encrypt_rsa_public_key.py", line 13, in <module>
pub_rsa = RSA.load_pub_key_bio(pub_bio) # Load public key
File "/Users/umeshpathak/env/py3env/lib/python3.7/site-packages/M2Crypto/RSA.py", line 444, in load_pub_key_bio
rsa_error()
File "/Users/umeshpathak/env/py3env/lib/python3.7/site-packages/M2Crypto/RSA.py", line 333, in rsa_error
raise RSAError(Err.get_error_message())
M2Crypto.RSA.RSAError: no start line
How can I solve this?
答案1
得分: 1
你正在使用的库可能希望以PEM格式获取公钥,即位于 `-----BEGIN PUBLIC KEY-----` 和 `-----END PUBLIC KEY-----` 之间。因此,您需要将您的 `pubkey` 更改为:
pubkey = '-----BEGIN PUBLIC KEY-----\n'
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvBXTe278Lwg2MoI7iGKolSYuF+sNFKrsZplxCN9x0kItU3KIf8+1q60ILLwLewCEf7foxzpWp32j9YYU9vNBghuJ7BHcDYTffTRcv+QdNno491j701Hq7DIw13AGCQQTRcnfclvblnytIEWoQsiUvPJcdiWgqJIX3IQGA47a+uwIDAQAB'
'\n-----END PUBLIC KEY-----';
换行符很重要。
英文:
The library that you are using might expect to get the public key in PEM format i.e. enclosed between -----BEGIN PUBLIC KEY-----
and -----END PUBLIC KEY-----
. So you would have to change your pubkey
to :
pubkey = '-----BEGIN PUBLIC KEY-----\n' \
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvBXTe278Lwg2MoI7iGKolSYuF+sNFKrsZplxCN9x0kItU3KIf8+1q60ILLwLewCEf7foxzpWp32j9YYU9vNBghuJ7BHcDYTffTRcv+QdNno491j701Hq7DIw13AGCQQTRcnfclvblnytIEWoQsiUvPJcdiWgqJIX3IQGA47a+uwIDAQAB' \
'\n-----END PUBLIC KEY-----'
the line breaks are important.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论