V
V
vapmaster2018-04-18 18:22:00
OpenSSL
vapmaster, 2018-04-18 18:22:00

PHP equivalent of openssl_public_decrypt() function in C#?

With the help of openssl, the public and private RSA 512 keys were generated. It is
necessary to decrypt the data in C # encrypted with the private key in PHP using the public key.
PHP code example:

$dataForEncryption = "Somedata";

printf("Data for encryption: %s\r\n\r\n", $dataForEncryption);

$hash = hash("SHA1", $dataForEncryption, true);

printf("SHA1 (hex): %s\r\n\r\n", bin2hex($hash));
printf("SHA1 (base64): %s\r\n\r\n", base64_encode($hash));

openssl_private_encrypt($hash, $encrypted, openssl_get_privatekey($private_key));

printf("Encrypted data: %s\r\n\r\n", bin2hex($encrypted));
printf("Encrypted data: %s\r\n\r\n", base64_encode($encrypted));
$public_key = <<<SOMEDATA777
-----BEGIN PUBLIC KEY-----
MDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAM3FNB5qt30VNntSHy75M9A3mfi8xG1K
cz+4LKcZuQMLAgMBAAE=
-----END PUBLIC KEY-----
SOMEDATA777;

openssl_public_decrypt($encrypted, $decrypted, openssl_get_publickey($public_key));
printf("Decrypted data: %s\r\n\r\n", bin2hex($decrypted));

Result of script execution:
Data for encryption: Somedata

SHA1 (hex): 13b05742beeaef568748ae6e7f40da744f4374f3

SHA1 (base64): E7BXQr7q71aHSK5uf0DadE9DdPM=

Encrypted data: 98e12894b164c700c26d04bca4a017191214460538b59b00ddff612732f7f94e

Encrypted data: mOEolLFkxwDCbQS8pKAXGRIURgU4tZsA3f9hJzL3+U4=

Decrypted data: 13b05742beeaef568748ae6e7f40da744f4374f3

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question