Sal-laS
Sal-laS

Reputation: 11649

Call to undefined function openssl_public_encrypt() in PHP

I am using WAMP 2.4.4. To encrypt a String I used the "MyEncryption" class, but the error is:

Call to undefined function openssl_public_encrypt()

Is there any special library that i must add before using openssl_public_encrypt()

class MyEncryption
{

    public $pubkey = '...public key here...';
    public $privkey = '...private key here...';

    function encrypt($data)
    {
        if (openssl_public_encrypt($data, $encrypted, $this->pubkey))
            $data = base64_encode($encrypted);
        else
            throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?');

        return $data;
    }

}

$url = new MyEncryption();
$d   = "Hello World";   
$enc = $url->encrypt($d);

echo "Encryption is: ", $enc;

Upvotes: 0

Views: 9045

Answers (4)

Vikrum
Vikrum

Reputation: 1

To use the openssl_ functions, you have to

a) have OpenSSL installed, and
b) build PHP with OpenSSL support.

See the PHP OpenSSL docs.

answer taken from Why can't I use openssl_encrypt?

Upvotes: -1

Kiran
Kiran

Reputation: 1197

Please enable the php_openssl extension in the WAMP -> PHP -> PHP Extensions.

Upvotes: 1

neubert
neubert

Reputation: 16792

You can use phpseclib, a pure PHP RSA implementation, instead of openssl_public_encrypt. eg.

<?php
include_once('Crypt/RSA.php'); 

$pubkey = file_get_contents('/path/to/public.key');

$rsa = new Crypt_RSA(); 
$rsa->loadKey($pubkey); 
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1); 
$ciphertext = $rsa->encrypt('ddd'); 

Upvotes: 0

hakre
hakre

Reputation: 197767

Is there any special library that i must add before using openssl_public_encrypt()?

Yes there is, it is called OpenSSL and you find it documented in the PHP manual here:

Depending on your operating system flavor the package is called php-openssl and all you need to do is to install it. Then it should be automatically available in PHP.

This is similar in the WAMP UI, which you can use to enable php_openssl as well. Only in case you're concerned for the commandline you need to do more work, see the related question:

Upvotes: 2

Related Questions