RSA is an asymmetric encryption algorithm that not only supports encryption and decryption, but also supports digital signatures. It ensures data security and prevents data tampering. Below are some common usage examples. Private Key signature
RSA is an asymmetric encryption algorithm that not only supports encryption and decryption, but also supports digital signatures. It ensures data security and prevents data tampering. Below are some common usage examples. Private Key signature-ensure that the submitted data is not tampered with during interaction between the Public Key signature and other systems. The most common practice in game access is to use MD5 encryption.
RSA is an asymmetric encryption algorithm that not only supports encryption and decryption, but also supports digital signatures. It ensures data security and prevents data tampering. Below are some common usage examples.
Private Key signature-public key verification
When interacting with other systems, it is often necessary to ensure that the submitted data is not tampered with. The most common practice in game access is to use MD5 encryption. Both parties process the communication data according to certain rules and combine the data with the KEY defined by both parties. Then, MD5 processing is performed on the data string. After receiving the data, the Receiver performs the same processing. If they are consistent, the request is normal and has not been tampered with; otherwise, the request is considered abnormal.
This method is simple and quick, but there is also a serious risk that both parties know the KEY and need to maintain it together. If the APPKEY is accidentally leaked, the normal situation can be simulated if you know the rules. This is risky.
The RSA signature method can effectively solve this problem. The RSA signature generates the corresponding private key and public key, uses the private key to sign the data, and then the receiver uses the public key to verify the signature. The signature action can only be performed by the private key, and the signature is verified by the public key. Similarly, the data returned by the receiver can be signed with the private key, which in turn uses the Public Key provided by the other party for signature verification. Each maintains its own private key, and the Public Key is provided to the other party for signature verification. The data passed by the signature must come from the private key signature of the other party to ensure security.
How to verify and add a signature in PHP:
public static function verify($data, $sign_str, $public_key){ if(empty($sign_str)) { return false; } $res = openssl_get_publickey($public_key); if(! is_resource($res)) { return false; } $status = openssl_verify($data, base64_decode($sign_str), $public_key); openssl_free_key($res); return $status;}public static function sign($data, $private_key){ $res = openssl_get_privatekey($private_key); if(! is_resource($res)) { return false; } $sign_str = ''; openssl_sign($data, $sign_str, $res); openssl_free_key($res); return base64_encode($sign_str);}Public key encryption-Private Key decryption
In addition to signatures, RSA can also be decrypted, which is the same as signing and verification. Common public key encryption-Private Key decryption ensures that data can only be decrypted with a private key, while private key encryption-Public Key decryption ensures that the data is signed with a private key.
Different from signature verification, encryption and decryption have a length limit. The ciphertext length of a 1024-bit key cannot exceed 117 bits, and that of a 2048-bit key cannot exceed 245 bits.
public static function pubEncrypt($data, $public_key){ $res = openssl_get_publickey($public_key); if(! is_resource($res)) { return false; } if(openssl_public_encrypt($data, $crypt_text, $res)) { openssl_free_key($res); return base64_encode($crypt_text); } return false;}public static function pubDecrypt($sign, $public_key){ $res = openssl_get_publickey($public_key); if(! is_resource($res)) { return false; } if(openssl_public_decrypt(base64_decode($sign), $data, $res)) { openssl_free_key($res); return $data; } return false;}public static function priEncrypt($data, $private_key){ $res = openssl_get_privatekey($private_key); if(! is_resource($res)) { return false; } if(openssl_private_encrypt($data, $crypt_text, $res)) { openssl_free_key($res); return base64_encode($crypt_text); } return false;}public static function priDecrypt($sign, $private_key){ $res = openssl_get_privatekey($private_key); if(! is_resource($res)) { return false; } if(openssl_private_decrypt(base64_decode($sign), $data, $res)) { openssl_free_key($res); return $data; } return false;}Generate key
In the manual, a valid openssl. cnf must be installed to use the openssl_pkey_new function to ensure proper operation of this function.
public static function create($bits = 1024){ $res = openssl_pkey_new(array( 'private_key_bits' => $bits, 'private_key_type' => OPENSSL_KEYTYPE_RSA )); openssl_pkey_export($res, $private_key); $public_key = openssl_pkey_get_details($res); $public_key = $public_key['key']; return array( 'private_key' => $private_key, 'public_key' => $public_key );}
Relevant demos and code are accessibleHttp://ipbt.vipsinaapp.com/demo/RSA/sign.php
Original article address: describes common RSA usage. Thank you for sharing it with the original author.