PHP uses OpenSSL for encrypted decryption

Source: Internet
Author: User
Tags openssl rsa

The Linux command generates the public and private keys directly using the OpenSSL command, as described in the OpenSSL command
# Generate 1024 bits RSA private key, encrypt it with 3DES, password for 123,# output to file rsa_private_key.pem# OpenSSL genrsa-out rsa_private_key.pem# from File Rsa_private_ KEY.PEM read private key # generated public key output to file rsa_public_key.pem# OpenSSL rsa-in rsa_private_key.pem-out rsa_public_key.pem# RSAPUBLICKEY.PEM encrypted file with public key data.txt,# output to file cipher.txt# OpenSSL Rsautl-encrypt-pubin-inkey rsa_public_key.pem-in Data.txt-out cipher.txt# using private key RSA_PRIVATE_KEY.PEM decryption cipher.txt,# output to file data.txt# OpenSSL Rsautl-decrypt-inkey Rsa_ Private_key.pem-in cipher.txt-out data.txt# with private key RSAPRIVATEKEY.PEM to file Plain.txt signature, # Output to file signature.bin# OpenSSL rsautl -sign-inkey rsa_private_key.pem-in data.txt-out signature.bin# authentication signature RSA_PUBLIC_KEY.PEM with male key signature.bin,# Output to file plain.txt# OpenSSL rsautl-verify-pubin-inkey rsa_public_key.pem-in signature.bin-out data

Generate public and private key files

# OpenSSL genrsa-out rsa_private_key.pem# OpenSSL rsa-in rsa_private_key.pem-pubout-out Rsa_public_key.pem

Use PHP to read public and private keys to encrypt and decrypt data

<?php$pub_file = file_get_contents (' Rsa_public_key.pem '); Echo reads the public key file \ n: $pub _file\n ";    $pub _key = Openssl_get_publickey ($pub _file);    Var_dump ($pub _key);        $encrypt _result = Openssl_public_encrypt (' Yangxunwu ', $encrypted, $pub _key);        if ($encrypt _result) {        echo "\ nthe encrypted data successfully \ n". Json_encode ($encrypted). " \ n ";        } Else{die        ("\ nthe encrypted data failed". Openssl_error_string (). " \ n ");        }        $pri _file = file_get_contents (' Rsa_private_key.pem ');        echo "Read private key file \n$pri_file\n";        $pri _key = Openssl_get_privatekey ($pri _file);        Var_dump ($pri _key);                $decrypt _result = Openssl_private_decrypt ($encrypted, $decrypted, $pri _key);        if ($decrypt _result) {        echo "\ n decrypt data successfully \ n". $decrypted. " \ n ";        } Else{die        ("\ nthe decryption data failed". Openssl_error_string (). " \ n ");                }

Run:

Http://man.linuxde.net/openssl

PHP uses OpenSSL for encrypted decryption

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.