Question

Using the PHPECC package from MDanter, how can I generate public/private key pairs and encrypt a message?

I found this library here: https://github.com/mdanter/phpecc But no tutorial or explaination is provided.

I tried the following, which works but I only have public keys, I don't know where to get private keys and how to change the key length.

$g = NISTcurve::generator_192();

$Alice = new EcDH($g);
$Bob = new EcDH($g);

//Alice and bob generate their private keys and public Point
$pubPointA = $Alice->getPublicPoint();

$pubPointB = $Bob->getPublicPoint();

//Alice sends Bob her public key and vice versa
$Alice->setPublicPoint($pubPointB);

$Bob->setPublicPoint($pubPointA);

//key_A == key_B
$key_A = $Alice->calculateKey();

$key_B = $Bob->calculateKey();

//String to encrypt
$str='My test msg.';

echo 'encoding '.$str;
//Alice encrypt the string
$Ae = $Alice->encrypt($str);
echo $Ae;

echo '<hr>';
//Bob receive the string and decrypt it
$Bd = $Bob->decrypt($Ae);
echo 'Bob decrypt '.$Bd;

Any help is appreciated, thank you

Était-ce utile?

La solution

The code sample you posted is for Diffie-Hellman, which is a key-agreement protocol for deriving keys for symmetric encryption. This will not let you generate public/private keys.

the package you posted also provides ECDSA, which is a signing algorithm. It can't really be used for encryption.

for asymmetric encryption, you pretty much want to use RSA. If you want to use something with Elliptic Curve Cryptography, you can try to find an implementation of the ElGamal-EC algorithm, but I don't really know of one for PHP.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top