openssl擴展

<?php

    ini_set('display_errors', 1);

    $config = array(
        "config" => 'D:\wamp\bin\php\php5.5.12\extras\ssl\openssl.cnf',
        "private_key_bits" => 2048,
        "private_key_type" => OPENSSL_KEYTYPE_RSA,
    );

    // Create the private and public key
    $res = openssl_pkey_new($config);

    if ($res === false) die('Failed to generate key pair.'."\n"); 

    if (!openssl_pkey_export($res, $privKey, "phrase", $config)) die('Failed to retrieve private key.'."\n"); 

    // Extract the private key from $res to $privKey
    openssl_pkey_export($res, $privKey, "phrase", $config);

    echo "<br/>";
    echo "Private Key = ".$privKey;
    echo "<br/>";

    // Extract the public key from $res to $pubKey
    $pubKey = openssl_pkey_get_details($res);
    $pubKey = $pubKey["key"];

    echo "<br/>";
    echo "Public Key = ".$pubKey;
    echo "<br/>";

    $data = 'plaintext data goes here';

    // Encrypt the data to $encrypted using the public key
    openssl_public_encrypt($data, $encrypted, $pubKey);
    echo "<br/>";
    echo "Encrypted Data = ".$encrypted;
    echo "<br/>";

    // Decrypt the data using the private key and store the results in $decrypted
  
    openssl_private_decrypt($encrypted, $decrypted, openssl_pkey_get_private($privKey, "phrase"));

    echo "<br/>";
    echo "Decrypted Data = ".$decrypted;
    echo "<br/>";
?>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章