Click here to Skip to main content
14,691,047 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_PKCS1_v1_5.new(keyPub)
    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')
print(encKey)


If I run this code, I get the results:
$ python status_id.py
U51jKvDgsqmNMDf5sPA04rKwZxsWx28w3c44DXF4E2qvgUTcOFsRNWkrNC8WktrBgkqjTcAfcxBj6hPIeJ3n0qMdGhbkmmIfeioKeTI+8msOpQ79IKsJrrN5YiFgOPA50KpEEZCdfDVXWIpsfvpqvnj0YRqB00Rc8jMgw9IPSRoDuL9fYO4qHLerB761rkBRTx01tYHFJr4sPIEu2eMB1ceqh4jI7t6cHk2aJLbgSwujZJWH1rf5ydQRqHN6XQtiCH894MX4SC/o1seUZiM8z5usE7uDUDekulHZrDJw7l3VBPEapeBvrR44J4x1sHOwP7cZC5ayxedj47BSNuoM/Q==



How can I get the same result in php?
thx.

What I have tried:

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

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

    return $encryptedData;
}
Posted
Updated 1-Nov-20 13:51pm
v2
Comments
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
   
https://stackoverflow.com/questions/4116596/converting-python-code-to-php

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

1 solution

If you REALLY need a solution try using
<?php shell_exec("python3 FILE.py");?>
   

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