2013-04-29 138 views
3

我期待在蘋果文檔的存摺,我需要:使用pycrypto PKCS#7創建簽名

  • 創建一個「清單文件的PKCS#7分離簽名」。

我理想上喜歡用Python做這個,我最好喜歡用pycrypto來完成這個任務,麻煩的是,我在網上找不到任何示例代碼來說明如何做到這一點,有很多這樣的代碼:

from Crypto.Cipher import PKCS1_v1_5 
from Crypto.PublicKey import RSA 
from Crypto.Hash import SHA 

message = 'To be encrypted' 
h = SHA.new(message) 

key = RSA.importKey(open('pubkey.der').read()) 
cipher = PKCS1_v1_5.new(key) 
ciphertext = cipher.encrypt(message+h.digest()) 

,但不知道有關的PKCS#7,我不知道我需要做的細節不夠......

有沒有人有什麼想法?

感謝

+0

由於這已經有幾年了,你有沒有找到解決方案? – WhyNotHugo 2015-08-15 00:04:50

回答

3

this answer很多的幫助,我做到了!

我知道op可能已經過去了,但這個答案在互聯網上是沒有的,所以這裏是解決方案,爲了後代的緣故!

from OpenSSL import crypto 

with open(cert) as cert_file: 
    cert_buf = cert_file.read() 

with open(key) as key_file: 
    key_buf = key_file.read() 

pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, key_buf) 
signcert = crypto.load_certificate(crypto.FILETYPE_PEM, cert_buf) 

bio_in = crypto._new_mem_buf(text.encode()) 
PKCS7_NOSIGS = 0x4 # defined in pkcs7.h 
pkcs7 = crypto._lib.PKCS7_sign(signcert._x509, pkey._pkey, crypto._ffi.NULL, bio_in, PKCS7_NOSIGS) # noqa 
bio_out = crypto._new_mem_buf() 
crypto._lib.i2d_PKCS7_bio(bio_out, pkcs7) 
sigbytes = crypto._bio_to_string(bio_out) 

請記住,它使用一些未公開的pyopenssl函數。