The length of encrypted content is limited to 11 bits less than the key length. For example, a 128-bit key can encrypt up to 117 pieces of content.
Public key encryption
$public_content=file_get_contents(public key path);
$public_key=openssl_get_publickey($public_content);
$original_str='content to be encrypted';
$original_arr=str_split($original_str,117); //divide
foreach($ original_arr as $o)
{
$sub_enc=null;
openssl_public_encrypt($o,$sub_enc,$public_key);
$original_enc_arr[]=$sub_enc;
}
openssl_free_key($public_key);
$original_enc_str=base64_encode(implode('',$original_enc_arr ));//
Decrypt
the ciphertext private key transmitted by the final network $private_content=file_get_contents(private key path);
$private_key=openssl_get_privatekey($private_content);
$original_enc_str=base64_decode(ciphertext);
$orig_dec_str='';
for($i=0;$i<strlen($original_enc_str)/128;$i++)
{
$data=substr($original_enc_str,$i*128,128);
openssl_private_decrypt($data,$decrypt,$private_key);
$orig_dec_str .=$decrypt;
}
$orig_dec_str is the last decrypted one.
Note: Private key encryption and public key decryption are the same.
-------------------------------------------------- -------------------------------------------------- -------------------
Private key signature
$private_content=file_get_contents(private key path);
$private_key=openssl_get_privatekey($private_content);
$original_str='';//Original data
openssl_sign($original_str,$sign,$private_key);
openssl_free_key($private_key);
$sign=base64_encode($sign);//Final signature
Public key verification
$public_content=file_get_contents(public key path);
$public_key=openssl_get_publickey($public_content);
$sign=base64_decode($sign)'';//Get the signature
$original_str=''; Get the data
$result=(bool)openssl_verify($original_str,$sign,$public_key);
openssl_free_key($public_key);
$ When the result is true, the signature check passes, and when it is false, it fails.