Click here to Skip to main content
15,745,973 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have a RSA encrypted function written in Python as follows: I like to use it in my PHP server. Pleasee help me write these codes in PHP?

# ========== RSA Encrypt  ==========
def publicEncRSA(pubKey, data):
    keyDER = base64.b64decode(pubKey)
    keyPub = RSA.importKey(keyDER)
    cipher =
    cipher_text = cipher.encrypt(data.encode())

    encryptedData = base64.b64encode(cipher_text).decode("utf-8")
    # print('encryptedData = ' + encryptedData)

    return encryptedData
# ========== RSA Encrypt  ==========

pubKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmJ2tPgMTjsVcj7OY/DrbdHgQicicmED7guMgq3UedQkrKoD4PMeT2r1ziXFneEtaXtckF8BvbagYnsDXHmDBuj3k/npOuyoukUUl3eE2v1Np6pfaOMPYL5Ov+xhmbEfv9U+Z9wVPXpHCFoD57p4AZWxB31HWgGsUxilhXo28DHjB/ASOf3t3VMEXqoHxvfdlJx9vm8/BiX3YXuHpPB9nOGC04bseZO1wnN3woYmUZ+ZNZL2XYxongl9hJ2II2PTC9sdp6xHwRHbI+AF/RXAz8QqS9v958fuDbFT63HCSRH/bWwqliRDfpxLNOfvsgTdBIeDWNCa1akhe4qTDXPhAPwIDAQAB'

encKey = publicEncRSA(pubKey, '236o1230!23')

If I run this code, I get the results:
$ python

How can I get the same result in php?

What I have tried:

function publicEncRSA($pubKey, $data) {
    $keyDER = base64_decode($pubKey);
    $keyPub = openssl_pkey_get_public($keyDER);
    //$cipher =$keyPub);
    $cipher_text = openssl_private_encrypt( $data, $crypted, $keyPub );

    $encryptedData = base64_encode($cipher_text);
    # print('encryptedData = ' + encryptedData)

    return $encryptedData;
Updated 1-Nov-20 12:51pm
Richard MacCutchan 1-Oct-20 11:58am    
What is the problem?
Dave Kreskowiak 1-Oct-20 12:38pm    
OK, you're going to have to completely understand the code, any libraries being used, and the languages. You'll also have to find any equivalent libraries for use by the target language and understand those too.

So... what's the problem?

You didn't think anyone was just going to do the work for you, did you?
Seungwan Chae 1-Oct-20 12:56pm    
I've just left the question here for help while I've been trying to convert it to PHP. What's wrong? If you don't want to answer, you don't have to answer. This place is where any developers freely ask any stupid questions, isn't it?
Dave Kreskowiak 1-Oct-20 14:27pm    
Yes, it is, but you didn't ask a question, nor described a problem you are having.
KriShna RaJendra N PraSad 9-Oct-20 1:47am

Hope this link will be help for you. Try once.

1 solution

If you REALLY need a solution try using
<?php shell_exec("python3");?>
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900