1

Hello i want to pass this python code to php, but i don't known how, so anyone can help me or explain me more?

Here is the code i write in python.

python

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives.ciphers.aead import AESCCM

def dec_public_key():
    backend = default_backend()
    #
    e = int("103....", 16)
    n = int("DASCGFWQFCA13......", 16)
    public_number = rsa.RSAPublicNumbers(e, n)
    return backend.load_rsa_public_numbers(public_number)

def enc_public_key(public_key, text):
    # here i have some questions, php have PKCS1v15?
    return public_key.encrypt(text, padding.PKCS1v15())

def aes_key():
    # ---
    return AESCCM.generate_key(256)

def nc():
    # random bytes
    return urandom(12)

def encrypt_aes_key(aes_key, nc, text):
    # ..
    cipher = AESCCM(aes_key, tag_length=8)
    return cipher.encrypt(nc, text, None)

php

Here is the code in php but idk about the rest....

<?php

function nc() {
    // here generate a random bytes
    return random_bytes(12);
}

function aes_key() {
    # this function generate a random bytes base 256 lenght with the algo aes-256-ccm
    return random_bytes(openssl_cipher_iv_length("aes-256-ccm"));
}

function dec_public_key() {
 ......?
}

function enc_public_key(public_key, text) {
 ......?
}

function encrypt_aes_key(aes_key, nc, text) {
 ......?
}
Joe Smith
  • 21
  • 1
  • As far as I can see you have a hybrid encryption on Python side (AES in GCM mode for data encryption and RSA encryption with PKCS1.5 padding for key encryption). Both is available with PHP built-in OpenSSL support. Just one note on your public key: you have it in "modulus" and "exponent" form but PHP/OpenSSL is just accepting a PEM encoded one so just need to convert it. Kindly note that your question is much too broad here on Stackoverflow to get a "full" answer. – Michael Fehr May 16 '21 at 07:19
  • _phpseclib_ supports importing a public key if modulus and exponent are given, but not CCM. _openssl_ in turn supports CCM, but not importing the key from modulus and exponent. If you can apply GCM instead of CCM, _phpseclib_ is probably the most convenient way. Otherwise _openssl_, whereas the public key has to be converted externally to X.509/SPKI format beforehand. – Topaco May 16 '21 at 09:43

1 Answers1

1

Actually, I don't think u can do this.cryptography.hazmat is a large package, if you really wanna transfer ur program into php script, I think u have to transfer the cryptography.hazmat(or functional part) into php.

So why don't u make this python encryption process as a service for php calling?

  • `exec('python get_thing.py aes') ?` .... but i think theres also fairly straightforward methods to achieve this with php (ie without python) – Joran Beasley May 16 '21 at 02:02
  • 1
    I think u can make python script as a http or socket service, php can call it by curl – Richard Wan May 16 '21 at 02:10
  • Yeah, i use it with exec and socket service, but i want to translate it to php, i'll try to pass "cryptography.hazmat" lib to php – Joe Smith May 16 '21 at 02:24
  • u want rsa encryption&decrytion in php, right? may be u can check this: https://stackoverflow.com/questions/4484246/encrypt-and-decrypt-text-with-rsa-in-php – Richard Wan May 16 '21 at 02:31