<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>SM4 - tag - 沐木</title><link>https://oldletter.cn/tags/sm4/</link><description>SM4 - tag - 沐木</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Thu, 25 May 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://oldletter.cn/tags/sm4/" rel="self" type="application/rss+xml"/><item><title>对称加密：AES 与 SM4 模式选择指南</title><link>https://oldletter.cn/posts/crypto-03-symmetric/</link><pubDate>Wed, 15 Mar 2023 00:00:00 +0000</pubDate><author>mumu</author><guid>https://oldletter.cn/posts/crypto-03-symmetric/</guid><description><![CDATA[<blockquote>
<p>作者：林 | 系列：密码学 | 适合读者：后端开发 / 安全工程师</p></blockquote>
<hr>
<h2 id="一对称加密基础">一、对称加密基础</h2>
<p>对称加密使用同一把密钥进行加密和解密。当前主流的两个分组密码算法：</p>
<table>
  <thead>
      <tr>
          <th>属性</th>
          <th>AES</th>
          <th>SM4</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>全称</td>
          <td>Advanced Encryption Standard</td>
          <td>国密分组密码算法</td>
      </tr>
      <tr>
          <td>标准</td>
          <td>NIST FIPS 197 (2001)</td>
          <td>GM/T 0002 (2012)</td>
      </tr>
      <tr>
          <td>分组长度</td>
          <td>128 bit</td>
          <td>128 bit</td>
      </tr>
      <tr>
          <td>密钥长度</td>
          <td>128 / 192 / 256 bit</td>
          <td>128 bit</td>
      </tr>
      <tr>
          <td>轮数</td>
          <td>10 / 12 / 14</td>
          <td>32</td>
      </tr>
      <tr>
          <td>结构</td>
          <td>SPN (代换-置换网络)</td>
          <td>非平衡 Feistel</td>
      </tr>
      <tr>
          <td>安全性</td>
          <td>无已知实际攻击</td>
          <td>无已知实际攻击</td>
      </tr>
  </tbody>
</table>
<p>两者安全性等价（AES-128 ≈ SM4），区别在于合规要求：信创项目用 SM4，其他场景用 AES。</p>
<hr>
<h2 id="二加密模式比算法本身更重要">二、加密模式：比算法本身更重要</h2>
<p>分组密码一次只能处理一个固定长度的数据块（16 字节）。当明文超过 16 字节时，<strong>加密模式</strong>决定了如何处理多个分组。</p>
<p>模式选错，算法再强也白搭。</p>
<h3 id="21-ecb千万别用">2.1 ECB：千万别用</h3>
<p>ECB（Electronic Codebook）每个分组独立加密：</p>]]></description></item><item><title>数字信封与密钥协商：SM2+SM4、ECDH、TLS</title><link>https://oldletter.cn/posts/crypto-05-key-exchange/</link><pubDate>Thu, 25 May 2023 00:00:00 +0000</pubDate><author>mumu</author><guid>https://oldletter.cn/posts/crypto-05-key-exchange/</guid><description><![CDATA[<blockquote>
<p>作者：林 | 系列：密码学 | 适合读者：后端开发 / 安全工程师</p></blockquote>
<hr>
<h2 id="一问题对称密钥怎么安全传递">一、问题：对称密钥怎么安全传递</h2>
<p>对称加密速度快，但面临一个核心问题：通信双方怎么共享同一把密钥？</p>
<table>
  <thead>
      <tr>
          <th>方案</th>
          <th>问题</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>线下交换</td>
          <td>不可能每次通信都见面</td>
      </tr>
      <tr>
          <td>明文传输密钥</td>
          <td>等于没加密</td>
      </tr>
      <tr>
          <td>预共享密钥</td>
          <td>不灵活，N 方通信需要 N×(N-1)/2 个密钥</td>
      </tr>
  </tbody>
