note 126296 added to function.openssl-encrypt
| From: | TheNorthMemory at main dot php dot net | Date: | Sat, 31 Jul 2021 07:52:19 +0000 |
| Subject: | note 126296 added to function.openssl-encrypt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216697@lists.php.net to get a copy of this message | ||
I saw that a doc bug(#80236) were there mentioned that $tag usage. Here is an examples, Hopes those
may help someone.
<?php
/**
* Encrypts given data with given key, iv and aad, returns a base64 encoded string.
*
* @param string $plaintext - Text to encode.
* @param string $key - The secret key, 32 bytes string.
* @param string $iv - The initialization vector, 16 bytes string.
* @param string $aad - The additional authenticated data, maybe empty string.
*
* @return string - The base64-encoded ciphertext.
*/
function encrypt(string $plaintext, string $key, string $iv = '', string $aad =
''): string
{
$ciphertext = openssl_encrypt($plaintext, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv,
$tag, $aad, 16);
if (false === $ciphertext) {
throw new UnexpectedValueException('Encrypting the input $plaintext failed, please
checking your $key and $iv whether or nor correct.');
}
return base64_encode($ciphertext . $tag);
}
/**
* Takes a base64 encoded string and decrypts it using a given key, iv and aad.
*
* @param string $ciphertext - The base64-encoded ciphertext.
* @param string $key - The secret key, 32 bytes string.
* @param string $iv - The initialization vector, 16 bytes string.
* @param string $aad - The additional authenticated data, maybe empty string.
*
* @return string - The utf-8 plaintext.
*/
function decrypt(string $ciphertext, string $key, string $iv = '', string $aad =
''): string
{
$ciphertext = base64_decode($ciphertext);
$authTag = substr($ciphertext, -16);
$tagLength = strlen($authTag);
/* Manually checking the length of the tag, because the
openssl_decrypt was
mentioned there, it's the caller's responsibility. */
if ($tagLength > 16 || ($tagLength < 12 && $tagLength !== 8 && $tagLength
!== 4)) {
throw new RuntimeException('The inputs $ciphertext incomplete, the bytes
length must be one of 16, 15, 14, 13, 12, 8 or 4.');
}
$plaintext = openssl_decrypt(substr($ciphertext, 0, -16), 'aes-256-gcm', $key,
OPENSSL_RAW_DATA, $iv, $authTag, $aad);
if (false === $plaintext) {
throw new UnexpectedValueException('Decrypting the input $ciphertext failed, please
checking your $key and $iv whether or nor correct.');
}
return $plaintext;
}
// usage samples
$aesKey = random_bytes(32);
$aesIv = random_bytes(16);
$ciphertext = encrypt('thing', $aesKey, $aesIv);
$plaintext = decrypt($ciphertext, $aesKey, $aesIv);
var_dump($ciphertext);
var_dump($plaintext);
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 89.238.132.78)
----
Manual Page -- https://php.net/manual/en/function.openssl-encrypt.php
Edit -- https://main.php.net/note/edit/126296
Del: integrated -- https://main.php.net/note/delete/126296/integrated
Del: useless -- https://main.php.net/note/delete/126296/useless
Del: bad code -- https://main.php.net/note/delete/126296/bad+code
Del: spam -- https://main.php.net/note/delete/126296/spam
Del: non-english -- https://main.php.net/note/delete/126296/non-english
Del: in docs -- https://main.php.net/note/delete/126296/in+docs
Del: other reasons-- https://main.php.net/note/delete/126296
Reject -- https://main.php.net/note/reject/126296
Search -- https://main.php.net/manage/user-notes.php