PHP generates certificates, keys, and encrypts decrypted data through OpenSSL

Source: Internet
Author: User

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

  1. $DN = Array (
  2. "CountryName" = ' XX ',//country name
  3. "Stateorprovincename" = ' state ',//province name
  4. "Localityname" = ' somewherecity ',//city name
  5. "OrganizationName" = ' myself ',//registrant Name
  6. "Organizationalunitname" = ' Whatever ',//Organization name
  7. "CommonName" = ' myself ',//Public name
  8. "EmailAddress" = ' user@domain.com '//Email
  9. );
  10. $privkeypass = ' 111111 '; Private Key Password
  11. $numberofdays = 365; Effective duration
  12. $cerpath = "./test.cer"; Generate Certificate Path
  13. $pfxpath = "./test.pfx"; Key file path
  14. Generate certificate
  15. $privkey = Openssl_pkey_new ();
  16. $CSR = Openssl_csr_new ($dn, $privkey);
  17. $sscert = openssl_csr_sign ($CSR, NULL, $privkey, $numberofdays);
  18. Openssl_x509_export ($sscert, $csrkey); Export Certificate $csrkey
  19. Openssl_pkcs12_export ($sscert, $privatekey, $privkey, $privkeypass); Export Key $privatekey
  20. Generate a certificate file
  21. $fp = fopen ($cerpath, "w");
  22. Fwrite ($fp, $csrkey);
  23. Fclose ($FP);
  24. Generate Key File
  25. $fp = fopen ($pfxpath, "w");
  26. Fwrite ($fp, $privatekey);
  27. Fclose ($FP);
  28. ?>
Copy Code
crypt.php
    1. $privkeypass = ' 111111 '; Private Key Password
    2. $pfxpath = "./test.pfx"; Key file path
    3. $priv _key = file_get_contents ($pfxpath); Get key File contents
    4. $data = "Test"; Encrypted data test
    5. Private key encryption
    6. Openssl_pkcs12_read ($priv _key, $certs, $privkeypass); Read public key, private key
    7. $prikeyid = $certs [' Pkey ']; Private
    8. Openssl_sign ($data, $SIGNMSG, $prikeyid, OPENSSL_ALGO_SHA1); Registering to generate encrypted information
    9. $SIGNMSG = Base64_encode ($SIGNMSG); Base64 transcoding Encryption Information
    10. Public Key decryption
    11. $UNSIGNMSG =base64_decode ($SIGNMSG);//base64 Decoding encrypted information
    12. Openssl_pkcs12_read ($priv _key, $certs, $privkeypass); Read public key, private key
    13. $pubkeyid = $certs [' cert ']; Public
    14. $res = Openssl_verify ($data, $UNSIGNMSG, $pubkeyid); Verify
    15. Echo $res; Output verification result, 1: Validation succeeded, 0: validation failed
    16. ?>
Copy Code


Encrypted decryption, PHP, OpenSSL
  • 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.