encrypt personal data and return an encrypted form

@param PersonalData $data
@param CryptographicDetails $crypto
@throws CryptographicDetailsDoNotContainKey
@throws CryptographicDetailsNotCompatibleWithEncryption
@return EncryptedPersonalData
11175
function encrypt(PersonalData $data, CryptographicDetails $crypto): EncryptedPersonalData {

        if ( ! $crypto->encryption() == 'libsodium') {
            throw new CryptographicDetailsNotCompatibleWithEncryption("{$crypto->encryption()} received, expected 'libsodium'");
        }

        $dataString = $data->toString();
        $secretKey  = $crypto->key('secretKey');
        $nonce      = $crypto->key('nonce');

        $encrypted = sodium_crypto_secretbox($dataString, $nonce, $secretKey);

        return EncryptedPersonalData::fromString($encrypted);
    }
1332672
public static function encrypt(string $data, string $secret='', string $algorithm='AES-256-CBC')
  {
    // # If AES we will add a random 16 byte IV before the encrypted data
    // if ( strtoupper(substr($algorithm,0,3))==='AES' ) {
    //   $iv = openssl_random_pseudo_bytes(16);
    // } else {
    //   $iv = '';
    // }
    # Add a random Initialization Vector
    $iv = \openssl_random_pseudo_bytes(16);

    # If no secret provided, use the one in config
    if (empty($secret))
      $secret = \config()->get('application.secret');

    # Encrypt
    $result = \openssl_encrypt($data,$algorithm,$secret,0,$iv);

    if ( !$result===false ) {
      return \base64_encode($iv.$result);
    } else {
      return null;
    }

  }
