2010-05-04 6 views
6

Per connettere un server, ho trovato che, usando PHP, ho bisogno di usare openssl_seal(). Va bene, ma voglio usare Python. Non sono in grado di convertire openssl_seal() in una funzione equivalente.openssl_seal() in Python

Potete aiutarmi?

Questo è ciò che openssl_seal() fa:

Descrizione int openssl_seal (string $ data, stringa & $ sealed_data, serie & $ env_keys, array $ pub_key_ids)

openssl_seal() seals (encrypts) data by using RC4 with a randomly generated 
secret key. The key is encrypted with each of the public keys associated 
with the identifiers in pub_key_ids and each encrypted key is returned in 
env_keys. This means that one can send sealed data to multiple recipients 
(provided one has obtained their public keys). Each recipient must receive 
both the sealed data and the envelope key that was encrypted with the 
recipient's public key. 

risposta

2

this blogpost ha una descrizione molto dettagliata di cosa sta succedendo all'interno di openssl_seal(). Ha anche un'implementazione in java.

Da questo, penserei che dovrebbe essere relativamente semplice ("la prova lasciato come esercizio per il lettore" tipo di semplice) per fare un'implementazione equivalente in Python usando pyopenssl, che comprende RC4, o il più recente, ma per questi scopi più concentrato tlslite.

+0

blogpost è morto :( –

+0

@MihaiOprea collegamento aggiornato per puntare alla versione archivio web. Ricordati di donare alla loro causa. – Steen

1

Nei openssl_seal fa è:

  1. Estrarre il public_key dal certificato
  2. generare un 128 bit (16 byte) lungo random_key (questo sarà utilizzato per crittografare il messaggio utilizzando un algoritmo simmetrico, poiché è più veloce)
  3. crittografare il random_key utilizzando PKCS # 1
  4. crittografare il messaggio utilizzando ARC4 e il random_key
  5. uscita l'encrypted_random_key e l'encrypted_message

La parte ricevente può quindi decifrare la chiave encrypted_random_ utilizzando il proprio private_key e quindi decrittografare il messaggio encrypted_message utilizzando il tasto random_key.

Dal momento che non c'è modo di fare questo in Python tramite la libreria standard, io sono solo gonna' buttare via i 3 approcci che ho provato:

# pyca/cryptography (cryptography.io) version 
# pip install cryptography 

import os 

import cryptography 
from cryptography import x509 


message = 'Super secret secret message' 
message = message.encode('utf-8') 
certificate_data = open('/path/to/certificate.cer', 'r').read() 
certificate_data = certificate_data.encode('utf-8') 
certificate = cryptography.x509.load_pem_x509_certificate(data=certificate_data, backend=cryptography.hazmat.backends.default_backend()) 
public_key = certificate.public_key() 
random_key = os.urandom(16) 
encrypted_random_key = public_key.encrypt(plaintext=random_key, padding=cryptography.hazmat.primitives.asymmetric.padding.PKCS1v15()) 
print(encrypted_random_key) 
algorithm = cryptography.hazmat.primitives.ciphers.algorithms.ARC4(random_key) 
cipher = cryptography.hazmat.primitives.ciphers.Cipher(algorithm=algorithm, mode=None, backend=cryptography.hazmat.backends.default_backend()) 
encryptor = cipher.encryptor() 
encrypted_message = encryptor.update(message) 
print(encrypted_message) 

.

# M2Crypto version 
# pip install pip install git+https://gitlab.com/m2crypto/[email protected] 

import M2Crypto 


message = 'Super secret secret message' 
message = message.encode('utf-8') 
certificate = M2Crypto.X509.load_cert('/path/to/certificate.cer') 
public_key = certificate.get_pubkey() 
rsa_pub = public_key.get_rsa() 
random_key = M2Crypto.Rand.rand_bytes(16) 
encrypted_random_key = rsa_pub.public_encrypt(random_key, M2Crypto.RSA.pkcs1_padding) 
print(encrypted_random_key) 
cipher = M2Crypto.EVP.Cipher(alg='rc4', key=random_key, iv=b'', op=M2Crypto.encrypt) 
encrypted_message = cipher.update(message) 
encrypted_message += cipher.final() 
print(encrypted_message) 

.

# PyCrypto version 
# pip install pycrypto 

# Please bear in mind that PyCrypto cannot handle x509 certificates. 
# You will have to extract the public_key to a pem file: 
# openssl x509 -inform pem -in certificate.cer -pubkey -noout > public_key.pem 

from Crypto import Random 
from Crypto.Cipher import ARC4 
from Crypto.Cipher import PKCS1_OAEP 
from Crypto.Cipher import PKCS1_v1_5 
from Crypto.PublicKey import RSA 


message = 'Super secret secret message' 
message = message.encode('utf-8') 
public_key_data = open('/path/to/public_key.pem', 'r').read() 
public_key = RSA.importKey(public_key_data) 
random_key = Random.new().read(16) 
cipher = PKCS1_v1_5.new(public_key) 
encrypted_random_key = cipher.encrypt(random_key) 
print(encrypted_random_key) 
cipher = ARC4.new(random_key) 
encrypted_message = cipher.encrypt(message) 
print(encrypted_message) 

È possibile controllare il mio post a =>http://helpfulsheep.com/2017-09-01-openssl-seal-in-python/

+0

Questo dovrebbe essere contrassegnato come risposta. –