如何在python中加载rsa key(stirng)

时间:2017-03-08 09:58:14

标签: java python algorithm encryption rsa

我从Android应用程序获得了一个rsa主键,但我不知道如何在python中使用它。

在java代码中:

public class RSAHelper
{
  public static String decrypt(String paramString)
    throws Exception
  {
    return new String(RSAUtils.decryptByPrivateKey(Base64Utils.decode(paramString), "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIrrUGxh+yvNNI1c9hUg1rH+EtipI0nPk3zRm2Cj4mLDWLJ6DaTzdJTXTF3BYZaancWeG3QtBL+fITUi72InwBP7zaNG8uv/guwuhWT6V/YO7AaTrOFeTkg9NXuaFbn3hWVtZxQm2tIlaVa8snoNj3VGnPqIjXmGcxk4axuYd7sTAgMBAAECgYA43YhnRVh2nqJzd2k4Tt/zrmhyjhHm5fSetIKg9ZT3DrXhITsymYHQZ61X95AGATayLT1Zug/mjLIgOTO6f0ENkRQtjVCmKd8Yf/BeDEc5kRLUYDfSqoEydHK0+rCw5tJMgrAnQc5lHc+FVdGe2bOxKTEtZoss9VQ2jYuQ+Z5fUQJBANnvDOcI2OYSksX3PpHzO9F272xkmqYBRGkMc/a5RuOv1CY6FqMIkkloTf6nVl9y6XYV8gnHfbbI/wj4Q4UnPYsCQQCjLxyRYaOeEb/qOzSmFXytgMuCM9sr4eY9jpjzDgNWhpbtaVaf1QvSTXqN0zaUu4Se2tmWGX7zXw9p/dFf8DmZAkEAzl1o0FU2XhZ0WXVYEIhMunpvGSrirhNBHmAmZxjmoa/bqh8TVGpHa6+TO3JlfZioraL2QIBg8Ha/2VSNS0bvJQJALfCLaFpGh6+TicuVLNSLvwStRkB3CUmVWesVIAfn5KoLP1cSbfi6VUA+qkK18PVBhr8x1lHjLXyriDlOgmXMsQJAW9vD/IoBs4QJF87xF7tZvu/b1KRVgLM1edqOgVwMNbIQHBAXghjVjrpuln5w6z1dJ2cEjRP98OxKC0hqEIwIuQ=="), Charset.defaultCharset());
  }

  public static String encrypt(String paramString)
    throws Exception
  {
    return Base64Utils.encode(RSAUtils.encryptByPrivateKey(paramString.getBytes(), "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIrrUGxh+yvNNI1c9hUg1rH+EtipI0nPk3zRm2Cj4mLDWLJ6DaTzdJTXTF3BYZaancWeG3QtBL+fITUi72InwBP7zaNG8uv/guwuhWT6V/YO7AaTrOFeTkg9NXuaFbn3hWVtZxQm2tIlaVa8snoNj3VGnPqIjXmGcxk4axuYd7sTAgMBAAECgYA43YhnRVh2nqJzd2k4Tt/zrmhyjhHm5fSetIKg9ZT3DrXhITsymYHQZ61X95AGATayLT1Zug/mjLIgOTO6f0ENkRQtjVCmKd8Yf/BeDEc5kRLUYDfSqoEydHK0+rCw5tJMgrAnQc5lHc+FVdGe2bOxKTEtZoss9VQ2jYuQ+Z5fUQJBANnvDOcI2OYSksX3PpHzO9F272xkmqYBRGkMc/a5RuOv1CY6FqMIkkloTf6nVl9y6XYV8gnHfbbI/wj4Q4UnPYsCQQCjLxyRYaOeEb/qOzSmFXytgMuCM9sr4eY9jpjzDgNWhpbtaVaf1QvSTXqN0zaUu4Se2tmWGX7zXw9p/dFf8DmZAkEAzl1o0FU2XhZ0WXVYEIhMunpvGSrirhNBHmAmZxjmoa/bqh8TVGpHa6+TO3JlfZioraL2QIBg8Ha/2VSNS0bvJQJALfCLaFpGh6+TicuVLNSLvwStRkB3CUmVWesVIAfn5KoLP1cSbfi6VUA+qkK18PVBhr8x1lHjLXyriDlOgmXMsQJAW9vD/IoBs4QJF87xF7tZvu/b1KRVgLM1edqOgVwMNbIQHBAXghjVjrpuln5w6z1dJ2cEjRP98OxKC0hqEIwIuQ=="));
  }
}

我在python中尝试:

key = "MIICdgIBADANBgkqhkiG9w0BAQEFA..."
rsa.PrivateKey.load_pkcs1(key)

但我得到了:

ValueError: No PEM start marker "b'-----BEGIN RSA PRIVATE KEY-----'" found

