Because the HTTP request is stateless, we don't know who the requester is. So it was born. Signature, the receiver and the requester negotiate a signature method to verify, to obtain mutual trust, the next business logic Exchange.
Where signature is used much is the public key private key, with the private key signature, public key verification, or public key encryption, private key decryption.
Whether it's a public key or a private key, we'll first format it, but if you get the formatting you can ignore this step
1. Formatting of public key private key
Private key Format ********************/functionFormatprikey ($priKey) { $fKey= "-----BEGIN PRIVATE KEY-----\ n"; $len=strlen($priKey); for($i= 0;$i<$len; ) { $fKey=$fKey.substr($priKey,$i, 64). "\ n"; $i+ = 64; } $fKey. = "-----END PRIVATE KEY-----"; return $fKey;}/******************** Public key formatting ********************/functionFormatpubkey ($pubKey) { $fKey= "-----BEGIN public KEY-----\ n"; $len=strlen($pubKey); for($i= 0;$i<$len; ) { $fKey=$fKey.substr($pubKey,$i, 64). "\ n"; $i+ = 64; } $fKey. = "-----END public KEY-----"; return $fKey;}
Formatting is the addition of a prefix, then every 64 bits of line wrapping, but also as follows simple format:
// private key Formatting $fKey = "-----BEGIN PRIVATE KEY-----\ n". Chunk_split ($public _key, "\ n"). ' -----END PRIVATE KEY-----'; // Public Key Formatting $fKey = "-----BEGIN public KEY-----\ n". Chunk_split ($public _key, "\ n"). ' -----END Public KEY-----';
2. Private key signature and public key verification
/******************** private key signature ********************/functionGet_private_sign ($sign _str,$private _key,$signature _alg=OPENSSL_ALGO_SHA1) { $private _key= Openssl_pkey_get_private (Private_key);//Load keyOpenssl_sign ($sign _str,$signature,$private _key,$signature _alg);//Generate Signature $signature=Base64_encode($signature); Openssl_free_key ($private _key); return $signature;}/******************** Public Key verification ********************/functionPublic_verify ($sign _str,$sign,$public _key,$signature _alg=OPENSSL_ALGO_SHA1) { $public _key= Openssl_get_publickey ($public _key); $verify= Openssl_verify ($sign _str,Base64_decode($sign),$public _key,$signature _alg); Openssl_free_key ($public _key); return $verify==1;//false or True}
$sign _str is a signature string or a string of signatures, $sign the signature, the public key must be formatted, otherwise it will not be recognized.
3. Public key encryption and private key decryption
/******************** Public Key Cryptography ********************/functionGet_public_sign ($sign _str,$public _key,$signature _alg=OPENSSL_ALGO_SHA1) { $public _key= Openssl_pkey_get_public ($public _key);//Load keyOpenssl_sign ($sign _str,$signature,$public _key,$signature _alg);//Generate Signature $signature=Base64_encode($signature); Openssl_free_key ($public); return $signature;}/******************** private key decryption ********************/ functionPrivate_verify ($sign _str,$sign,$private _key,$signature _alg=OPENSSL_ALGO_SHA1) { $private _key= Openssl_get_privatekey ($private _key); $verify= Openssl_verify ($sign _str,Base64_decode($sign),$private _key,$signature _alg); Openssl_free_key ($private _key); return $verify==1;//false or True}
PHP Signature Public key, private key explanation