Python等效的RSA密钥Java代码中的密码加密部分:

huangapple go评论76阅读模式
英文:

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 = &#39;MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvBXTe278Lwg2MoI7iGKolSYuF+sNFKrsZplxCN9x0kItU3KIf8+1q60ILLwLewCEf7foxzpWp32j9YYU9vNBghuJ7BHcDYTffTRcv+QdNno491j701Hq7DIw13AGCQQTRcnfclvblnytIEWoQsiUvPJcdiWgqJIX3IQGA47a+uwIDAQAB&#39;

# encryption
text = &quot;test123&quot;.encode(&#39;utf-8&#39;)  # Plaintext
pub_bio = BIO.MemoryBuffer(pubkey.encode(&#39;utf-8&#39;))  # 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 &quot;encrypt_rsa_public_key.py&quot;, line 13, in &lt;module&gt;
    pub_rsa = RSA.load_pub_key_bio(pub_bio)  # Load public key
  File &quot;/Users/umeshpathak/env/py3env/lib/python3.7/site-packages/M2Crypto/RSA.py&quot;, line 444, in load_pub_key_bio
    rsa_error()
  File &quot;/Users/umeshpathak/env/py3env/lib/python3.7/site-packages/M2Crypto/RSA.py&quot;, 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 = &#39;-----BEGIN PUBLIC KEY-----\n&#39; \
         &#39;MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvBXTe278Lwg2MoI7iGKolSYuF+sNFKrsZplxCN9x0kItU3KIf8+1q60ILLwLewCEf7foxzpWp32j9YYU9vNBghuJ7BHcDYTffTRcv+QdNno491j701Hq7DIw13AGCQQTRcnfclvblnytIEWoQsiUvPJcdiWgqJIX3IQGA47a+uwIDAQAB&#39; \
         &#39;\n-----END PUBLIC KEY-----&#39;

the line breaks are important.

huangapple
  • 本文由 发表于 2020年5月29日 23:14:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/62089196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定