# 一次性密码

一次性密码（OTP）是由认证器生成、仅使用一次以证明持有该认证器的秘密代码，在登录系统时用于替代或补充可重复使用的密码<sup>[1](https://pages.nist.gov/800-63-4/sp800-63b.html?trk=public_post_comment-text)</sup>。由于每个代码仅对单次认证有效，窃听者截获的代码无法在之后重放，这一特性将一次性密码与普通密码区分开来——普通密码在每次登录时都会提供相同的认证器输出<sup>[1](https://pages.nist.gov/800-63-4/sp800-63b.html?trk=public_post_comment-text)</sup>。一次性密码广泛用作多因素认证中的第二因素，由硬件令牌、智能手机应用生成，或通过短信等独立渠道传递<sup>[2](https://cheatsheetseries.owasp.org/cheatsheets/Multifactor_Authentication_Cheat_Sheet.html)</sup>。

| 关键事实 | 详情 |
|---|---|
| 定义 | 由认证器生成、仅使用一次以证明持有该认证器的秘密<sup>[1](https://pages.nist.gov/800-63-4/sp800-63b.html?trk=public_post_comment-text)</sup> |
| HOTP（RFC 4226，2005） | \( \mathrm{HOTP}(K,C) = \mathrm{Truncate}(\mathrm{HMAC\text{-}SHA\text{-}1}(K,C)) \)，以8字节计数器作为移动因子；共享秘密至少128位，建议160位；值至少6位数字<sup>[3](https://www.rfc-editor.org/info/rfc4226/)</sup> |
| TOTP（RFC 6238，2011） | 以时间因子\( T = (\text{Unix时间} - T_0)/X \)替代计数器；建议默认时间步长\( X = 30 \)秒<sup>[4](https://www.rfc-editor.org/info/rfc6238/)</sup> |
| 代码长度 | NIST允许截断至最少六位十进制数字，约20位熵；6位代码约有100万种可能<sup>[1](https://pages.nist.gov/800-63-4/sp800-63b.html?trk=public_post_comment-text)</sup><sup> • </sup><sup>[2](https://cheatsheetseries.owasp.org/cheatsheets/Multifactor_Authentication_Cheat_Sheet.html)</sup> |
| 抗钓鱼性 | 一次性密码认证器不抗钓鱼，因为手动输入不会将代码绑定到正在认证的特定会话<sup>[1](https://pages.nist.gov/800-63-4/sp800-63b.html?trk=public_post_comment-text)</sup> |
| 重放控制 | 验证方在有效期内必须只接受给定的一次性密码一次；基于时钟的随机数必须至少每两分钟变化一次<sup>[1](https://pages.nist.gov/800-63-4/sp800-63b.html?trk=public_post_comment-text)</sup> |
| 早期系统 | S/KEY（RFC 1760，1995年2月）通过反复哈希生成64位一次性密码，编码为六个简短英文单词<sup>[5](https://www.rfc-editor.org/info/rfc1760/)</sup> |

## 一次性密码的工作原理

一次性密码系统有两方。认证器——硬件令牌或软件应用——根据秘密密钥和一个每次使用都会变化的值（称为移动因子）计算代码。验证方重复相同的计算并比较结果。两种常见设计的不同之处在于以什么作为移动因子。

**基于计数器：HOTP。** 基于HMAC的一次性密码算法于2005年12月作为RFC 4226发布，计算\( \mathrm{HOTP}(K,C) = \mathrm{Truncate}(\mathrm{HMAC\text{-}SHA\text{-}1}(K,C)) \)，其中\( K \)是仅令牌和验证服务知道的静态对称密钥，\( C \)是必须在生成器和验证器之间保持同步的8字节计数器<sup>[3](https://www.rfc-editor.org/info/rfc4226/)</sup>。由于HMAC-SHA-1输出为160位，需截断为用户可输入的值：截断提取一个31位二进制数，再对\( 10^{\mathrm{Digit}} \)取模<sup>[3](https://www.rfc-editor.org/info/rfc4226/)</sup>。共享秘密必须至少128位，建议160位，显示值必须至少6位数字<sup>[3](https://www.rfc-editor.org/info/rfc4226/)</sup>。如果客户端和服务器计数器出现偏差——例如用户生成了代码但未提交——服务器会在\( s \)个连续计数器值的前瞻窗口内尝试收到的值以重新同步<sup>[3](https://www.rfc-editor.org/info/rfc4226/)</sup>。

**基于时间：TOTP。** 基于时间的一次性密码算法于2011年5月作为RFC 6238发布，通过以时间因子替代计数器来扩展HOTP：\( \mathrm{TOTP} = \mathrm{HOTP}(K, T) \)，其中\( T = (\text{当前Unix时间} - T_0)/X \)，使用向下取整函数<sup>[4](https://www.rfc-editor.org/info/rfc6238/)</sup>。默认时间步长\( X \)为30秒，是在安全性和可用性之间权衡选择的，\( T_0 \)默认为Unix纪元<sup>[4](https://www.rfc-editor.org/info/rfc6238/)</sup>。实现可使用HMAC-SHA-256或HMAC-SHA-512替代HMAC-SHA-1<sup>[4](https://www.rfc-editor.org/info/rfc6238/)</sup>。时间步长越大，暴露的代码被攻击者利用的窗口就越宽，因此标准建议最多接受一个时间步长的网络延迟，且验证方在成功验证后不得接受同一代码的第二次提交<sup>[4](https://www.rfc-editor.org/info/rfc6238/)</sup>。基于时间的令牌带有内部时钟，尽管存在温度驱动的漂移，在数年寿命内必须与验证方大致保持同步，因此验证方在决定是否接受某个值时必须考虑时钟漂移<sup>[6](https://pages.nist.gov/800-63-3-Implementation-Resources/63B/Authenticators/)</sup>。

## 早期一次性密码系统

S/KEY一次性密码系统于1995年2月在RFC 1760中描述，由贝尔通信研究所公开发布，用于对抗窃听攻击——即截获的登录名和密码在之后被重放<sup>[5](https://www.rfc-editor.org/info/rfc1760/)</sup>。客户端对用户的秘密口令短语反复应用安全哈希函数，每次使用时将应用次数减一，从而生成唯一的密码序列；主机通过对收到的密码哈希一次并与先前存储的值比较来验证<sup>[5](https://www.rfc-editor.org/info/rfc1760/)</sup>。该技术最早由计算机科学家[莱斯利·兰波特](https://www.edgepedia.org/zh/leslie-lamport)（Leslie Lamport）——以分布式系统研究闻名——在1981年一篇关于不安全通信上密码认证的论文中提出<sup>[5](https://www.rfc-editor.org/info/rfc1760/)</sup>。S/KEY密码为64位长，为便于手动输入，编码为从2048词词典中选取的六个简短英文单词序列<sup>[5](https://www.rfc-editor.org/info/rfc1760/)</sup>。

## 安全属性与局限

**暴力破解与限流。** 将160位HMAC输出截断为短十进制代码使猜测在理论上可行，因此验证服务器必须检测并阻止暴力破解攻击。RFC 4226将对手的成功概率近似为\( \mathrm{Sec} = s \cdot v / 10^{\mathrm{Digit}} \)，其中\( s \)是前瞻窗口大小，\( v \)是验证尝试次数，Digit是位数；运营者调整\( s \)、限制尝试的限流参数\( T \)和Digit以达到目标安全级别<sup>[3](https://www.rfc-editor.org/info/rfc4226/)</sup>。该RFC的安全分析得出结论：对HOTP的最佳可能攻击是暴力破解，验证服务器采取的谨慎对策可以防止此类攻击<sup>[3](https://www.rfc-editor.org/info/rfc4226/)</sup>。

**钓鱼。** NIST的数字身份指南将一次性密码认证器归类为不抗钓鱼：由于代码是手动输入的，它不会绑定到正在认证的特定会话，因此冒充的验证方可以转发从声称者处截获的代码并成功认证<sup>[1](https://pages.nist.gov/800-63-4/sp800-63b.html?trk=public_post_comment-text)</sup>。抗钓鱼反而需要密码学认证，通过通道绑定或验证方名称绑定实现，如FIDO2通行密钥等认证器所提供的那样<sup>[1](https://pages.nist.gov/800-63-4/sp800-63b.html?trk=public_post_comment-text)</sup><sup> • </sup><sup>[7](https://pages.nist.gov/800-63-4-Implementation-Resources/authenticator/index.html)</sup>。

**存储与处理。** 一次性密码是认证秘密，应按照类似密码的安全规范处理：短生存期、单次使用、严格尝试限制、验证成功后失效、不记录日志、不长期明文存储<sup>[2](https://cheatsheetseries.owasp.org/cheatsheets/Multifactor_Authentication_Cheat_Sheet.html)</sup>。仍建议对存储的一次性密码进行哈希，但原因与密码哈希不同：6位代码只有约100万种可能，因此数据库攻击者可以快速暴力破解任何一次性密码哈希，哈希主要限制代码有效窗口内的短期暴露<sup>[2](https://cheatsheetseries.owasp.org/cheatsheets/Multifactor_Authentication_Cheat_Sheet.html)</sup>。NIST要求一次性密码验证方内部的对称密钥受到强保护，因为验证方复制了认证器的生成过程，因此持有相同的秘密<sup>[1](https://pages.nist.gov/800-63-4/sp800-63b.html?trk=public_post_comment-text)</sup>。

## 部署指南

NIST的指南将单因素一次性密码设备视为“你拥有的东西”，并允许多因素一次性密码设备在生成代码前要求激活因素，如PIN或生物特征<sup>[1](https://pages.nist.gov/800-63-4/sp800-63b.html?trk=public_post_comment-text)</sup>。在认证器保证等级3——最高等级——一次性密码设备只能与其他认证器组合出现，因为AAL3要求基于硬件的认证器和抗验证方冒充<sup>[8](https://pages.nist.gov/800-63-3/sp800-63b.html)</sup>。知名的硬件令牌包括[RSA SecurID](https://www.edgepedia.org/zh/rsa-securid)，它生成每60秒变化的六位数字<sup>[2](https://cheatsheetseries.owasp.org/cheatsheets/Multifactor_Authentication_Cheat_Sheet.html)</sup>；软件TOTP应用通过扫描包含初始种子的[二维码](https://www.edgepedia.org/zh/qr-code)来配置，是更便宜也更常见的选择<sup>[2](https://cheatsheetseries.owasp.org/cheatsheets/Multifactor_Authentication_Cheat_Sheet.html)</sup>。

CISA面向企业的指南将主要第二因素从最安全到最不安全排序：物理安全密钥第一，其次是带数字匹配的认证器应用，然后是带一次性代码的认证器应用，再是生物特征，最后是短信或电子邮件代码——它称这是人们最熟悉但保护最弱的形式<sup>[9](https://www.cisa.gov/audiences/small-and-medium-businesses/secure-your-business/require-multifactor-authentication)</sup>。OWASP同样建议优先选择抗钓鱼认证器或TOTP，而非短信或语音一次性密码<sup>[2](https://cheatsheetseries.owasp.org/cheatsheets/Multifactor_Authentication_Cheat_Sheet.html)</sup>。

## References

1. [NIST Special Publication 800-63B-4, Digital Identity Guidelines: Authentication and Authenticator Management](https://pages.nist.gov/800-63-4/sp800-63b.html?trk=public_post_comment-text)
2. [OWASP Cheat Sheet Series: Multifactor Authentication Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Multifactor_Authentication_Cheat_Sheet.html)
3. [RFC 4226: HOTP: An HMAC-Based One-Time Password Algorithm, RFC Editor, December 2005](https://www.rfc-editor.org/info/rfc4226/)
4. [RFC 6238: TOTP: Time-Based One-Time Password Algorithm, RFC Editor, May 2011](https://www.rfc-editor.org/info/rfc6238/)
5. [RFC 1760: The S/KEY One-Time Password System, RFC Editor, February 1995](https://www.rfc-editor.org/info/rfc1760/)
6. [NIST SP 800-63B Implementation Resources: Authenticators](https://pages.nist.gov/800-63-3-Implementation-Resources/63B/Authenticators/)
7. [NIST SP 800-63B-4 Implementation Resources: Authenticator Examples](https://pages.nist.gov/800-63-4-Implementation-Resources/authenticator/index.html)
8. [NIST Special Publication 800-63B (SP 800-63-3 edition)](https://pages.nist.gov/800-63-3/sp800-63b.html)
9. [CISA: Require Multifactor Authentication](https://www.cisa.gov/audiences/small-and-medium-businesses/secure-your-business/require-multifactor-authentication)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 网络与安全 › Security governance and internet policy › Cryptographic protocols › Commitment, challenge–response, and interlocking protocols

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/one-time-password

English original: https://www.edgepedia.org/one-time-password
