我想用PyCrypto加密python中的一些数据.
但是我在使用时遇到错误key = RSA.importKey(pubkey):
RSA key format is not supported
Run Code Online (Sandbox Code Playgroud)
密钥生成时:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mycert.key -out mycert.pem
Run Code Online (Sandbox Code Playgroud)
代码是:
def encrypt(data):
pubkey = open('mycert.pem').read()
key = RSA.importKey(pubkey)
cipher = PKCS1_OAEP.new(key)
return cipher.encrypt(data)
Run Code Online (Sandbox Code Playgroud) 关于appengine的SignedJwtAssertionCredentials(使用pycrypto 2.6)不支持PKCS12格式,因此我正在尝试使用PEM密钥,正如所建议的那样.
这是我的代码:
f = file(os.path.join(os.path.dirname(__file__), KEY_FILE), "r")
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key,
scope="https://www.googleapis.com/auth/drive"
http = httplib2.Http()
http = credentials.authorize(http)
Run Code Online (Sandbox Code Playgroud)
并且KEY_FILE是一个PEM键,使用以下命令转换:
openssl pkcs12 -in privatekey.p12 -nodes -nocerts > privatekey.pem
Run Code Online (Sandbox Code Playgroud)
但我仍然得到这个错误,好像它没有认识到这是一个PEM密钥:
NotImplementedError: PKCS12 format is not supported by the PyCrpto library.
Try converting to a "PEM" (openssl pkcs12 -in xxxxx.p12 -nodes -nocerts > privatekey.pem) or using PyOpenSSL if native code is an option.
Run Code Online (Sandbox Code Playgroud)
如果我只将文件名传递给构造函数(不读取文件的内容),则会出现同样的错误
任何的想法?
我正在尝试运行此处提供的示例https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-py进行授权.
我从SO中的其他问题中注意到(ImportError:无法导入名称SignedJwtAssertionCredentials)SignedJwtAssertionCredentials已被删除,因此无法导入.
因此,我开始遵循GitHub页面(https://github.com/google/oauth2client/issues/401)和StackOverflow 提供的解决方案.到目前为止,没有任何效果,我仍然看到同样的错误.以下是我的代码.
import argparse
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools
Run Code Online (Sandbox Code Playgroud)
而且,这是我在运行上述代码时收到的错误.
ImportError: cannot import name ServiceAccountCredentials
Run Code Online (Sandbox Code Playgroud)
由于我是这个领域的全新手,我试着为OAuth(2.0.0和1.5.2)这两个版本做到这一点.我也在安装后尝试过pyopenssl,但仍然失败了.
python google-api google-analytics-api oauth-2.0 google-oauth
openssl ×2
pycrypto ×2
python ×2
google-api ×1
google-oauth ×1
oauth-2.0 ×1
pem ×1
pkcs#12 ×1
rsa ×1