sodium_crypto_secretbox_open
(PHP 7 >= 7.2.0, PHP 8)
sodium_crypto_secretbox_open - 인증된 공유 키 암호 해독
설명
sodium_crypto_secretbox_open(string $ciphertext
, string $nonce
, string $key
): string|false
대칭(공유) 키로 암호화된 메시지를 해독합니다.
매개변수
ciphertext
- sodium_crypto_secretbox()에서 제공하는 형식이어야 합니다(암호문 및 태그, 연결됨).
nonce
- 메시지당 한 번만 사용해야 하는 번호입니다. 24바이트 길이. 이것은 무작위로 생성하기에 충분히 큰 범위입니다(즉, random_bytes()).
key
- 암호화 키(256비트).
반환 값
성공 시 해독된 문자열 또는 실패 시 false
입니다.
오류/예외
-
nonce
의 길이가SODIUM_CRYPTO_SECRETBOX_NONCEBYTES
(24바이트)와 다른 경우 SodiumException이 발생합니다. -
key
의 길이가SODIUM_CRYPTO_SECRETBOX_KEYBYTES
(32바이트)와 다른 경우 SodiumException이 발생합니다.
Examples
예제 #1 sodium_crypto_secretbox_open() 예제
<?php
// The $key must be kept confidential
$key = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES);
// Do not reuse $nonce with the same key
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$ciphertext = sodium_crypto_secretbox('message to be encrypted', $nonce, $key);
// The same nouce and key are required to decrypt the $ciphertext
$plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $key);
if ($plaintext !== false) {
echo $plaintext . PHP_EOL;
}
?>
위의 예는 다음을 출력합니다.
message to be encrypted
기타
- sodium_crypto_secretbox() - 인증된 공유 키 암호화
- sodium_crypto_secretbox_keygen() - sodium_crypto_secretbox에 대한 임의 키 생성
- random_bytes() - 암호학적으로 안전한 의사 난수 바이트 생성