<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>ECDH - tag - 沐木</title><link>https://oldletter.cn/tags/ecdh/</link><description>ECDH - 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/ecdh/" rel="self" type="application/rss+xml"/><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></channel></rss>