</table>
<p>解决方案有两种：</p>
<ul>
<li><strong>数字信封</strong>：用非对称加密传递对称密钥</li>
<li><strong>密钥协商</strong>：双方各出一部分，协商出共享密钥（不传输密钥本身）</li>
</ul>
<hr>
<h2 id="二数字信封">二、数字信封</h2>
<h3 id="21-原理">2.1 原理</h3>
<div class="code-block code-line-numbers open" style="counter-reset: code-block 0">
    <div class="code-header language-text">
        <span class="code-title"><i class="arrow fas fa-angle-right" aria-hidden="true"></i></span>
        <span class="ellipses"><i class="fas fa-ellipsis-h" aria-hidden="true"></i></span>
        <span class="copy" title=""><i class="far fa-copy" aria-hidden="true"></i></span>
    </div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">发送方                                   接收方
</span></span><span class="line"><span class="cl">  │                                       │
</span></span><span class="line"><span class="cl">  │ 1. 随机生成对称密钥 K                   │
</span></span><span class="line"><span class="cl">  │ 2. 用 K 加密业务数据：C = Enc(K, Data) │
</span></span><span class="line"><span class="cl">  │ 3. 用接收方公钥加密 K：EK = Enc(PubB, K)│
</span></span><span class="line"><span class="cl">  │ 4. 发送 (EK, C) ─────────────────────→│
</span></span><span class="line"><span class="cl">  │                                       │
</span></span><span class="line"><span class="cl">  │                 5. 用私钥解密：K = Dec(PriB, EK)
</span></span><span class="line"><span class="cl">  │                 6. 用 K 解密数据：Data = Dec(K, C)
</span></span><span class="line"><span class="cl">  │                                       │</span></span></code></pre></div></div>
<p>好处：</p>]]></description></item><item><title>国密实战（三）：SM2+SM4 数字信封完整实现（对接国企实战）</title><link>https://oldletter.cn/posts/guomi-03-sm2-sm4-envelope/</link><pubDate>Thu, 15 Dec 2022 00:00:00 +0000</pubDate><author>mumu</author><guid>https://oldletter.cn/posts/guomi-03-sm2-sm4-envelope/</guid><description><![CDATA[<blockquote>
<p>作者：林 | 系列：国密实战 | 适合读者：Java 后端/架构师</p></blockquote>
<hr>
<h2 id="一背景与问题">一、背景与问题</h2>
<p>在国企、央企、政府项目的接口对接中，数据传输的安全性要求通常采用<strong>数字信封</strong>（Digital Envelope）机制：使用非对称算法加密对称密钥，再用对称密钥加密业务数据。这既保证了密钥传输的安全性，又兼顾了大数据量加密的性能。</p>
<p>在国密体系中，这个组合就是 <strong>SM2 + SM4</strong>：</p>
<ul>
<li><strong>SM4</strong>：对称加密算法，负责加密业务数据（高效）</li>
<li><strong>SM2</strong>：非对称加密算法，负责加密 SM4 密钥（安全）</li>
</ul>
<h3 id="11-为什么需要数字信封">1.1 为什么需要数字信封？</h3>
<table>
  <thead>
      <tr>
          <th>方案</th>
          <th style="text-align: center">安全性</th>
          <th style="text-align: center">性能</th>
          <th>适用场景</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>直接 SM2 加密数据</td>
          <td style="text-align: center">✅ 高</td>
          <td style="text-align: center">❌ 差（数据量受限）</td>
          <td>小数据（如密钥）</td>
      </tr>
      <tr>
          <td>直接 SM4 加密数据</td>
          <td style="text-align: center">❌ 密钥如何安全传输？</td>
          <td style="text-align: center">✅ 好</td>
          <td>需要解决密钥分发问题</td>
      </tr>
      <tr>
          <td><strong>SM2+SM4 数字信封</strong></td>
          <td style="text-align: center">✅ 高</td>
          <td style="text-align: center">✅ 好</td>
          <td><strong>大数据量 + 高安全性</strong></td>
      </tr>
  </tbody>
</table>
<p>SM2 加密的数据长度受限，上限明文长度约为 <strong>256 字节</strong>（受椭圆曲线参数限制）。不适合直接加密业务数据。而 SM4 虽然性能好，但密钥需要安全传输。数字信封完美结合了两者的优势。</p>
<p>SM2 加密数据长度计算：密文长度 = 65（C1，临时公钥点）+ 32（C3，SM3 哈希）+ n = 97 + n bytes。其中 n 为明文长度。加密 16 字节 SM4 密钥时，密文为 97 + 16 = 113 字节。</p>]]></description></item></channel></rss>