简体   繁体   中英

RSA encryption and decryption

im doing an assignment for school and we have to use rsa to crypt and sending data (for example password) to the server for user authentication i'm using php class for thet phpseclib, a pure PHP RSA implementation !

i can encrypt text

<?php
include('Math/BigInteger.php');
include('Crypt/Hash.php');
include('Crypt/RSA.php');
ini_set('max_execution_time', 0);

$rsa = new Crypt_RSA();
extract($rsa->createKey());

$plaintext = 'terrafrost';

$rsa->loadKey($privatekey);
$ciphertext = $rsa->encrypt($plaintext);


echo"<form action=\"dec.php\" method=\"post\"><input name=\"ciphertext\" type=\"text\"  value=\"".$ciphertext."\"/>";

echo"<input name=\"publickey\" type=\"text\"  value=\"".$publickey."\"/>";
echo"<input type=\"submit\" name=\"button\" id=\"button\" value=\"Submit\" /></form>";


?>

but when i send the encrypted code to another page and try to decrypt it i get a errors

Warning: unpack() [function.unpack]: Type N: not enough input, need 4, have 3 in C:\wamp\www\RSA\Crypt\RSA.php on line 972

Warning: extract() expects parameter 1 to be array, boolean given in C:\wamp\www\RSA\Crypt\RSA.php on line 972

Notice: Undefined variable: length in C:\wamp\www\RSA\Crypt\RSA.php on line 973

Warning: unpack() [function.unpack]: Type N: not enough input, need 4, have 0 in C:\wamp\www\RSA\Crypt\RSA.php on line 974

Warning: extract() expects parameter 1 to be array, boolean given in C:\wamp\www\RSA\Crypt\RSA.php on line 974

Notice: Undefined variable: length in C:\wamp\www\RSA\Crypt\RSA.php on line 975

here is my decryption page:

    <?php
    include('Math/BigInteger.php');
    include('Crypt/Hash.php');
    include('Crypt/RSA.php');
    ini_set('max_execution_time', 0);

    $rsa = new Crypt_RSA();

    $publickey=$_POST['publickey'];
    $ciphertext = $_POST['ciphertext'];

    $rsa->loadKey($publickey);
    echo $rsa->decrypt($ciphertext);

?>

am i missing something?

  1. Encrypt with the public key, decrypt with the private key
  2. Base64 encode the cipher before posting it, base64 decode it before decrypting it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM