About PHP generated certificate key information is really very few ah, check for a long, finally in the official documents found relevant information, and according to their own understanding, organized into the following code, divided into two parts: Generate certificate key, encryption and decryption data. Just copy it and make two files to run it. Has written a detailed note, I believe that PHP programmers can read. generate.php
- $DN = Array (
- "CountryName" = ' XX ',//country name
- "Stateorprovincename" = ' state ',//province name
- "Localityname" = ' somewherecity ',//city name
- "OrganizationName" = ' myself ',//registrant Name
- "Organizationalunitname" = ' Whatever ',//Organization name
- "CommonName" = ' myself ',//Public name
- "EmailAddress" = ' user@domain.com '//Email
- );
- $privkeypass = ' 111111 '; Private Key Password
- $numberofdays = 365; Effective duration
- $cerpath = "./test.cer"; Generate Certificate Path
- $pfxpath = "./test.pfx"; Key file path
- Generate certificate
- $privkey = Openssl_pkey_new ();
- $CSR = Openssl_csr_new ($dn, $privkey);
- $sscert = openssl_csr_sign ($CSR, NULL, $privkey, $numberofdays);
- Openssl_x509_export ($sscert, $csrkey); Export Certificate $csrkey
- Openssl_pkcs12_export ($sscert, $privatekey, $privkey, $privkeypass); Export Key $privatekey
- Generate a certificate file
- $fp = fopen ($cerpath, "w");
- Fwrite ($fp, $csrkey);
- Fclose ($FP);
- Generate Key File
- $fp = fopen ($pfxpath, "w");
- Fwrite ($fp, $privatekey);
- Fclose ($FP);
- ?>
Copy Code crypt.php
- $privkeypass = ' 111111 '; Private Key Password
- $pfxpath = "./test.pfx"; Key file path
- $priv _key = file_get_contents ($pfxpath); Get key File contents
- $data = "Test"; Encrypted data test
- Private key encryption
- Openssl_pkcs12_read ($priv _key, $certs, $privkeypass); Read public key, private key
- $prikeyid = $certs [' Pkey ']; Private
- Openssl_sign ($data, $SIGNMSG, $prikeyid, OPENSSL_ALGO_SHA1); Registering to generate encrypted information
- $SIGNMSG = Base64_encode ($SIGNMSG); Base64 transcoding Encryption Information
- Public Key decryption
- $UNSIGNMSG =base64_decode ($SIGNMSG);//base64 Decoding encrypted information
- Openssl_pkcs12_read ($priv _key, $certs, $privkeypass); Read public key, private key
- $pubkeyid = $certs [' cert ']; Public
- $res = Openssl_verify ($data, $UNSIGNMSG, $pubkeyid); Verify
- Echo $res; Output verification result, 1: Validation succeeded, 0: validation failed
- ?>
Copy Code
|