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