OpenSSL PHP example for asymmetric encryption and symmetric encryption

Source: Internet
Author: User
Tags openssl php example asymmetric encryption

Make sure the php OpenSSL extension is turned on: Extension=php_openssl.dll

<?php/**
* @file
* Author: Yunke url:http://blog.csdn.net/u011474028
*
*/
Header ("content-type:text/html; Charset=utf-8 ");

$key _file = "Yunkeserver.key"; Private
$publickey _file = "YUNKESERVER.CRT"; Certificate file
$publickey _file= "SERVER.CRT";//and the private key does not match the certificate file
$data _file = "Msg.txt"; Data files to be encrypted

$private _key = Openssl_get_privatekey (file_get_contents ($key _file)); Get private key non string type is resource type
echo "Private key is:<br>". $private _key. "<br><br>";
$public _key = Openssl_get_publickey (file_get_contents ($publickey _file)); Get public key non-string type is resource type
echo "Public key is:<br>." $public _key. "<br><br>";

/*
$str _key = '; String representation of key
if (Openssl_pkey_export ($private _key, $str _key)) {//The openssl.cnf file needs to be properly configured to succeed
echo "The string representation of the key is:<br>". $str _key. "<br><br>";
} else {
echo "string failure:<br> of key";
while ($msg = openssl_error_string ()) {
Echo $msg. "<br/>\n";
}
echo "<br/>";
}
*/
Print_r (Openssl_pkey_get_details ($public _key)); Output key details Here you can see that the private key contains the public key

$data = file_get_contents ($data _file);
echo "PlainText data is:<br>". $data. "<br><br>";

$crypted _data = null; Ciphertext
if (Openssl_private_encrypt ($data, $crypted _data, $private _key)) {
echo "The following is the secret key encrypted ciphertext:<br>". $crypted _data. "<br><br>";
} else {
echo "Cryptographic failure:<br>";
while ($msg = openssl_error_string ()) {
Echo $msg. "<br/>\n";
}
}
$decrypted _data = null; Encrypted plaintext after restore
if (Openssl_public_decrypt ($crypted _data, $decrypted _data, $public _key)) {
echo "Below is a clear:<br> after the public key has been decrypted." $decrypted _data. "<br><br>";
} else {
echo "Decryption failure:<br>";
while ($msg = openssl_error_string ()) {
Echo $msg. "<br/>\n";
}
}




Above for asymmetric encryption the following demonstrates symmetric encryption

$key = "123456"; Set a shared password
$cipher = ';//encryption algorithm
$arr =openssl_get_cipher_methods (); Gets the key and value of the array of supported cryptographic algorithms that do not have a corresponding relationship
$cipher = $arr [20];
echo "Selected encryption algorithm is: $cipher <br>\n";
$value =openssl_encrypt ($data, $cipher, $key); IV parameter Openssl_raw_data output RAW data
echo "Encrypted ciphertext is:<br>". $value. " <br><br> ";
$old _data=openssl_decrypt ($value, $cipher, $key);
echo "Decrypts the restored plaintext:<br>". $old _data. " <br><br> ";

Print_r ($arr);


































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.