olá, queria saber como faço para usar o openssl-encrypt separado do openssl-decrypt.
em varios lugares da internet ja procurei e todos vem com o mesmo exemplo:
$key = “123”;
$plaintext = "é uma mensagem secreta";
$cipher = "aes-128-gcm";
if (in_array($cipher, openssl_get_cipher_methods()))
{
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv, $tag);
//store $cipher, $iv, and $tag for decryption later
$original_plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv, $tag);
}
echo "DESCRIPTOGRAFADO: ".$original_plaintext."\n";
echo "CRIPTOGRAFADO: ".$ciphertext."\n";
mas eu preciso criptografar um texto em um arquivo salvar ele em algum lugar e descriptografar depois em outro arquivo, tentei modificar o exemplo a cima assim:
function encrypt($plaintext)
{
$key = '123';
$cipher = "aes-128-gcm";
if (in_array($cipher, openssl_get_cipher_methods())) {
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options = 0, $iv, $tag);
return $ciphertext;
}
}
function decrypt($ciphertext)
{
$key = '123';
$cipher = "aes-128-gcm";
if (in_array($cipher, openssl_get_cipher_methods())) {
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
$original_plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv, $tag);
return $original_plaintext;
}
}
$crypt = base64_encode(encrypt("Mensagem secreta"));
echo "CRIPTOGRAFADO: ".$crypt;
echo "\nDESCRIPTOGRAFADO: ".decrypt(base64_decode($crypt));
mas não funcionou alguem sabe como faz?