php 8.6用口令保护短文本时,可以先以argon2id派生密钥,再用secretbox进行认证加密,保存随机盐、nonce、密文和固定版本信息。口令不是直接作为加密密钥,解密也不能采信输入文件任意指定的高耗资源参数;下面使用固定、受限的交互级参数。

PHP手册中的sodium_crypto_pwhash
把盐、nonce和密文封装为版本化JSON
<?php function derive(string $password,string $salt): string {
return sodium_crypto_pwhash(SODIUM_CRYPTO_SECRETBOX_KEYBYTES,$password,$salt,
SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE,
SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13);
}
function protect(string $text,string $password): string {
if (strlen($text)>65536 || $password==='') throw new InvalidArgumentException('输入不符');
$salt=random_bytes(SODIUM_CRYPTO_PWHASH_SALTBYTES);
$nonce=random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$key=derive($password,$salt);
try {$cipher=sodium_crypto_secretbox($text,$nonce,$key);}
finally {sodium_memzero($key);}
return json_encode(['v'=>1,'kdf'=>'argon2id13-interactive-v1',
'salt'=>base64_encode($salt),'nonce'=>base64_encode($nonce),
'cipher'=>base64_encode($cipher)],JSON_THROW_ON_ERROR);
}
function unprotect(string $json,string $password): string {
if (strlen($json)>100000) throw new InvalidArgumentException('封装过大');
$e=json_decode($json,true,8,JSON_THROW_ON_ERROR);
if (($e['v']??null)!==1 || ($e['kdf']??null)!=='argon2id13-interactive-v1')
throw new RuntimeException('不支持的参数版本');
$salt=base64_decode($e['salt']??'',true);
$nonce=base64_decode($e['nonce']??'',true);
$cipher=base64_decode($e['cipher']??'',true);
if ($salt===false || strlen($salt)!==SODIUM_CRYPTO_PWHASH_SALTBYTES ||
$nonce===false || strlen($nonce)!==SODIUM_CRYPTO_SECRETBOX_NONCEBYTES ||
$cipher===false || strlen($cipher)<sodium_crypto_secretbox_macbytes throw new runtimeexception try finally if return><p style="text-align:center">@@##@@</p>
<p style="text-align:center">PHP手册中的sodium_crypto_secretbox</p>
<h2>固定参数版本,避免输入触发无限制开销</h2>
<p>此版本把KDF参数固定为已知方案,不从JSON读取任意内存和运算限制。部署时确认交互级参数符合机器预算,并限制并发解密次数;未来改变方案需增加版本并保留旧版解密逻辑。salt和nonce无需保密,但每次加密都应重新随机生成,不能复用同一个nonce与密钥组合。</p>
<h2>认证失败和口令管理分别处理</h2>
<p>解密失败统一提示口令错误或损坏,不返回未经认证的明文。口令不要写在源代码、日志或封装文件里;该方法用于普通短文本,不是登录密码存储方案。sodium_memzero只清理所持有的变量,不能承诺所有运行时副本都被擦除;敏感应用还要评估交换空间、备份和进程访问权限。</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20e352aa140.jpg" alt="Build MCP servers and clients for PHP applications with the official PHP SDK" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="overflowclass">Build MCP servers and clients for PHP applications with the official PHP SDK</a>
<p class="overflowclass">使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。</p>
</div>
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div></sodium_crypto_secretbox_macbytes>

php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










