网络安全简述
网络安全概述
网络安全就是信息安全。而信息安全的核心就在于密码技术,很多高校开设的有密码学,实际上就是研究数学领域内如何保证信息安全。
Kerckhoffs原则:密码系统的安全性不应依赖于其算法的保密性,而是应该建立在密钥的保密之上。
理论上来说,密码都是可以破解的,但是不同的密码技术所需要的成本截然不同。
密码技术的常见手段有三类:
1、替换
2、换位
3、一次性填充
加密技术/算法
分类:
| 加密技术 | 例子 |
|---|---|
| 对称加密 | DES 3DES 2DEA AES DC4/5 |
| 非对称加密 | RSA SM2 |
| 哈希 | MD5 SHA SM3 |
对称加密技术/算法
简而言之就是: 加解密的密钥相同
常见有: DES 3DES AES 等等
其中值得一提的是:
AES 是可以使用硬件来实现的,因此在运算速度上具备优势,分组为128位,密钥长度(120/192/256位)。
3DES 的加密流程是 先使用Key1 对信息加密 再使用Key2 对信息解密 再使用Key1进行加密,总共有三步,但是只使用了两个密钥。
3DES加密流程详解
3DES(Triple DES)是对DES算法的增强,通过三次DES操作来提高安全性。其加密流程如下:
流程说明
-
第一步:DES加密
- 使用 Key1 对明文进行DES加密
- 这是标准的DES加密操作
-
第二步:DES解密
- 使用 Key2 对第一步的结果进行DES解密
- 注意:这里使用的是解密操作,但密钥不同
-
第三步:DES加密
- 再次使用 Key1 对第二步的结果进行DES加密
- 最终得到密文
密钥使用总结
- Key1: 用于第一步加密和第三步加密
- Key2: 用于第二步解密
- 总共使用两个密钥完成三次DES操作
安全性特点
- 有效密钥长度:112位(两个56位DES密钥)
- 抗暴力破解能力:2¹¹²次尝试
- 兼容性:可以解密使用单DES加密的数据
注意:在实际应用中,Key1和Key2应该是两个不同的密钥,如果使用相同的密钥,3DES会退化为单DES。
非对称加密技术/算法
公钥加密 + 私钥解密 = 信息保密
私钥加密 + 公钥解密 = 数字签名
公钥和私钥是一对钥匙 ,类似于电视剧中,男女主通过玉佩相识。这一对钥匙均属于一个主体,且私钥要保存妥当,原则上只有主体知道。
常见有RSA 加密 其密钥长度为 512位或者1024位。
RSA加密流程详解
RSA是目前最经典的非对称加密算法,其核心在于公钥和私钥的使用顺序决定了加密的目的。下面通过流程图和对比来说明。
RSA加密流程图(信息保密场景)
#mermaid-svg-ZfttUr8flh85rjXi{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ZfttUr8flh85rjXi .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ZfttUr8flh85rjXi .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ZfttUr8flh85rjXi .error-icon{fill:#552222;}#mermaid-svg-ZfttUr8flh85rjXi .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ZfttUr8flh85rjXi .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ZfttUr8flh85rjXi .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ZfttUr8flh85rjXi .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ZfttUr8flh85rjXi .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ZfttUr8flh85rjXi .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ZfttUr8flh85rjXi .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ZfttUr8flh85rjXi .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ZfttUr8flh85rjXi .marker.cross{stroke:#333333;}#mermaid-svg-ZfttUr8flh85rjXi svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ZfttUr8flh85rjXi p{margin:0;}#mermaid-svg-ZfttUr8flh85rjXi .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ZfttUr8flh85rjXi .cluster-label text{fill:#333;}#mermaid-svg-ZfttUr8flh85rjXi .cluster-label span{color:#333;}#mermaid-svg-ZfttUr8flh85rjXi .cluster-label span p{background-color:transparent;}#mermaid-svg-ZfttUr8flh85rjXi .label text,#mermaid-svg-ZfttUr8flh85rjXi span{fill:#333;color:#333;}#mermaid-svg-ZfttUr8flh85rjXi .node rect,#mermaid-svg-ZfttUr8flh85rjXi .node circle,#mermaid-svg-ZfttUr8flh85rjXi .node ellipse,#mermaid-svg-ZfttUr8flh85rjXi .node polygon,#mermaid-svg-ZfttUr8flh85rjXi .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ZfttUr8flh85rjXi .rough-node .label text,#mermaid-svg-ZfttUr8flh85rjXi .node .label text,#mermaid-svg-ZfttUr8flh85rjXi .image-shape .label,#mermaid-svg-ZfttUr8flh85rjXi .icon-shape .label{text-anchor:middle;}#mermaid-svg-ZfttUr8flh85rjXi .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ZfttUr8flh85rjXi .rough-node .label,#mermaid-svg-ZfttUr8flh85rjXi .node .label,#mermaid-svg-ZfttUr8flh85rjXi .image-shape .label,#mermaid-svg-ZfttUr8flh85rjXi .icon-shape .label{text-align:center;}#mermaid-svg-ZfttUr8flh85rjXi .node.clickable{cursor:pointer;}#mermaid-svg-ZfttUr8flh85rjXi .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ZfttUr8flh85rjXi .arrowheadPath{fill:#333333;}#mermaid-svg-ZfttUr8flh85rjXi .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ZfttUr8flh85rjXi .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ZfttUr8flh85rjXi .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ZfttUr8flh85rjXi .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ZfttUr8flh85rjXi .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ZfttUr8flh85rjXi .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ZfttUr8flh85rjXi .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ZfttUr8flh85rjXi .cluster text{fill:#333;}#mermaid-svg-ZfttUr8flh85rjXi .cluster span{color:#333;}#mermaid-svg-ZfttUr8flh85rjXi div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ZfttUr8flh85rjXi .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ZfttUr8flh85rjXi rect.text{fill:none;stroke-width:0;}#mermaid-svg-ZfttUr8flh85rjXi .icon-shape,#mermaid-svg-ZfttUr8flh85rjXi .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ZfttUr8flh85rjXi .icon-shape p,#mermaid-svg-ZfttUr8flh85rjXi .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ZfttUr8flh85rjXi .icon-shape .label rect,#mermaid-svg-ZfttUr8flh85rjXi .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ZfttUr8flh85rjXi .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ZfttUr8flh85rjXi .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ZfttUr8flh85rjXi :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 接收方操作
发送方操作
发送方
使用接收方的公钥加密
密文传输
接收方使用自己的私钥解密
接收方获得明文
RSA签名流程图(身份认证场景)
#mermaid-svg-hmLIU6SvqVcV8zzp{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-hmLIU6SvqVcV8zzp .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-hmLIU6SvqVcV8zzp .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-hmLIU6SvqVcV8zzp .error-icon{fill:#552222;}#mermaid-svg-hmLIU6SvqVcV8zzp .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-hmLIU6SvqVcV8zzp .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-hmLIU6SvqVcV8zzp .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-hmLIU6SvqVcV8zzp .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-hmLIU6SvqVcV8zzp .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-hmLIU6SvqVcV8zzp .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-hmLIU6SvqVcV8zzp .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-hmLIU6SvqVcV8zzp .marker{fill:#333333;stroke:#333333;}#mermaid-svg-hmLIU6SvqVcV8zzp .marker.cross{stroke:#333333;}#mermaid-svg-hmLIU6SvqVcV8zzp svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-hmLIU6SvqVcV8zzp p{margin:0;}#mermaid-svg-hmLIU6SvqVcV8zzp .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-hmLIU6SvqVcV8zzp .cluster-label text{fill:#333;}#mermaid-svg-hmLIU6SvqVcV8zzp .cluster-label span{color:#333;}#mermaid-svg-hmLIU6SvqVcV8zzp .cluster-label span p{background-color:transparent;}#mermaid-svg-hmLIU6SvqVcV8zzp .label text,#mermaid-svg-hmLIU6SvqVcV8zzp span{fill:#333;color:#333;}#mermaid-svg-hmLIU6SvqVcV8zzp .node rect,#mermaid-svg-hmLIU6SvqVcV8zzp .node circle,#mermaid-svg-hmLIU6SvqVcV8zzp .node ellipse,#mermaid-svg-hmLIU6SvqVcV8zzp .node polygon,#mermaid-svg-hmLIU6SvqVcV8zzp .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-hmLIU6SvqVcV8zzp .rough-node .label text,#mermaid-svg-hmLIU6SvqVcV8zzp .node .label text,#mermaid-svg-hmLIU6SvqVcV8zzp .image-shape .label,#mermaid-svg-hmLIU6SvqVcV8zzp .icon-shape .label{text-anchor:middle;}#mermaid-svg-hmLIU6SvqVcV8zzp .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-hmLIU6SvqVcV8zzp .rough-node .label,#mermaid-svg-hmLIU6SvqVcV8zzp .node .label,#mermaid-svg-hmLIU6SvqVcV8zzp .image-shape .label,#mermaid-svg-hmLIU6SvqVcV8zzp .icon-shape .label{text-align:center;}#mermaid-svg-hmLIU6SvqVcV8zzp .node.clickable{cursor:pointer;}#mermaid-svg-hmLIU6SvqVcV8zzp .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-hmLIU6SvqVcV8zzp .arrowheadPath{fill:#333333;}#mermaid-svg-hmLIU6SvqVcV8zzp .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-hmLIU6SvqVcV8zzp .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-hmLIU6SvqVcV8zzp .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-hmLIU6SvqVcV8zzp .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-hmLIU6SvqVcV8zzp .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-hmLIU6SvqVcV8zzp .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-hmLIU6SvqVcV8zzp .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-hmLIU6SvqVcV8zzp .cluster text{fill:#333;}#mermaid-svg-hmLIU6SvqVcV8zzp .cluster span{color:#333;}#mermaid-svg-hmLIU6SvqVcV8zzp div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-hmLIU6SvqVcV8zzp .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-hmLIU6SvqVcV8zzp rect.text{fill:none;stroke-width:0;}#mermaid-svg-hmLIU6SvqVcV8zzp .icon-shape,#mermaid-svg-hmLIU6SvqVcV8zzp .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-hmLIU6SvqVcV8zzp .icon-shape p,#mermaid-svg-hmLIU6SvqVcV8zzp .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-hmLIU6SvqVcV8zzp .icon-shape .label rect,#mermaid-svg-hmLIU6SvqVcV8zzp .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-hmLIU6SvqVcV8zzp .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-hmLIU6SvqVcV8zzp .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-hmLIU6SvqVcV8zzp :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 接收方操作
发送方操作
发送方
使用自己的私钥加密(签名)
签名+原文传输
接收方使用发送方的公钥解密(验签)
确认发送方身份
公钥私钥使用顺序对比
| 场景 | 加密方使用的密钥 | 解密方使用的密钥 | 目的 |
|---|---|---|---|
| 信息保密 | 接收方的公钥 | 接收方的私钥 | 保证只有接收方才能解密 |
| 数字签名 | 发送方的私钥 | 发送方的公钥 | 保证信息确实来自发送方 |
关键区别
-
信息保密场景:先公钥后私钥
- 发送方用接收方的公钥加密
- 接收方用自己的私钥解密
- 公钥公开,任何人都能加密,但只有持有私钥的接收方才能解密
-
数字签名场景:先私钥后公钥
- 发送方用自己的私钥加密(签名)
- 接收方用发送方的公钥解密(验签)
- 私钥只有发送方持有,因此能证明信息确实来自发送方
核心要点:公钥加密、私钥解密 用于保密通信;私钥加密、公钥解密用于身份认证。两者的使用顺序恰好相反,这正是非对称加密的精髓所在。
混合加密
既然 对称加密 加密信息具备优势,而非对称加密 在传递信息上具备优势,因此结合起来,利用*对称密钥加密信息 ,使用非对称加密加密密钥,既能加密信息又保障了密钥不易第三方获取。
哈希算法
哈希 最出名的特点就在于:
将任意长度的内容转化为固定长度的哈希值
特点
1、不可逆性 无法 通过哈希值还原 原内容
2、抗碰撞性 很难找到两个不同 的输入使得它们的哈希值相同
3、雪崩效应 原信息变动 一个比特也会使得哈希值大变
常见算法
| 算法 | 数据分组 | 哈希值长度 |
|---|---|---|
| MD5 | 512位 | 128位 |
| SHA | 512位 | 160位 |
| SM3 | 512位 | 256位 |
应用场景
1、文件校验
下载软件或大文件时,官方会同时公布文件的哈希值(如 MD5、SHA-256)。下载完成后本地计算哈希值,与官方公布值比对,一致则说明文件在传输过程中未被篡改或损坏。
2、密码存储
网站存储用户密码时,不保存明文,而是保存密码的哈希值。用户登录时,将输入的密码做同样的哈希运算,与数据库中的哈希值比对。即使数据库泄露,攻击者也无法直接得到明文密码。
3、用户身份认证(HMAC)
HMAC(Hash-based Message Authentication Code,基于哈希的消息认证码) 是哈希在身份认证中的典型应用。它结合了哈希函数 与共享密钥,既能验证消息的完整性,又能确认消息确实来自持有密钥的合法用户。
HMAC 认证流程
#mermaid-svg-2fdxnaniNJogQdHZ{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-2fdxnaniNJogQdHZ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-2fdxnaniNJogQdHZ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-2fdxnaniNJogQdHZ .error-icon{fill:#552222;}#mermaid-svg-2fdxnaniNJogQdHZ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-2fdxnaniNJogQdHZ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-2fdxnaniNJogQdHZ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-2fdxnaniNJogQdHZ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-2fdxnaniNJogQdHZ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-2fdxnaniNJogQdHZ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-2fdxnaniNJogQdHZ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-2fdxnaniNJogQdHZ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-2fdxnaniNJogQdHZ .marker.cross{stroke:#333333;}#mermaid-svg-2fdxnaniNJogQdHZ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-2fdxnaniNJogQdHZ p{margin:0;}#mermaid-svg-2fdxnaniNJogQdHZ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-2fdxnaniNJogQdHZ .cluster-label text{fill:#333;}#mermaid-svg-2fdxnaniNJogQdHZ .cluster-label span{color:#333;}#mermaid-svg-2fdxnaniNJogQdHZ .cluster-label span p{background-color:transparent;}#mermaid-svg-2fdxnaniNJogQdHZ .label text,#mermaid-svg-2fdxnaniNJogQdHZ span{fill:#333;color:#333;}#mermaid-svg-2fdxnaniNJogQdHZ .node rect,#mermaid-svg-2fdxnaniNJogQdHZ .node circle,#mermaid-svg-2fdxnaniNJogQdHZ .node ellipse,#mermaid-svg-2fdxnaniNJogQdHZ .node polygon,#mermaid-svg-2fdxnaniNJogQdHZ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-2fdxnaniNJogQdHZ .rough-node .label text,#mermaid-svg-2fdxnaniNJogQdHZ .node .label text,#mermaid-svg-2fdxnaniNJogQdHZ .image-shape .label,#mermaid-svg-2fdxnaniNJogQdHZ .icon-shape .label{text-anchor:middle;}#mermaid-svg-2fdxnaniNJogQdHZ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-2fdxnaniNJogQdHZ .rough-node .label,#mermaid-svg-2fdxnaniNJogQdHZ .node .label,#mermaid-svg-2fdxnaniNJogQdHZ .image-shape .label,#mermaid-svg-2fdxnaniNJogQdHZ .icon-shape .label{text-align:center;}#mermaid-svg-2fdxnaniNJogQdHZ .node.clickable{cursor:pointer;}#mermaid-svg-2fdxnaniNJogQdHZ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-2fdxnaniNJogQdHZ .arrowheadPath{fill:#333333;}#mermaid-svg-2fdxnaniNJogQdHZ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-2fdxnaniNJogQdHZ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-2fdxnaniNJogQdHZ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-2fdxnaniNJogQdHZ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-2fdxnaniNJogQdHZ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-2fdxnaniNJogQdHZ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-2fdxnaniNJogQdHZ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-2fdxnaniNJogQdHZ .cluster text{fill:#333;}#mermaid-svg-2fdxnaniNJogQdHZ .cluster span{color:#333;}#mermaid-svg-2fdxnaniNJogQdHZ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-2fdxnaniNJogQdHZ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-2fdxnaniNJogQdHZ rect.text{fill:none;stroke-width:0;}#mermaid-svg-2fdxnaniNJogQdHZ .icon-shape,#mermaid-svg-2fdxnaniNJogQdHZ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-2fdxnaniNJogQdHZ .icon-shape p,#mermaid-svg-2fdxnaniNJogQdHZ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-2fdxnaniNJogQdHZ .icon-shape .label rect,#mermaid-svg-2fdxnaniNJogQdHZ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-2fdxnaniNJogQdHZ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-2fdxnaniNJogQdHZ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-2fdxnaniNJogQdHZ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 一致
不一致
用户
输入账号密码
服务端生成随机挑战值 nonce
用户用共享密钥计算 HMAC(密钥, nonce)
用户将 HMAC 值发送给服务端
服务端用同一密钥计算 HMAC
比对两个 HMAC 值
认证通过
认证失败
HMAC 认证示例
以用户登录为例:
- 用户发起登录请求:用户输入账号和密码,向服务端请求登录。
- 服务端下发挑战值 :服务端生成一个随机数
nonce并发送给用户。 - 用户计算 HMAC :用户使用共享密钥 (登录时协商好的密钥)对
nonce计算 HMAC 值,连同账号一起发送给服务端。 - 服务端验证 :服务端用数据库中保存的同一共享密钥,对同一个
nonce计算 HMAC 值,与用户发来的 HMAC 值比对。 - 判定结果:两个 HMAC 值一致 → 认证通过;不一致 → 认证失败。
关键点:HMAC 的安全性同时依赖哈希函数的单向性 和共享密钥的保密性。攻击者即使截获了 HMAC 值,也无法反推出密钥;没有密钥,也无法伪造出合法的 HMAC 值。因此 HMAC 常用于 API 接口签名、Token 校验等身份认证场景。