NodeJS forge Encryption Equivalent in PHP

Known:

My Client has nodeJS code:

function EncRSA(str){

        let data = Buffer.from(str,'utf8');

        let publicKey = forge.pki.publicKeyFromPem('-----BEGIN PUBLIC KEY-----

MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgGFzlANAwlCNrGZ+VBtUjohZ1tSc
c8LqG6ulJRCIHkk0akuJG4MfcPCXKe/6Kc5YtZfUbNEIyRNrgbFaao+2hbN9jfkA
JoIClLcRFxFPwLMdYKvMXPyaPl9PgJ4bgCUSLNiJjqiWXQZAQTP/ppW5hAGdcTbl
7NlsD75GnjlPVW4BAgMBAAE=

-----END PUBLIC KEY-----');

        let encrypted = publicKey.encrypt(data, 'RSA-OAEP');

        return Buffer.from(encrypted,'binary').toString("base64");

    }

Private Key:

-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgGFzlANAwlCNrGZ+VBtUjohZ1tScc8LqG6ulJRCIHkk0akuJG4Mf
cPCXKe/6Kc5YtZfUbNEIyRNrgbFaao+2hbN9jfkAJoIClLcRFxFPwLMdYKvMXPya
Pl9PgJ4bgCUSLNiJjqiWXQZAQTP/ppW5hAGdcTbl7NlsD75GnjlPVW4BAgMBAAEC
gYBWUk7V4tM9D4Ga+GQAIZJv7USC6GMSUNVZ4dtHHYvWRHuzlSutpnggZYZccDf/
9ExGxX/uWd3jOeOT+gdSbddX4xiDxhtOlhDJf5DN13wjHElhtxVdEKWvsDt8U4R7
gEBNiSbziMkxvwgfZP7PepwF62ET+UhE2vZjcDioUrnwwQJBAJ6lZgcsZbI4DKjh
epu3+NyVpELnUFtbTcMGr1EmABrMW/EpKCCNOfLdI4I8q+/dSyMumfXl5vMuKLAo
rLyOws0CQQCdQMagbnTtkRL4vG90VGOSSOUjXfuGlC8RjR83AcsOJnIBWTjGZxjo
tv51IZJ3dDoEU/APvdudbur8agu8+iAFAkEAlC5tdDnV63dK69tgCF74hiEHwq4I
cqx/n7B4xNU3t9A1+LW2k2EWRXCkoBtHhc4vRndau0G4TGBLBO+ZGK5H6QJAVD4Q
an/iCibu9+vS9YoD8ak1f9XoWndmI8vrTgea5VC5QHmwzbELihgGh29dzLqqHglt
kLFQRq3WGzUgZrPElQJAEn6gHgIN2xsu5JhxnaVe/rZsWbrxkigbSssuJAxZoCbJ
Vj+Eu3dVxcxfBl1tlD7HyNwnJg87xaJ9cRuKdvto3g==
-----END RSA PRIVATE KEY-----

Request:

How to make it in php and also make a decrypt function … ?

Solution:

I make this code:

<?php
$pubkey='MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgGFzlANAwlCNrGZ+VBtUjohZ1tSc
c8LqG6ulJRCIHkk0akuJG4MfcPCXKe/6Kc5YtZfUbNEIyRNrgbFaao+2hbN9jfkA
JoIClLcRFxFPwLMdYKvMXPyaPl9PgJ4bgCUSLNiJjqiWXQZAQTP/ppW5hAGdcTbl
7NlsD75GnjlPVW4BAgMBAAE=';

$privkey='MIICWwIBAAKBgGFzlANAwlCNrGZ+VBtUjohZ1tScc8LqG6ulJRCIHkk0akuJG4Mf
cPCXKe/6Kc5YtZfUbNEIyRNrgbFaao+2hbN9jfkAJoIClLcRFxFPwLMdYKvMXPya
Pl9PgJ4bgCUSLNiJjqiWXQZAQTP/ppW5hAGdcTbl7NlsD75GnjlPVW4BAgMBAAEC
gYBWUk7V4tM9D4Ga+GQAIZJv7USC6GMSUNVZ4dtHHYvWRHuzlSutpnggZYZccDf/
9ExGxX/uWd3jOeOT+gdSbddX4xiDxhtOlhDJf5DN13wjHElhtxVdEKWvsDt8U4R7
gEBNiSbziMkxvwgfZP7PepwF62ET+UhE2vZjcDioUrnwwQJBAJ6lZgcsZbI4DKjh
epu3+NyVpELnUFtbTcMGr1EmABrMW/EpKCCNOfLdI4I8q+/dSyMumfXl5vMuKLAo
rLyOws0CQQCdQMagbnTtkRL4vG90VGOSSOUjXfuGlC8RjR83AcsOJnIBWTjGZxjo
tv51IZJ3dDoEU/APvdudbur8agu8+iAFAkEAlC5tdDnV63dK69tgCF74hiEHwq4I
cqx/n7B4xNU3t9A1+LW2k2EWRXCkoBtHhc4vRndau0G4TGBLBO+ZGK5H6QJAVD4Q
an/iCibu9+vS9YoD8ak1f9XoWndmI8vrTgea5VC5QHmwzbELihgGh29dzLqqHglt
kLFQRq3WGzUgZrPElQJAEn6gHgIN2xsu5JhxnaVe/rZsWbrxkigbSssuJAxZoCbJ
Vj+Eu3dVxcxfBl1tlD7HyNwnJg87xaJ9cRuKdvto3g==';

$publicKey  = "-----BEGIN PUBLIC KEY-----\n" .  wordwrap($pubkey, 64, "\n", true)  . "\n-----END PUBLIC KEY-----";

$privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" . wordwrap($privkey, 64, "\n", true) . "\n-----END RSA PRIVATE KEY-----";


function encrypt($data)
    {
		
	$utf8=utf8_encode($data);
		
        if (openssl_public_encrypt($utf8, $encrypted,$GLOBALS['publicKey'],OPENSSL_PKCS1_OAEP_PADDING))
            $data = base64_encode($encrypted);
        else
            throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?');

        return $data;
    }
	
function decrypt($data)
    {
	$hasil='';
		
        if (openssl_private_decrypt(base64_decode($data), $decrypted,$GLOBALS['privateKey'],OPENSSL_PKCS1_OAEP_PADDING))
           return utf8_decode($decrypted);
       else
	    return $hasil;
	}

Explain:

  • Buffer.from(str,’utf8′) equivalent with utf8_encode($data)
  • forge.pki.publicKeyFromPem equaivalent with openssl_public_encrypt($utf8, $encrypted,$GLOBALS[‘publicKey’])
  • publicKey.encrypt(data, ‘RSA-OAEP’) is equivalent with OPENSSL_PKCS1_OAEP_PADDING
  • Buffer.from(encrypted,’binary’).toString(“base64”) is equivalent with base64_encode($encrypted);

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *