2016-09-22 4 views
5

Je suis générer une clé CE en utilisant le module de cryptographie python de cette manièreComment trouver les composants ASN.1 de clé CE python-cryptographie

from cryptography.hazmat.backends import default_backend 
from cryptography.hazmat.primitives.asymmetric import ec 
key=ec.generate_private_key(ec.SECP256R1(), default_backend()) 

La structure ASN.1 de la clé C'est comme suit

ECPrivateKey ::= SEQUENCE { 
version  INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1), 
privateKey  OCTET STRING, 
parameters [0] ECParameters {{ NamedCurve }} OPTIONAL, 
publicKey [1] BIT STRING OPTIONAL 
} 

de https://tools.ietf.org/html/rfc5915 setion 3.

ma question est de savoir comment obtenir les composants ASN.1 de cette clé. Je veux convertir l'objet clé de la clé privée OpenSSH, quelque chose comme

-----BEGIN EC PRIVATE KEY----- 
Proc-Type: 4,ENCRYPTED 
DEK-Info: AES-128-CBC,9549ED842979FDAF5299BD7B0E25B384 

Z+B7I6jfgC9C03Kcq9rbWKo88mA5+YqxSFpnfRG4wkm2eseWBny62ax9Y1izGPvb 
J7gn2eBjEph9xobNewgPfW6/3ZDw9VGeaBAYRkSolNRadyN2Su6OaT9a2gKiVQi+ 
mqFeJmxsLyvew9XPkZqQIjML1d1M3T3oSA32zYX21UY= 
-----END EC PRIVATE KEY----- 

Il est facile avec la manipulation DSA ou RSA parce que tous les paramètres ASN.1 sont des nombres entiers dans ce.

Merci Vous à l'avance

Répondre

0

Il est relativement facile d'extraire le point commun de la séquence ASN.1 en utilisant pyasn1, mais si vous voulez PKCS1 PEM-crypté (alias « OpenSSL traditionnel ») puis pyca/cryptographie peut faire qui assez facilement:

from cryptography.hazmat.backends import default_backend 
from cryptography.hazmat.primitives import serialization 
from cryptography.hazmat.primitives.asymmetric import ec 

backend = default_backend() 

key = ec.generate_private_key(ec.SECP256R1(), backend) 
serialized_key = key.private_bytes(
    serialization.Encoding.PEM, 
    serialization.PrivateFormat.TraditionalOpenSSL, 
    serialization.BestAvailableEncryption(b"my_great_password") 
) 

Vous trouverez de plus amples informations sur la méthode private_bytes dans la documentation. À ce moment BestAvailableEncryption va crypter en utilisant AES-256-CBC.