php rsa encryption code example

Example 1: php rsa encryption

// Use phpseclib: http://phpseclib.sourceforge.net/
<?php
include('Crypt/RSA.php');

$privatekey = file_get_contents('private.key');
$rsa = new Crypt_RSA();
$rsa->loadKey($privatekey);

$plaintext = new Math_BigInteger('aaaaaa');
echo $rsa->_exponentiate($plaintext)->toBytes();
?>

Example 2: php RSA encryption

<?php

$publicKey = file_get_contents("public.key");
$textToEncrypt = "My Plain Text" ;
$cipherType = "RSA/ECB/PKCS1Padding"; //RSA, RSA/ECB/OAEPWithSHA-1AndMGF1Padding


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://www.devglan.com/online-tools/rsa-encrypt',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "textToEncrypt": "'.$textToEncrypt.'",
    "publicKey": "'.$publicKey.'",
    "keyType": "publicKeyForEncryption",
    "cipherType": "'.$cipherType.'"
}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

?>

Tags:

Php Example