Php is connected to java reality signature-adding instances and java

Source: Internet
Author: User
Tags begin rsa private key

Php is connected to java reality signature-adding instances and java

The main method I implemented is to convert the key generated by java into a key in the pem format that php can recognize. Other signatures and signatures use the built-in openssl signature method.

The key generated by java is mainly a string, while the pem-format key is a 64-bit line with the following header and tail file format, and php then obtains the corresponding pem-format string.

Pem format key

----- Begin public key ----- // 64chars one row (multiple rows) ----- end public key---begin rsa private key ----- // 64chars one row (multiple rows) ----- end rsa private key -----

1. php code for converting java keys to pem format

/*** Format the string public/private key as the pem public/Private Key * @ param $ secret_key * @ param $ type * @ return string */public static function format_secret_key ($ secret_key, $ type) {// a line break "\ n" after 64 English characters, and a line break "\ n" $ key = (wordwrap ($ secret_key, 64, "\ n", true )). "\ n"; // Add the pem format header and end if ($ type = 'pub') {$ pem_key = "----- begin public key ----- \ n ". $ key. "----- end public key ----- \ n";} else if ($ type = 'pri ') {$ pem_key = "----- begin rsa private key ----- \ n ". $ key. "----- end rsa private key ----- \ n" ;}else {echo ('illegal Public/private key type'); exit () ;}return $ pem_key ;}

2. Signature

/*** RSA signature * @ param $ paramStr * @ param $ priKey * @ return string */public static function sign ($ paramStr, $ priKey) {$ sign = ''; // convert the string-format Public/Private Key to the pem-format Public/Private Key $ priKeyPem = SignUtil: format_secret_key ($ priKey, 'pri '); // convert it to an openssl key, must be a private key without pkcs8 conversion $ res = openssl_get_privatekey ($ priKeyPem); // call the openssl built-in signature method to generate a signature $ sign openssl_sign ($ paramStr, $ sign, $ res); // release the resource openssl_free_key ($ res); // base64 encoded signature $ signBase64 = base64_encode ($ sign ); // url encoded signature $ sign = urlencode ($ signBase64); return $ sign ;}

3. Signature Verification

/*** RSA signature * @ param $ paramStr * @ param $ sign * @ param $ pubKey * @ return bool */public static function verify ($ paramStr, $ sign, $ pubKey) {// convert a string-format public/private key to a pem-format Public/Private Key $ pubKeyPem = SignUtil: format_secret_key ($ pubKey, 'pub'); // convert it to an openssl key, must be a public key without pkcs8 conversion $ res = openssl_get_publickey ($ pubKeyPem); // url Decoding signature $ signUrl = urldecode ($ sign ); // base64 decoded signature $ signBase64 = base64_decode ($ signUrl); // call the openssl built-in method signature and return the bool value $ result = (bool) openssl_verify ($ paramStr, $ signBase64, $ res); // release the resource openssl_free_key ($ res); // return whether the resource is successfully returned $ result ;}

The above php code is used to connect to the java real-time signature verification example, which is all the content shared by the editor. I hope to give you a reference and support for the help house.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.