Joe Smith
Joe Smith

Reputation: 31

how do I transfer this python encrypt functions (aes, rsa public key) to php with openssl and rsa?

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) {
 ......?
}

Upvotes: 1

Views: 227

Answers (1)

Richard Wan
Richard Wan

Reputation: 86

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?

Upvotes: 1

Related Questions