OpenSSL Public Key Public Key Encryption String

I have a public key

-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwKMDEDjbP5v/9kcvpQKf IG3nU5Yid/tUNIeXBSDlxqhTEOKs8iQHXk0T17C4g7KHmrT2hxUomaAa2wwbfL+Z 4ppqvZZ4cu7CO6jaA0HyoBCU96siSuE0mPt8kU/PRA9+nAwu9lu9oYZUiVVJ3D4f o2bc+jWWL4GGY+PdSlz81ZW5cW/LOmNs9D0jJIxbwNocHxGgJ+xTZ3JKp6AO4MvL zXyipXu562N8wVc7UIgYYnvr63zFU8vzRL180X5x5MiJbjTYbfLH3z7qINPMZZLv A5vzJ0HX3J8rG96tmFuUzopCnvf+WVLvaS2T5uMxieK9dfA32CFQp4i3cj39c2b7 NwIDAQAB -----END PUBLIC KEY----- 

stored as a base64 encoded string (specified in MySQL as $ row ['public_key'])

 == 

in PHP. I am trying to use this string to encrypt string data, but it just fails without telling me why when I use the following code:

 $success = openssl_public_encrypt($data, $encrypted, base64_decode($row['public_key'])); 

When executed above, $success always false and $encrypted empty. I tried to start the public key through openssl_pkey_get_public() and send the resource to openssl_public_encrypt() without any luck. Writing to a file is not an option and does not store the private key.

Does anyone know how to make this work? (or does it work for everyone else and is it just a weird server problem?)

EDIT . Since there seems to be some confusion in my problem, here is the testing code I'm using and its output. (Note: I removed the base64 encoding to make sure it matters)

 $pubkey = openssl_get_publickey($row['public_key']); $publicKey = openssl_pkey_get_details($res); $encrypted = null; $success = openssl_public_encrypt($data, $encrypted, $pubkey); print "\npubkey1 " . $row['public_key']; print "\npubkey2 " . $pubkey; print "\npubkey3 " . $publicKey; print "\npubkey4 " . $publicKey["key"]; print "\nencryption " . ($success ? 'true' : 'false') . ' "' . $encrypted . '"'; $success = openssl_public_encrypt($data, $encrypted, $row['public_key']); print "\nencryption2 " . ($success ? 'true' : 'false') . ' "' . $encrypted . '"'; 

Output:

 pubkey1 -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA89FgfElm36q7iAf8frqa o58naoROsAljaFbmztfnqlmzXfyijK5CNJFdkUCgsu2zGxN7UlGiBOassTd4ijWm 1rz6/ad9fGXplfMGxZxyPCz31VreSWXmTG/PeSIYs1Co+dibV3imYt5jTxfLs7BZ WsT8nuLxGPw/o/gyKut0Ru+jXI2GgT4s3SylXinn/IbIA497SohqYA7/ViQnBwSL ZKUysOx2QgBmc9m0viRqDSKNUtDw7+L7bjhlwgZUGr6fxfTuNj9PWo97aPSE74CD owYYl2ToTboKSjZUszeNwQKpUnlHY/DBkwmYUJ7SAYDY70VNooadN5dZ4ehjdaka 6QIDAQAB -----END PUBLIC KEY----- pubkey2 Resource id #50 pubkey3 pubkey4 encryption false "" encryption2 false "" 
+4
source share
2 answers

So the problem is really data related. PKI for php has a very small length limit and cannot be used to encrypt large amounts of data. Instead, you should use PKI to encrypt the symmetric key, which is then used to encrypt / decrypt the data.

Read more @ How to encrypt long strings in PHP?

0
source

The key you must pass to this function is a PHP resource object, not a string representation of the key itself.

 $pubkey = openssl_get_publickey(base64_decode($row['public_key'])); $success = openssl_public_encrypt($data, $encrypted, $pubkey); 
+2
source

Source: https://habr.com/ru/post/1485896/


All Articles