Search code examples
laravelencryptionaesrsa

laravel encrypt the AES key with RSA public Key


i have generated a custom AES key,i need to encrypt this AES key with RSA public key.

How can we do this in Laravel

Any suggestion will be appreciated.

$aesKey = base64_encode(Encrypter::generateKey('AES-128-CBC'));

Solution

  • i found one solution and seems its working

    install phpseclib via composer

    composer require phpseclib/phpseclib
    use phpseclib\Crypt\RSA as Crypt_RSA;
    
    $rsa = new Crypt_RSA();
    $rsa->loadKey($publickey);
    $rsa->setEncryptionMode(2);
    $data = 'Your String';
    $output = $rsa->encrypt($data);
    echo base64_encode($output);