如何使用python rsa模块加载此字符串主键?

添加BEGIN和END

In [109]: key
Out[109]: '-----BEGIN RSA PRIVATE KEY-----\nMIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIrrUGxh+yvNNI1c9hUg1rH+Etip I0nPk3zRm2Cj4mLDWLJ6DaTzdJTXTF3BYZaancWeG3QtBL+fITUi72InwBP7zaNG8uv/guwuhWT6V/YO7AaTrOFeTkg9NXuaFbn3hWVtZxQm2tIlaVa8snoN j3VGnPqIjXmGcxk4axuYd7sTAgMBAAECgYA43YhnRVh2nqJzd2k4Tt/zrmhyjhHm5fSetIKg9ZT3DrXhITsymYHQZ61X95AGATayLT1Zug/mjLIgOTO6f0EN kRQtjVCmKd8Yf/BeDEc5kRLUYDfSqoEydHK0+rCw5tJMgrAnQc5lHc+FVdGe2bOxKTEtZoss9VQ2jYuQ+Z5fUQJBANnvDOcI2OYSksX3PpHzO9F272xkmqYB RGkMc/a5RuOv1CY6FqMIkkloTf6nVl9y6XYV8gnHfbbI/wj4Q4UnPYsCQQCjLxyRYaOeEb/qOzSmFXytgMuCM9sr4eY9jpjzDgNWhpbtaVaf1QvSTXqN0zaU u4Se2tmWGX7zXw9p/dFf8DmZAkEAzl1o0FU2XhZ0WXVYEIhMunpvGSrirhNBHmAmZxjmoa/bqh8TVGpHa6+TO3JlfZioraL2QIBg8Ha/2VSNS0bvJQJALfCL aFpGh6+TicuVLNSLvwStRkB3CUmVWesVIAfn5KoLP1cSbfi6VUA+qkK18PVBhr8x1lHjLXyriDlOgmXMsQJAW9vD/IoBs4QJF87xF7tZvu/b1KRVgLM1edqO gVwMNbIQHBAXghjVjrpuln5w6z1dJ2cEjRP98OxKC0hqEIwIuQ==\n-----END RSA PRIVATE KEY-----'

In [110]: rsa.PrivateKey.load_pkcs1(key)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-110-0a0425032302> in <module>()
----> 1 rsa.PrivateKey.load_pkcs1(key)

c:\users\winside824\appdata\local\programs\python\python35-32\lib\site-packages\rsa\key.py in load_pkcs1(cls, keyfile, f ormat)
     73
     74         method = cls._assert_format_exists(format, methods)
---> 75         return method(keyfile)
     76
     77     @staticmethod

c:\users\winside824\appdata\local\programs\python\python35-32\lib\site-packages\rsa\key.py in _load_pkcs1_pem(cls, keyfi le)
    509
    510         der = rsa.pem.load_pem(keyfile, b('RSA PRIVATE KEY'))
--> 511         return cls._load_pkcs1_der(der)
    512
    513     def _save_pkcs1_pem(self):

c:\users\winside824\appdata\local\programs\python\python35-32\lib\site-packages\rsa\key.py in _load_pkcs1_der(cls, keyfi le)
    457             raise ValueError('Unable to read this file, version %s != 0' % priv[0])
    458
--> 459         as_ints = tuple(int(x) for x in priv[1:9])
    460         return cls(*as_ints)
    461

c:\users\winside824\appdata\local\programs\python\python35-32\lib\site-packages\rsa\key.py in <genexpr>(.0)
    457             raise ValueError('Unable to read this file, version %s != 0' % priv[0])
    458
--> 459         as_ints = tuple(int(x) for x in priv[1:9])
    460         return cls(*as_ints)
    461

TypeError: int() argument must be a string, a bytes-like object or a number, not 'Sequence'

解决:

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
from base64 import b64decode

key = "MIICdgIBADANBgkqhkiG9w0BAQEFA..."
msg = "plain text"
key = b64decode(key)
private_key = RSA.importKey(key)
cipher = PKCS1_v1_5.new(private_key)
cipher.encrypt(msg)

1 个答案:

答案 0 :(得分:0)

rsa.PrivateKey.load_pkcs1默认使用PEM format

因此,它要求密钥以'-----BEGIN RSA PRIVATE KEY-----\n开头并以

结尾

'\n-----END RSA PRIVATE KEY-----'

pem_prefix = '-----BEGIN RSA PRIVATE KEY-----\n'
pem_suffix = '\n-----END RSA PRIVATE KEY-----'
key = "MIICdgIBADANBgkqhkiG9w0BAQEFA..."
key = '{}{}{}'.format(pem_prefix, key, pem_suffix)
rsa.PrivateKey.load_pkcs1(key)