915007
public byte[] encryptData(PublicKey key,byte[] data)
    {
    	try {
    		if(data==null || data.length<=0)
    		{
    			return null;
    		}
			Cipher cipher=Cipher.getInstance(algorithm);
			cipher.init(Cipher.ENCRYPT_MODE, key);
			return cipher.doFinal(data);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
    	return null;
    }
1229868
private function encrypt(string $data)
  {
    $secret = explode(':', config('app.key'))[1];
    $secret = base64_decode($secret);

    return (new Encrypter($secret))->encrypt($data, 9);
  }
640577
public function setData($data)
    {
        foreach ($data as $key => $dtoData) {
            $this->data[$key] = new ProfileIconDetailsDto($dtoData);
        }
    
        return $this;
    }
721823
public function encrypt($data)
    {
        $this->checkClosed();

        $data = self::convertToString($data);

        if (strlen($data) == 0) {
            throw new \InvalidArgumentException('Encryption doesn\'t support empty string');
        }

        //Encryption
        if ($this->randomInitializationVector) {
            $initializationVector = mcrypt_create_iv($this->initializationVectorSize, $this->mcryptRandomMethod);
        } else {
            $initializationVector = $this->fixedInitializationVector;
        }
        mcrypt_generic_init($this->module, $this->key, $initializationVector);
        $encryptedData = mcrypt_generic($this->module, (string) $data);
        if ($this->randomInitializationVector) {
            $encryptedData = $initializationVector . $encryptedData;
        }

        //Base64
        if ($this->base64) {
            $encryptedData = base64_encode($encryptedData);

            //Url safe
            if ($this->base64UrlSafe) {
                $encryptedData = strtr($encryptedData, self::$BASE64_URL_SAFE_REPLACE[0], self::$BASE64_URL_SAFE_REPLACE[1]);
            }

            $encryptedData = rtrim($encryptedData, '='); //Remove '=' at the end (it's useless)
        }

        return $encryptedData;
    }
905926
public static function encryptRsaPublic(string $data, string $publicKey, string $format = null): string
    {
        $key = \openssl_pkey_get_public($publicKey);

        $encrypted = '';
        if (!\openssl_public_encrypt($data, $encrypted, $key, OPENSSL_PKCS1_OAEP_PADDING)) {
            throw new CryptoException('RSA public encrypt error: ' . openssl_error_string());
        }
        \openssl_pkey_free($key);

        return self::fromRaw($encrypted, $format);
    }
62255
private static function decryptCipherText($encryptedData, $wrappedReceiptKey, $pem)
    {
        // Unwrap key and get profile
        openssl_private_decrypt(base64_decode($wrappedReceiptKey), $unwrappedKey, $pem);

        // Decipher encrypted data with unwrapped key and IV
        $cipherText = openssl_decrypt(
            $encryptedData->getCipherText(),
            'aes-256-cbc',
            $unwrappedKey,
            OPENSSL_RAW_DATA,
            $encryptedData->getIv()
        );

        return $cipherText;
    }
964641
public function decrypt($encryptedData)
    {
        $this->checkClosed();

        //Check encrypted data
        if (!is_string($encryptedData)) {
            throw new \InvalidArgumentException('Encrypted data must be a string');
        }

        //Base64
        if ($this->base64) {
            if ($this->base64UrlSafe) {
                $encryptedData = strtr($encryptedData, self::$BASE64_URL_SAFE_REPLACE[1], self::$BASE64_URL_SAFE_REPLACE[0]);
            }

            $encryptedData = base64_decode($encryptedData, true);

            if ($encryptedData === false) {
                throw new \InvalidArgumentException('Encrypted data is not a valid base64 string');
            }
        }

        //Encryption
        if ($this->randomInitializationVector) {
            $initializationVector = substr($encryptedData, 0, $this->initializationVectorSize);

            if (strlen($initializationVector) < $this->initializationVectorSize) {
                throw new \InvalidArgumentException('Encrypted data is not long enough to get the initialization vector');
            }

            $encryptedData = substr($encryptedData, $this->initializationVectorSize);
        } else {
            $initializationVector = $this->fixedInitializationVector;
        }
        mcrypt_generic_init($this->module, $this->key, $initializationVector);
        $data = mdecrypt_generic($this->module, $encryptedData);
        $data = rtrim($data, "\0");

        return $data;
    }
1320661
public function setData($data)
    {
        parent::setData($data);

        //Set the user data
        $this->getSession()->set('user', KObjectConfig::unbox($data));

        return $this;
    }
84186
public function encode($data)
    {
        return openssl_encrypt($data, $this->method, $this->key, 0, $this->iv);
    }
910024
public function encrypt($data)
    {
        if (!$this->iv || $this->autoIvUpdate) {
            $this->generateIv();
        }

        $output = openssl_encrypt($data, $this->cipher, $this->secret, OPENSSL_RAW_DATA, $this->iv);

        if ($this->formatter) {
            $output = $this->formatter->format($this->iv, $output);
        }

        return $output;
    }
1182358
public byte[] encrypt(byte[] data) {
		lock.lock();
		try {
			if (null == this.params) {
				cipher.init(Cipher.ENCRYPT_MODE, secretKey);
			} else {
				cipher.init(Cipher.ENCRYPT_MODE, secretKey, params);
			}
			return cipher.doFinal(data);
		} catch (Exception e) {
			throw new CryptoException(e);
		} finally {
			lock.unlock();
		}
	}
1064845
protected function encrypt($unencryptedData)
    {
        try {
            if ($this->encryptionKey instanceof Key) {
                return Crypto::encrypt($unencryptedData, $this->encryptionKey);
            }

            return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
        } catch (Exception $e) {
            throw new LogicException($e->getMessage(), null, $e);
        }
    }
824335
public function key($name): string {
        if ( ! isset($this->details[$name])) {
            throw new CryptographicDetailsDoNotContainKey($name);
        }
        return $this->details[$name];
    }
265924
public function get($data=null, $token=null)
    {
        if ($token) {
            $res = static::refresh($token, $data);
            if (isset($res['token'])) {
                return $res['token'];
            }
        }
        
        $data = static::formatData($data);
        
        if ($this->options['shared']) {
            $token = md5(serialize($data));
            $data['_token_'] = $token;
        } else {
            $token = Crypt::instance()->encrypt(uniqid());
        }
        $key = $token;
        
        $data['_expire_time_'] = time() + static::getExpire();
        
        static::getStorage()->set($key, $data);
        
        return $token;
    }
101338
public function encrypt($data): string
    {
        if (!$this->password)
        {
            return JSON::encode($data);
        }

        return $this->security()
            ->encrypt(JSON::encode($data));
    }