Sophie

Sophie

distrib > Mageia > 6 > armv5tl > by-pkgid > 2d8a45edbd96402b289f55f81ca7bead > files > 78

botan-doc-1.10.17-1.mga6.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Public Key Cryptography &#8212; Botan</title>
    
    <link rel="stylesheet" href="_static/agogo.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '1.10.17',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="top" title="Botan" href="contents.html" />
    <link rel="next" title="Certificate Handling" href="x509.html" />
    <link rel="prev" title="Information Flow: Pipes and Filters" href="filters.html" /> 
  </head>
  <body role="document">
    <div class="header-wrapper">
      <div class="header">
        <h1>Botan</h1>
      </div>
    </div>

    <div class="content-wrapper">
      <div class="content">
        <div class="document">
            
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="public-key-cryptography">
<h1>Public Key Cryptography<a class="headerlink" href="#public-key-cryptography" title="Permalink to this headline">¶</a></h1>
<p>Public key cryptography (also called assymmetric cryptography) is a collection
of techniques allowing for encryption, signatures, and key agreement.</p>
<div class="section" id="key-objects">
<h2>Key Objects<a class="headerlink" href="#key-objects" title="Permalink to this headline">¶</a></h2>
<p>Public and private keys are represented by classes <code class="docutils literal"><span class="pre">Public_Key</span></code> and it&#8217;s
subclass <code class="docutils literal"><span class="pre">Private_Key</span></code>. The use of inheritence here means that a
<code class="docutils literal"><span class="pre">Private_Key</span></code> can be converted into a reference to a public key.</p>
<p>None of the functions on <code class="docutils literal"><span class="pre">Public_Key</span></code> and <code class="docutils literal"><span class="pre">Private_Key</span></code> itself are
particularly useful for users of the library, because &#8216;bare&#8217; public key
operations are <em>very insecure</em>. The only purpose of these functions is to
provide a clean interface that higher level operations can be built on. So
really the only thing you need to know is that when a function takes a
reference to a <code class="docutils literal"><span class="pre">Public_Key</span></code>, it can take any public key or private key, and
similiarly for <code class="docutils literal"><span class="pre">Private_Key</span></code>.</p>
<p>Types of <code class="docutils literal"><span class="pre">Public_Key</span></code> include <code class="docutils literal"><span class="pre">RSA_PublicKey</span></code>, <code class="docutils literal"><span class="pre">DSA_PublicKey</span></code>,
<code class="docutils literal"><span class="pre">ECDSA_PublicKey</span></code>, <code class="docutils literal"><span class="pre">DH_PublicKey</span></code>, <code class="docutils literal"><span class="pre">ECDH_PublicKey</span></code>, <code class="docutils literal"><span class="pre">RW_PublicKey</span></code>,
<code class="docutils literal"><span class="pre">NR_PublicKey</span></code>,, and <code class="docutils literal"><span class="pre">GOST_3410_PublicKey</span></code>.  There are cooresponding
<code class="docutils literal"><span class="pre">Private_Key</span></code> classes for each of these algorithms.</p>
</div>
<div class="section" id="creating-new-private-keys">
<span id="id1"></span><h2>Creating New Private Keys<a class="headerlink" href="#creating-new-private-keys" title="Permalink to this headline">¶</a></h2>
<p>Creating a new private key requires two things: a source of random numbers
(see <a class="reference internal" href="rng.html#random-number-generators"><span class="std std-ref">Random Number Generators</span></a>) and some algorithm specific parameters
that define the <em>security level</em> of the resulting key. For instance, the
security level of an RSA key is (at least in part) defined by the length of
the public key modulus in bits. So to create a new RSA private key, you would
call</p>
<dl class="function">
<dt id="_CPPv2N14RSA_PrivateKey14RSA_PrivateKeyER21RandomNumberGenerator6size_t">
<span id="RSA_PrivateKey::RSA_PrivateKey__RandomNumberGeneratorR.s"></span><code class="descclassname">RSA_PrivateKey::</code><code class="descname">RSA_PrivateKey</code><span class="sig-paren">(</span>RandomNumberGenerator &amp;<em>rng</em>, size_t <em>bits</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N14RSA_PrivateKey14RSA_PrivateKeyER21RandomNumberGenerator6size_t" title="Permalink to this definition">¶</a></dt>
<dd><p>A constructor that creates a new random RSA private key with a modulus
of length <em>bits</em>.</p>
</dd></dl>

<p>Algorithms based on the discrete-logarithm problem uses what is called a
<em>group</em>; a group can safely be used with many keys, and for some operations,
like key agreement, the two keys <em>must</em> use the same group.  There are
currently two kinds of discrete logarithm groups supported in botan: the
integers modulo a prime, represented by <a class="reference internal" href="#dl-group"><span class="std std-ref">DL_Group</span></a>, and elliptic curves
in GF(p), represented by <a class="reference internal" href="#ec-group"><span class="std std-ref">EC_Group</span></a>. A rough generalization is that the
larger the group is, the more secure the algorithm is, but coorespondingly the
slower the operations will be.</p>
<p>Given a <code class="docutils literal"><span class="pre">DL_Group</span></code>, you can create new DSA, Diffie-Hellman, and
Nyberg-Rueppel key pairs with</p>
<dl class="function">
<dt id="_CPPv2N14DSA_PrivateKey14DSA_PrivateKeyER21RandomNumberGeneratorRK8DL_GroupRK6BigInt">
<span id="DSA_PrivateKey::DSA_PrivateKey__RandomNumberGeneratorR.DL_GroupCR.BigIntCR"></span><code class="descclassname">DSA_PrivateKey::</code><code class="descname">DSA_PrivateKey</code><span class="sig-paren">(</span>RandomNumberGenerator &amp;<em>rng</em>, <em class="property">const</em> DL_Group &amp;<em>group</em>, <em class="property">const</em> BigInt &amp;<em>x</em> = 0<span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N14DSA_PrivateKey14DSA_PrivateKeyER21RandomNumberGeneratorRK8DL_GroupRK6BigInt" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N13DH_PrivateKey13DH_PrivateKeyER21RandomNumberGeneratorRK8DL_GroupRK6BigInt">
<span id="DH_PrivateKey::DH_PrivateKey__RandomNumberGeneratorR.DL_GroupCR.BigIntCR"></span><code class="descclassname">DH_PrivateKey::</code><code class="descname">DH_PrivateKey</code><span class="sig-paren">(</span>RandomNumberGenerator &amp;<em>rng</em>, <em class="property">const</em> DL_Group &amp;<em>group</em>, <em class="property">const</em> BigInt &amp;<em>x</em> = 0<span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N13DH_PrivateKey13DH_PrivateKeyER21RandomNumberGeneratorRK8DL_GroupRK6BigInt" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N13NR_PrivateKey13NR_PrivateKeyER21RandomNumberGeneratorRK8DL_GroupRK6BigInt">
<span id="NR_PrivateKey::NR_PrivateKey__RandomNumberGeneratorR.DL_GroupCR.BigIntCR"></span><code class="descclassname">NR_PrivateKey::</code><code class="descname">NR_PrivateKey</code><span class="sig-paren">(</span>RandomNumberGenerator &amp;<em>rng</em>, <em class="property">const</em> DL_Group &amp;<em>group</em>, <em class="property">const</em> BigInt &amp;<em>x</em> = 0<span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N13NR_PrivateKey13NR_PrivateKeyER21RandomNumberGeneratorRK8DL_GroupRK6BigInt" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N18ElGamal_PrivateKey18ElGamal_PrivateKeyER21RandomNumberGeneratorRK8DL_GroupRK6BigInt">
<span id="ElGamal_PrivateKey::ElGamal_PrivateKey__RandomNumberGeneratorR.DL_GroupCR.BigIntCR"></span><code class="descclassname">ElGamal_PrivateKey::</code><code class="descname">ElGamal_PrivateKey</code><span class="sig-paren">(</span>RandomNumberGenerator &amp;<em>rng</em>, <em class="property">const</em> DL_Group &amp;<em>group</em>, <em class="property">const</em> BigInt &amp;<em>x</em> = 0<span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N18ElGamal_PrivateKey18ElGamal_PrivateKeyER21RandomNumberGeneratorRK8DL_GroupRK6BigInt" title="Permalink to this definition">¶</a></dt>
<dd><p>The optional <em>x</em> parameter to each of these contructors is a private key
value. This allows you to create keys where the private key is formed by
some special technique; for instance you can use the hash of a password (see
<a class="reference internal" href="pbkdf.html#pbkdf"><span class="std std-ref">PBKDF Algorithms</span></a> for how to do that) as a private key value. Normally, you would
leave the value as zero, letting the class generate a new random key.</p>
</dd></dl>

<p>Finally, given an <code class="docutils literal"><span class="pre">EC_Group</span></code> object, you can create a new ECDSA,
ECDH, or GOST 34.10-2001 private key with</p>
<dl class="function">
<dt id="_CPPv2N16ECDSA_PrivateKey16ECDSA_PrivateKeyER21RandomNumberGeneratorRK8EC_GroupRK6BigInt">
<span id="ECDSA_PrivateKey::ECDSA_PrivateKey__RandomNumberGeneratorR.EC_GroupCR.BigIntCR"></span><code class="descclassname">ECDSA_PrivateKey::</code><code class="descname">ECDSA_PrivateKey</code><span class="sig-paren">(</span>RandomNumberGenerator &amp;<em>rng</em>, <em class="property">const</em> EC_Group &amp;<em>domain</em>, <em class="property">const</em> BigInt &amp;<em>x</em> = 0<span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N16ECDSA_PrivateKey16ECDSA_PrivateKeyER21RandomNumberGeneratorRK8EC_GroupRK6BigInt" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N15ECDH_PrivateKey15ECDH_PrivateKeyER21RandomNumberGeneratorRK8EC_GroupRK6BigInt">
<span id="ECDH_PrivateKey::ECDH_PrivateKey__RandomNumberGeneratorR.EC_GroupCR.BigIntCR"></span><code class="descclassname">ECDH_PrivateKey::</code><code class="descname">ECDH_PrivateKey</code><span class="sig-paren">(</span>RandomNumberGenerator &amp;<em>rng</em>, <em class="property">const</em> EC_Group &amp;<em>domain</em>, <em class="property">const</em> BigInt &amp;<em>x</em> = 0<span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N15ECDH_PrivateKey15ECDH_PrivateKeyER21RandomNumberGeneratorRK8EC_GroupRK6BigInt" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N20GOST_3410_PrivateKey20GOST_3410_PrivateKeyER21RandomNumberGeneratorRK8EC_GroupRK6BigInt">
<span id="GOST_3410_PrivateKey::GOST_3410_PrivateKey__RandomNumberGeneratorR.EC_GroupCR.BigIntCR"></span><code class="descclassname">GOST_3410_PrivateKey::</code><code class="descname">GOST_3410_PrivateKey</code><span class="sig-paren">(</span>RandomNumberGenerator &amp;<em>rng</em>, <em class="property">const</em> EC_Group &amp;<em>domain</em>, <em class="property">const</em> BigInt &amp;<em>x</em> = 0<span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N20GOST_3410_PrivateKey20GOST_3410_PrivateKeyER21RandomNumberGeneratorRK8EC_GroupRK6BigInt" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="section" id="generating-rsa-keys">
<h3>Generating RSA keys<a class="headerlink" href="#generating-rsa-keys" title="Permalink to this headline">¶</a></h3>
<p>This example will generate an RSA key of a specified bitlength, and put it
into a pair of key files. One is the public key in X.509 format (PEM encoded),
the private key is in PKCS #8 format (also PEM encoded), either encrypted or
unencrypted depending on if a password was given.</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;fstream&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;cstdlib&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;memory&gt;</span><span class="cp"></span>

<span class="cp">#include</span> <span class="cpf">&lt;botan/botan.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;botan/rsa.h&gt;</span><span class="cp"></span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">Botan</span><span class="p">;</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="n">argv</span><span class="p">[])</span>
   <span class="p">{</span>
   <span class="k">if</span><span class="p">(</span><span class="n">argc</span> <span class="o">!=</span> <span class="mi">2</span> <span class="o">&amp;&amp;</span> <span class="n">argc</span> <span class="o">!=</span> <span class="mi">3</span><span class="p">)</span>
      <span class="p">{</span>
      <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Usage: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;&lt;</span> <span class="s">&quot; bitsize [passphrase]&quot;</span>
                <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
      <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
      <span class="p">}</span>

   <span class="k">const</span> <span class="kt">size_t</span> <span class="n">bits</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">atoi</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
   <span class="k">if</span><span class="p">(</span><span class="n">bits</span> <span class="o">&lt;</span> <span class="mi">1024</span> <span class="o">||</span> <span class="n">bits</span> <span class="o">&gt;</span> <span class="mi">16384</span><span class="p">)</span>
      <span class="p">{</span>
      <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Invalid argument for bitsize&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
      <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
      <span class="p">}</span>

   <span class="k">try</span>
      <span class="p">{</span>
      <span class="n">Botan</span><span class="o">::</span><span class="n">LibraryInitializer</span> <span class="n">init</span><span class="p">;</span>

      <span class="n">std</span><span class="o">::</span><span class="n">ofstream</span> <span class="n">pub</span><span class="p">(</span><span class="s">&quot;rsapub.pem&quot;</span><span class="p">);</span>
      <span class="n">std</span><span class="o">::</span><span class="n">ofstream</span> <span class="n">priv</span><span class="p">(</span><span class="s">&quot;rsapriv.pem&quot;</span><span class="p">);</span>
      <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">priv</span> <span class="o">||</span> <span class="o">!</span><span class="n">pub</span><span class="p">)</span>
         <span class="p">{</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Couldn&#39;t write output files&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
         <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
         <span class="p">}</span>

      <span class="n">AutoSeeded_RNG</span> <span class="n">rng</span><span class="p">;</span>

      <span class="n">RSA_PrivateKey</span> <span class="n">key</span><span class="p">(</span><span class="n">rng</span><span class="p">,</span> <span class="n">bits</span><span class="p">);</span>
      <span class="n">pub</span> <span class="o">&lt;&lt;</span> <span class="n">X509</span><span class="o">::</span><span class="n">PEM_encode</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>

      <span class="k">if</span><span class="p">(</span><span class="n">argc</span> <span class="o">==</span> <span class="mi">2</span><span class="p">)</span>
         <span class="n">priv</span> <span class="o">&lt;&lt;</span> <span class="n">PKCS8</span><span class="o">::</span><span class="n">PEM_encode</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
      <span class="k">else</span>
         <span class="n">priv</span> <span class="o">&lt;&lt;</span> <span class="n">PKCS8</span><span class="o">::</span><span class="n">PEM_encode</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">rng</span><span class="p">,</span> <span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">]);</span>
      <span class="p">}</span>
   <span class="k">catch</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">exception</span><span class="o">&amp;</span> <span class="n">e</span><span class="p">)</span>
      <span class="p">{</span>
      <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Exception caught: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">e</span><span class="p">.</span><span class="n">what</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
      <span class="p">}</span>
   <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
   <span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="generate-dsa-keys">
<h3>Generate DSA keys<a class="headerlink" href="#generate-dsa-keys" title="Permalink to this headline">¶</a></h3>
<p>This example generates a 2048 bit DSA key</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;fstream&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;botan/botan.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;botan/dsa.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;botan/rng.h&gt;</span><span class="cp"></span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">Botan</span><span class="p">;</span>

<span class="cp">#include</span> <span class="cpf">&lt;memory&gt;</span><span class="cp"></span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="n">argv</span><span class="p">[])</span>
   <span class="p">{</span>
   <span class="k">try</span>
      <span class="p">{</span>
      <span class="k">if</span><span class="p">(</span><span class="n">argc</span> <span class="o">!=</span> <span class="mi">1</span> <span class="o">&amp;&amp;</span> <span class="n">argc</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">)</span>
         <span class="p">{</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Usage: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;&lt;</span> <span class="s">&quot; [passphrase]&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
         <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
         <span class="p">}</span>

      <span class="n">std</span><span class="o">::</span><span class="n">ofstream</span> <span class="n">priv</span><span class="p">(</span><span class="s">&quot;dsapriv.pem&quot;</span><span class="p">);</span>
      <span class="n">std</span><span class="o">::</span><span class="n">ofstream</span> <span class="n">pub</span><span class="p">(</span><span class="s">&quot;dsapub.pem&quot;</span><span class="p">);</span>
      <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">priv</span> <span class="o">||</span> <span class="o">!</span><span class="n">pub</span><span class="p">)</span>
         <span class="p">{</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Couldn&#39;t write output files&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
         <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
         <span class="p">}</span>

      <span class="n">Botan</span><span class="o">::</span><span class="n">LibraryInitializer</span> <span class="n">init</span><span class="p">;</span>

      <span class="n">AutoSeeded_RNG</span> <span class="n">rng</span><span class="p">;</span>

      <span class="n">DL_Group</span> <span class="n">group</span><span class="p">(</span><span class="n">rng</span><span class="p">,</span> <span class="n">DL_Group</span><span class="o">::</span><span class="n">DSA_Kosherizer</span><span class="p">,</span> <span class="mi">2048</span><span class="p">,</span> <span class="mi">256</span><span class="p">);</span>

      <span class="n">DSA_PrivateKey</span> <span class="n">key</span><span class="p">(</span><span class="n">rng</span><span class="p">,</span> <span class="n">group</span><span class="p">);</span>

      <span class="n">pub</span> <span class="o">&lt;&lt;</span> <span class="n">X509</span><span class="o">::</span><span class="n">PEM_encode</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
      <span class="k">if</span><span class="p">(</span><span class="n">argc</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span>
         <span class="n">priv</span> <span class="o">&lt;&lt;</span> <span class="n">PKCS8</span><span class="o">::</span><span class="n">PEM_encode</span><span class="p">(</span><span class="n">key</span><span class="p">);</span>
      <span class="k">else</span>
         <span class="n">priv</span> <span class="o">&lt;&lt;</span> <span class="n">PKCS8</span><span class="o">::</span><span class="n">PEM_encode</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">rng</span><span class="p">,</span> <span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span>
   <span class="p">}</span>
   <span class="k">catch</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">exception</span><span class="o">&amp;</span> <span class="n">e</span><span class="p">)</span>
      <span class="p">{</span>
      <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Exception caught: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">e</span><span class="p">.</span><span class="n">what</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
      <span class="p">}</span>
   <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
   <span class="p">}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="serializing-private-keys-using-pkcs-8">
<span id="serializing-private-keys"></span><h2>Serializing Private Keys Using PKCS #8<a class="headerlink" href="#serializing-private-keys-using-pkcs-8" title="Permalink to this headline">¶</a></h2>
<p>The standard format for serializing a private key is PKCS #8, the operations
for which are defined in <code class="docutils literal"><span class="pre">pkcs8.h</span></code>. It supports both unencrypted and
encrypted storage.</p>
<dl class="function">
<dt id="_CPPv2N5PKCS810BER_encodeERK11Private_KeyR21RandomNumberGeneratorRKNSt6stringERKNSt6stringE">
<span id="PKCS8::BER_encode__Private_KeyCR.RandomNumberGeneratorR.ssCR.ssCR"></span>SecureVector&lt;byte&gt; <code class="descclassname">PKCS8::</code><code class="descname">BER_encode</code><span class="sig-paren">(</span><em class="property">const</em> Private_Key &amp;<em>key</em>, RandomNumberGenerator &amp;<em>rng</em>, <em class="property">const</em> std::string &amp;<em>password</em>, <em class="property">const</em> std::string &amp;<em>pbe_algo</em> = &quot;&quot;<span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N5PKCS810BER_encodeERK11Private_KeyR21RandomNumberGeneratorRKNSt6stringERKNSt6stringE" title="Permalink to this definition">¶</a></dt>
<dd><p>Takes any private key object, serializes it, encrypts it using
<em>password</em>, and returns a binary structure representing the private
key.</p>
<p>The final (optional) argument, <em>pbe_algo</em>, specifies a particular
password based encryption (or PBE) algorithm. If you don&#8217;t specify a
PBE, a sensible default will be used.</p>
</dd></dl>

<dl class="function">
<dt id="_CPPv2N5PKCS810PEM_encodeERK11Private_KeyR21RandomNumberGeneratorRKNSt6stringERKNSt6stringE">
<span id="PKCS8::PEM_encode__Private_KeyCR.RandomNumberGeneratorR.ssCR.ssCR"></span>std::string <code class="descclassname">PKCS8::</code><code class="descname">PEM_encode</code><span class="sig-paren">(</span><em class="property">const</em> Private_Key &amp;<em>key</em>, RandomNumberGenerator &amp;<em>rng</em>, <em class="property">const</em> std::string &amp;<em>pass</em>, <em class="property">const</em> std::string &amp;<em>pbe_algo</em> = &quot;&quot;<span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N5PKCS810PEM_encodeERK11Private_KeyR21RandomNumberGeneratorRKNSt6stringERKNSt6stringE" title="Permalink to this definition">¶</a></dt>
<dd><p>This formats the key in the same manner as <code class="docutils literal"><span class="pre">BER_encode</span></code>, but additionally
encodes it into a text format with identifying headers. Using PEM encoding
is <em>highly</em> recommended for many reasons, including compatibility with other
software, for transmission over 8-bit unclean channels, because it can be
identified by a human without special tools, and because it sometimes allows
more sane behavior of tools that process the data.</p>
</dd></dl>

<p>Unencrypted serialization is also supported.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">In most situations, using unecrypted private key storage is a bad idea,
because anyone can come along and grab the private key without having to
know any passwords or other secrets. Unless you have very particular
security requirements, always use the versions that encrypt the key based on
a passphrase, described above.</p>
</div>
<dl class="function">
<dt id="_CPPv2N5PKCS810BER_encodeERK11Private_Key">
<span id="PKCS8::BER_encode__Private_KeyCR"></span>SecureVector&lt;byte&gt; <code class="descclassname">PKCS8::</code><code class="descname">BER_encode</code><span class="sig-paren">(</span><em class="property">const</em> Private_Key &amp;<em>key</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N5PKCS810BER_encodeERK11Private_Key" title="Permalink to this definition">¶</a></dt>
<dd><p>Serializes the private key and returns the result.</p>
</dd></dl>

<dl class="function">
<dt id="_CPPv2N5PKCS810PEM_encodeERK11Private_Key">
<span id="PKCS8::PEM_encode__Private_KeyCR"></span>std::string <code class="descclassname">PKCS8::</code><code class="descname">PEM_encode</code><span class="sig-paren">(</span><em class="property">const</em> Private_Key &amp;<em>key</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N5PKCS810PEM_encodeERK11Private_Key" title="Permalink to this definition">¶</a></dt>
<dd><p>Serializes the private key, base64 encodes it, and returns the
result.</p>
</dd></dl>

<p>Last but not least, there are some functions that will load (and
decrypt, if necessary) a PKCS #8 private key:</p>
<dl class="function">
<dt id="_CPPv2N5PKCS88load_keyER10DataSourceR21RandomNumberGeneratorRK14User_Interface">
<span id="PKCS8::load_key__DataSourceR.RandomNumberGeneratorR.User_InterfaceCR"></span>Private_Key *<code class="descclassname">PKCS8::</code><code class="descname">load_key</code><span class="sig-paren">(</span>DataSource &amp;<em>in</em>, RandomNumberGenerator &amp;<em>rng</em>, <em class="property">const</em> User_Interface &amp;<em>ui</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N5PKCS88load_keyER10DataSourceR21RandomNumberGeneratorRK14User_Interface" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N5PKCS88load_keyER10DataSourceR21RandomNumberGeneratorNSt6stringE">
<span id="PKCS8::load_key__DataSourceR.RandomNumberGeneratorR.ss"></span>Private_Key *<code class="descclassname">PKCS8::</code><code class="descname">load_key</code><span class="sig-paren">(</span>DataSource &amp;<em>in</em>, RandomNumberGenerator &amp;<em>rng</em>, std::string <em>passphrase</em> = &quot;&quot;<span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N5PKCS88load_keyER10DataSourceR21RandomNumberGeneratorNSt6stringE" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N5PKCS88load_keyERKNSt6stringER21RandomNumberGeneratorRK14User_Interface">
<span id="PKCS8::load_key__ssCR.RandomNumberGeneratorR.User_InterfaceCR"></span>Private_Key *<code class="descclassname">PKCS8::</code><code class="descname">load_key</code><span class="sig-paren">(</span><em class="property">const</em> std::string &amp;<em>filename</em>, RandomNumberGenerator &amp;<em>rng</em>, <em class="property">const</em> User_Interface &amp;<em>ui</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N5PKCS88load_keyERKNSt6stringER21RandomNumberGeneratorRK14User_Interface" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N5PKCS88load_keyERKNSt6stringER21RandomNumberGeneratorRKNSt6stringE">
<span id="PKCS8::load_key__ssCR.RandomNumberGeneratorR.ssCR"></span>Private_Key *<code class="descclassname">PKCS8::</code><code class="descname">load_key</code><span class="sig-paren">(</span><em class="property">const</em> std::string &amp;<em>filename</em>, RandomNumberGenerator &amp;<em>rng</em>, <em class="property">const</em> std::string &amp;<em>passphrase</em> = &quot;&quot;<span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N5PKCS88load_keyERKNSt6stringER21RandomNumberGeneratorRKNSt6stringE" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>These functions will return an object allocated key object based on the data
from whatever source it is using (assuming, of course, the source is in fact
storing a representation of a private key, and the decryption was
sucessful). The encoding used (PEM or BER) need not be specified; the format
will be detected automatically. The key is allocated with <code class="docutils literal"><span class="pre">new</span></code>, and should
be released with <code class="docutils literal"><span class="pre">delete</span></code> when you are done with it. The first takes a
generic <code class="docutils literal"><span class="pre">DataSource</span></code> that you have to create - the other is a simple wrapper
functions that take either a filename or a memory buffer and create the
appropriate <code class="docutils literal"><span class="pre">DataSource</span></code>.</p>
<p>The versions taking a <code class="docutils literal"><span class="pre">std::string</span></code> attempt to decrypt using the password
given (if the key is encrypted; if it is not, the passphase value will be
ignored). If the passphrase does not decrypt the key, an exception will be
thrown.</p>
<p>The ones taking a <code class="docutils literal"><span class="pre">User_Interface</span></code> provide a simple callback interface which
makes handling incorrect passphrases and such a bit simpler. A
<code class="docutils literal"><span class="pre">User_Interface</span></code> has very little to do with talking to users; it&#8217;s just a
way to glue together Botan and whatever user interface you happen to be using.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">In a future version, it is likely that <code class="docutils literal"><span class="pre">User_Interface</span></code> will be
replaced by a simple callback using <code class="docutils literal"><span class="pre">std::function</span></code>.</p>
</div>
<p>To use <code class="docutils literal"><span class="pre">User_Interface</span></code>, derive a subclass and implement:</p>
<dl class="function">
<dt id="_CPPv2NK14User_Interface14get_passphraseERKNSt6stringERKNSt6stringER9UI_Result">
<span id="User_Interface::get_passphrase__ssCR.ssCR.UI_ResultRC"></span>std::string <code class="descclassname">User_Interface::</code><code class="descname">get_passphrase</code><span class="sig-paren">(</span><em class="property">const</em> std::string &amp;<em>what</em>, <em class="property">const</em> std::string &amp;<em>source</em>, UI_Result &amp;<em>result</em><span class="sig-paren">)</span> <em class="property">const</em><a class="headerlink" href="#_CPPv2NK14User_Interface14get_passphraseERKNSt6stringERKNSt6stringER9UI_Result" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal"><span class="pre">what</span></code> argument specifies what the passphrase is needed for (for
example, PKCS #8 key loading passes <code class="docutils literal"><span class="pre">what</span></code> as &#8220;PKCS #8 private key&#8221;). This
lets you provide the user with some indication of <em>why</em> your application is
asking for a passphrase; feel free to pass the string through <code class="docutils literal"><span class="pre">gettext(3)</span></code>
or moral equivalent for i18n purposes. Similarly, <code class="docutils literal"><span class="pre">source</span></code> specifies where
the data in question came from, if available (for example, a file name). If
the source is not available for whatever reason, then <code class="docutils literal"><span class="pre">source</span></code> will be an
empty string; be sure to account for this possibility.</p>
<p>The function returns the passphrase as the return value, and a status code
in <code class="docutils literal"><span class="pre">result</span></code> (either <code class="docutils literal"><span class="pre">OK</span></code> or <code class="docutils literal"><span class="pre">CANCEL_ACTION</span></code>). If <code class="docutils literal"><span class="pre">CANCEL_ACTION</span></code> is
returned in <code class="docutils literal"><span class="pre">result</span></code>, then the return value will be ignored, and the
caller will take whatever action is necessary (typically, throwing an
exception stating that the passphrase couldn&#8217;t be determined). In the
specific case of PKCS #8 key decryption, a <code class="docutils literal"><span class="pre">Decoding_Error</span></code> exception will
be thrown; your UI should assume this can happen, and provide appropriate
error handling (such as putting up a dialog box informing the user of the
situation, and canceling the operation in progress).</p>
</dd></dl>

<div class="section" id="serializing-public-keys">
<span id="id2"></span><h3>Serializing Public Keys<a class="headerlink" href="#serializing-public-keys" title="Permalink to this headline">¶</a></h3>
<p>To import and export public keys, use:</p>
<dl class="function">
<dt id="_CPPv2N4X50910BER_encodeERK10Public_Key">
<span id="X509::BER_encode__Public_KeyCR"></span>MemoryVector&lt;byte&gt; <code class="descclassname">X509::</code><code class="descname">BER_encode</code><span class="sig-paren">(</span><em class="property">const</em> Public_Key &amp;<em>key</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N4X50910BER_encodeERK10Public_Key" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N4X50910PEM_encodeERK10Public_Key">
<span id="X509::PEM_encode__Public_KeyCR"></span>std::string <code class="descclassname">X509::</code><code class="descname">PEM_encode</code><span class="sig-paren">(</span><em class="property">const</em> Public_Key &amp;<em>key</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N4X50910PEM_encodeERK10Public_Key" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N4X5098load_keyER10DataSource">
<span id="X509::load_key__DataSourceR"></span>Public_Key *<code class="descclassname">X509::</code><code class="descname">load_key</code><span class="sig-paren">(</span>DataSource &amp;<em>in</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N4X5098load_keyER10DataSource" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N4X5098load_keyERK12SecureVectorI4byteE">
<span id="X509::load_key__SecureVector:byte:CR"></span>Public_Key *<code class="descclassname">X509::</code><code class="descname">load_key</code><span class="sig-paren">(</span><em class="property">const</em> SecureVector&lt;byte&gt; &amp;<em>buffer</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N4X5098load_keyERK12SecureVectorI4byteE" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N4X5098load_keyERKNSt6stringE">
<span id="X509::load_key__ssCR"></span>Public_Key *<code class="descclassname">X509::</code><code class="descname">load_key</code><span class="sig-paren">(</span><em class="property">const</em> std::string &amp;<em>filename</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N4X5098load_keyERKNSt6stringE" title="Permalink to this definition">¶</a></dt>
<dd><p>These functions operate in the same way as the ones described in
<a class="reference internal" href="#serializing-private-keys"><span class="std std-ref">Serializing Private Keys Using PKCS #8</span></a>, except that no encryption option is
availabe.</p>
</dd></dl>

</div>
<div class="section" id="dl-group">
<span id="id3"></span><h3>DL_Group<a class="headerlink" href="#dl-group" title="Permalink to this headline">¶</a></h3>
<p>As described in <a class="reference internal" href="#creating-new-private-keys"><span class="std std-ref">Creating New Private Keys</span></a>, a discrete logarithm group
can be shared among many keys, even keys created by users who do not trust
each other. However, it is necessary to trust the entity who created the
group; that is why organization like NIST use algorithms which generate groups
in a deterministic way such that creating a bogus group would require breaking
some trusted cryptographic primitive like SHA-2.</p>
<p>Instantiating a <code class="docutils literal"><span class="pre">DL_Group</span></code> simply requires calling</p>
<dl class="function">
<dt id="_CPPv2N8DL_Group8DL_GroupERKNSt6stringE">
<span id="DL_Group::DL_Group__ssCR"></span><code class="descclassname">DL_Group::</code><code class="descname">DL_Group</code><span class="sig-paren">(</span><em class="property">const</em> std::string &amp;<em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N8DL_Group8DL_GroupERKNSt6stringE" title="Permalink to this definition">¶</a></dt>
<dd><p>The <em>name</em> parameter is a specially formatted string that consists of three
things, the type of the group (&#8220;modp&#8221; or &#8220;dsa&#8221;), the creator of the group,
and the size of the group in bits, all delimited by &#8216;/&#8217; characters.</p>
<p>Currently all &#8220;modp&#8221; groups included in botan are ones defined by the
Internet Engineering Task Force, so the provider is &#8220;ietf&#8221;, and the strings
look like &#8220;modp/ietf/N&#8221; where N can be any of 768, 1024, 1536, 2048, 3072,
4096, 6144, or 8192. This group type is used for Diffie-Hellman and ElGamal
algorithms.</p>
<p>The other type, &#8220;dsa&#8221; is used for DSA and Nyberg-Rueppel keys.  They can
also be used with Diffie-Hellman and ElGamal, but this is less common. The
currently available groups are &#8220;dsa/jce/N&#8221; for N in 512, 768, or 1024, and
&#8220;dsa/botan/N&#8221; with N being 2048 or 3072.  The &#8220;jce&#8221; groups are the standard
DSA groups used in the Java Cryptography Extensions, while the &#8220;botan&#8221;
groups were randomly generated using the FIPS 186-3 algorithm by the library
maintainers.</p>
</dd></dl>

<p>You can generate a new random group using</p>
<dl class="function">
<dt id="_CPPv2N8DL_Group8DL_GroupER21RandomNumberGenerator9PrimeType6size_t6size_t">
<span id="DL_Group::DL_Group__RandomNumberGeneratorR.PrimeType.s.s"></span><code class="descclassname">DL_Group::</code><code class="descname">DL_Group</code><span class="sig-paren">(</span>RandomNumberGenerator &amp;<em>rng</em>, PrimeType <em>type</em>, size_t <em>pbits</em>, size_t <em>qbits</em> = 0<span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N8DL_Group8DL_GroupER21RandomNumberGenerator9PrimeType6size_t6size_t" title="Permalink to this definition">¶</a></dt>
<dd><p>The <em>type</em> can be either <code class="docutils literal"><span class="pre">Strong</span></code>, <code class="docutils literal"><span class="pre">Prime_Subgroup</span></code>, or
<code class="docutils literal"><span class="pre">DSA_Kosherizer</span></code>. <em>pbits</em> specifies the size of the prime in
bits. If the <em>type</em> is <code class="docutils literal"><span class="pre">Prime_Subgroup</span></code> or <code class="docutils literal"><span class="pre">DSA_Kosherizer</span></code>,
then <em>qbits</em> specifies the size of the subgroup.</p>
</dd></dl>

<p>You can serialize a <code class="docutils literal"><span class="pre">DL_Group</span></code> using</p>
<dl class="function">
<dt id="_CPPv2N8DL_Group10DER_EncodeE6Format">
<span id="DL_Group::DER_Encode__Format"></span>SecureVector&lt;byte&gt; <code class="descclassname">DL_Group::</code><code class="descname">DER_Encode</code><span class="sig-paren">(</span>Format <em>format</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N8DL_Group10DER_EncodeE6Format" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>or</p>
<dl class="function">
<dt id="_CPPv2N8DL_Group10PEM_encodeE6Format">
<span id="DL_Group::PEM_encode__Format"></span>std::string <code class="descclassname">DL_Group::</code><code class="descname">PEM_encode</code><span class="sig-paren">(</span>Format <em>format</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N8DL_Group10PEM_encodeE6Format" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>where <em>format</em> is any of</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">ANSI_X9_42</span></code> (or <code class="docutils literal"><span class="pre">DH_PARAMETERS</span></code>) for modp groups</li>
<li><code class="docutils literal"><span class="pre">ANSI_X9_57</span></code> (or <code class="docutils literal"><span class="pre">DSA_PARAMETERS</span></code>) for DSA-style groups</li>
<li><code class="docutils literal"><span class="pre">PKCS_3</span></code> is an older format for modp groups; it should only
be used for backwards compatability.</li>
</ul>
<p>You can reload a serialized group using</p>
<dl class="function">
<dt id="_CPPv2N8DL_Group10BER_decodeER10DataSource6Format">
<span id="DL_Group::BER_decode__DataSourceR.Format"></span>void <code class="descclassname">DL_Group::</code><code class="descname">BER_decode</code><span class="sig-paren">(</span>DataSource &amp;<em>source</em>, Format <em>format</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N8DL_Group10BER_decodeER10DataSource6Format" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N8DL_Group10PEM_decodeER10DataSource">
<span id="DL_Group::PEM_decode__DataSourceR"></span>void <code class="descclassname">DL_Group::</code><code class="descname">PEM_decode</code><span class="sig-paren">(</span>DataSource &amp;<em>source</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N8DL_Group10PEM_decodeER10DataSource" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

</div>
<div class="section" id="ec-group">
<span id="id4"></span><h3>EC_Group<a class="headerlink" href="#ec-group" title="Permalink to this headline">¶</a></h3>
<p>An <code class="docutils literal"><span class="pre">EC_Group</span></code> is initialized by passing the name of the
group to be used to the constructor. These groups have
semi-standardized names like &#8220;secp256r1&#8221; and &#8220;brainpool512r1&#8221;.</p>
</div>
</div>
<div class="section" id="key-checking">
<h2>Key Checking<a class="headerlink" href="#key-checking" title="Permalink to this headline">¶</a></h2>
<p>Most public key algorithms have limitations or restrictions on their
parameters. For example RSA requires an odd exponent, and algorithms
based on the discrete logarithm problem need a generator $&gt; 1$.</p>
<p>Each public key type has a function</p>
<dl class="function">
<dt id="_CPPv2N10Public_Key9check_keyER21RandomNumberGeneratorb">
<span id="Public_Key::check_key__RandomNumberGeneratorR.b"></span>bool <code class="descclassname">Public_Key::</code><code class="descname">check_key</code><span class="sig-paren">(</span>RandomNumberGenerator &amp;<em>rng</em>, bool <em>strong</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N10Public_Key9check_keyER21RandomNumberGeneratorb" title="Permalink to this definition">¶</a></dt>
<dd><p>This function performs a number of algorithm-specific tests that the key
seems to be mathematically valid and consistent, and returns true if all of
the tests pass.</p>
<p>It does not have anything to do with the validity of the key for any
particular use, nor does it have anything to do with certificates that link
a key (which, after all, is just some numbers) with a user or other
entity. If <em>strong</em> is <code class="docutils literal"><span class="pre">true</span></code>, then it does &#8220;strong&#8221; checking, which
includes expensive operations like primality checking.</p>
</dd></dl>

</div>
<div class="section" id="encryption">
<h2>Encryption<a class="headerlink" href="#encryption" title="Permalink to this headline">¶</a></h2>
<p>Safe public key encryption requires the use of a padding scheme which hides
the underlying mathematical properties of the algorithm.  Additionally, they
will add randomness, so encrypting the same plaintext twice produces two
different ciphertexts.</p>
<p>The primary interface for encryption is</p>
<dl class="class">
<dt id="_CPPv212PK_Encryptor">
<span id="PK_Encryptor"></span><em class="property">class </em><code class="descclassname"></code><code class="descname">PK_Encryptor</code><a class="headerlink" href="#_CPPv212PK_Encryptor" title="Permalink to this definition">¶</a></dt>
<dd><dl class="function">
<dt id="_CPPv2NK12PK_Encryptor7encryptEPK4byte6size_tR21RandomNumberGenerator">
<span id="PK_Encryptor::encrypt__byteCP.s.RandomNumberGeneratorRC"></span>SecureVector&lt;byte&gt; <code class="descclassname"></code><code class="descname">encrypt</code><span class="sig-paren">(</span><em class="property">const</em> byte *<em>in</em>, size_t <em>length</em>, RandomNumberGenerator &amp;<em>rng</em><span class="sig-paren">)</span> <em class="property">const</em><a class="headerlink" href="#_CPPv2NK12PK_Encryptor7encryptEPK4byte6size_tR21RandomNumberGenerator" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2NK12PK_Encryptor7encryptERK12MemoryRegionI4byteER21RandomNumberGenerator">
<span id="PK_Encryptor::encrypt__MemoryRegion:byte:CR.RandomNumberGeneratorRC"></span>SecureVector&lt;byte&gt; <code class="descclassname"></code><code class="descname">encrypt</code><span class="sig-paren">(</span><em class="property">const</em> MemoryRegion&lt;byte&gt; &amp;<em>in</em>, RandomNumberGenerator &amp;<em>rng</em><span class="sig-paren">)</span> <em class="property">const</em><a class="headerlink" href="#_CPPv2NK12PK_Encryptor7encryptERK12MemoryRegionI4byteER21RandomNumberGenerator" title="Permalink to this definition">¶</a></dt>
<dd><p>These encrypt a message, returning the ciphertext.</p>
</dd></dl>

<dl class="function">
<dt id="_CPPv2NK12PK_Encryptor18maximum_input_sizeEv">
<span id="PK_Encryptor::maximum_input_sizeC"></span>size_t <code class="descclassname"></code><code class="descname">maximum_input_size</code><span class="sig-paren">(</span><span class="sig-paren">)</span> <em class="property">const</em><a class="headerlink" href="#_CPPv2NK12PK_Encryptor18maximum_input_sizeEv" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the maximum size of the message that can be processed, in
bytes. If you call <a class="reference internal" href="#_CPPv2NK12PK_Encryptor7encryptEPK4byte6size_tR21RandomNumberGenerator" title="PK_Encryptor::encrypt"><code class="xref cpp cpp-func docutils literal"><span class="pre">PK_Encryptor::encrypt</span></code></a> with a value larger
than this the operation will fail with an exception.</p>
</dd></dl>

</dd></dl>

<p><a class="reference internal" href="#_CPPv212PK_Encryptor" title="PK_Encryptor"><code class="xref cpp cpp-class docutils literal"><span class="pre">PK_Encryptor</span></code></a> is only an interface - to actually encrypt you have
to create an implementation, of which there are currently two available in the
library, <a class="reference internal" href="#_CPPv216PK_Encryptor_EME" title="PK_Encryptor_EME"><code class="xref cpp cpp-class docutils literal"><span class="pre">PK_Encryptor_EME</span></code></a> and <a class="reference internal" href="#_CPPv215DLIES_Encryptor" title="DLIES_Encryptor"><code class="xref cpp cpp-class docutils literal"><span class="pre">DLIES_Encryptor</span></code></a>. DLIES
is a standard method (from IEEE 1363) that uses a key agreement technique such
as DH or ECDH to perform message encryption. Normally, public key encryption
is done using algorithms which support it directly, such as RSA or ElGamal;
these use the EME class:</p>
<dl class="class">
<dt id="_CPPv216PK_Encryptor_EME">
<span id="PK_Encryptor_EME"></span><em class="property">class </em><code class="descclassname"></code><code class="descname">PK_Encryptor_EME</code><a class="headerlink" href="#_CPPv216PK_Encryptor_EME" title="Permalink to this definition">¶</a></dt>
<dd><dl class="function">
<dt id="_CPPv2N16PK_Encryptor_EME16PK_Encryptor_EMEERK10Public_KeyNSt6stringE">
<span id="PK_Encryptor_EME::PK_Encryptor_EME__Public_KeyCR.ss"></span><code class="descclassname"></code><code class="descname">PK_Encryptor_EME</code><span class="sig-paren">(</span><em class="property">const</em> Public_Key &amp;<em>key</em>, std::string <em>eme</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N16PK_Encryptor_EME16PK_Encryptor_EMEERK10Public_KeyNSt6stringE" title="Permalink to this definition">¶</a></dt>
<dd><p>With <em>key</em> being the key you want to encrypt messages to. The padding
method to use is specified in <em>eme</em>.</p>
<p>The recommended values for <em>eme</em> is &#8220;EME1(SHA-1)&#8221; or &#8220;EME1(SHA-256)&#8221;. If
you need compatability with protocols using the PKCS #1 v1.5 standard,
you can also use &#8220;EME-PKCS1-v1_5&#8221;.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="_CPPv215DLIES_Encryptor">
<span id="DLIES_Encryptor"></span><em class="property">class </em><code class="descclassname"></code><code class="descname">DLIES_Encryptor</code><a class="headerlink" href="#_CPPv215DLIES_Encryptor" title="Permalink to this definition">¶</a></dt>
<dd><p>Available in the header <code class="docutils literal"><span class="pre">dlies.h</span></code></p>
<dl class="function">
<dt id="_CPPv2N15DLIES_Encryptor15DLIES_EncryptorERK20PK_Key_Agreement_KeyP3KDFP25MessageAuthenticationCode6size_t">
<span id="DLIES_Encryptor::DLIES_Encryptor__PK_Key_Agreement_KeyCR.KDFP.MessageAuthenticationCodeP.s"></span><code class="descclassname"></code><code class="descname">DLIES_Encryptor</code><span class="sig-paren">(</span><em class="property">const</em> PK_Key_Agreement_Key &amp;<em>key</em>, <a class="reference internal" href="kdf.html#_CPPv23KDF" title="KDF">KDF</a> *<em>kdf</em>, MessageAuthenticationCode *<em>mac</em>, size_t <em>mac_key_len</em> = 20<span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N15DLIES_Encryptor15DLIES_EncryptorERK20PK_Key_Agreement_KeyP3KDFP25MessageAuthenticationCode6size_t" title="Permalink to this definition">¶</a></dt>
<dd><p>Where <em>kdf</em> is a key derivation function (see
<a class="reference internal" href="kdf.html#key-derivation-function"><span class="std std-ref">Key Derivation Functions</span></a>) and <em>mac</em> is a
MessageAuthenticationCode.</p>
</dd></dl>

</dd></dl>

<p>The decryption classes are named <code class="docutils literal"><span class="pre">PK_Decryptor</span></code>, <code class="docutils literal"><span class="pre">PK_Decryptor_EME</span></code>, and
<code class="docutils literal"><span class="pre">DLIES_Decryptor</span></code>. They are created in the exact same way, except they take
the private key, and the processing function is named <code class="docutils literal"><span class="pre">decrypt</span></code>.</p>
</div>
<div class="section" id="signatures">
<h2>Signatures<a class="headerlink" href="#signatures" title="Permalink to this headline">¶</a></h2>
<p>Signature generation is performed using</p>
<dl class="class">
<dt id="_CPPv29PK_Signer">
<span id="PK_Signer"></span><em class="property">class </em><code class="descclassname"></code><code class="descname">PK_Signer</code><a class="headerlink" href="#_CPPv29PK_Signer" title="Permalink to this definition">¶</a></dt>
<dd><dl class="function">
<dt id="_CPPv2N9PK_Signer9PK_SignerERK11Private_KeyRKNSt6stringE16Signature_Format">
<span id="PK_Signer::PK_Signer__Private_KeyCR.ssCR.Signature_Format"></span><code class="descclassname"></code><code class="descname">PK_Signer</code><span class="sig-paren">(</span><em class="property">const</em> Private_Key &amp;<em>key</em>, <em class="property">const</em> std::string &amp;<em>emsa</em>, Signature_Format <em>format</em> = IEEE_1363<span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N9PK_Signer9PK_SignerERK11Private_KeyRKNSt6stringE16Signature_Format" title="Permalink to this definition">¶</a></dt>
<dd><p>Constructs a new signer object for the private key <em>key</em> using the
signature format <em>emsa</em>. The key must support signature operations.  In
the current version of the library, this includes RSA, DSA, ECDSA, GOST
34.10-2001, Nyberg-Rueppel, and Rabin-Williams. Other signature schemes
may be supported in the future.</p>
<p>Currently available values for <em>emsa</em> include EMSA1, EMSA2, EMSA3, EMSA4,
and Raw. All of them, except Raw, take a parameter naming a message
digest function to hash the message with. The Raw encoding signs the
input directly; if the message is too big, the signing operation will
fail. Raw is not useful except in very specialized applications. Examples
are &#8220;EMSA1(SHA-1)&#8221; and &#8220;EMSA4(SHA-256)&#8221;.</p>
<p>For RSA, use EMSA4 (also called PSS) unless you need compatability with
software that uses the older PKCS #1 v1.5 standard, in which case use
EMSA3 (also called &#8220;EMSA-PKCS1-v1_5&#8221;). For DSA, ECDSA, GOST 34.10-2001,
and Nyberg-Rueppel, you should use EMSA1.</p>
<p>The <em>format</em> defaults to <code class="docutils literal"><span class="pre">IEEE_1363</span></code> which is the only available
format for RSA. For DSA and ECDSA, you can also use
<code class="docutils literal"><span class="pre">DER_SEQUENCE</span></code>, which will format the signature as an ASN.1
SEQUENCE value.</p>
</dd></dl>

<dl class="function">
<dt id="_CPPv2N9PK_Signer6updateEPK4byte6size_t">
<span id="PK_Signer::update__byteCP.s"></span>void <code class="descclassname"></code><code class="descname">update</code><span class="sig-paren">(</span><em class="property">const</em> byte *<em>in</em>, size_t <em>length</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N9PK_Signer6updateEPK4byte6size_t" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N9PK_Signer6updateERK12MemoryRegionI4byteE">
<span id="PK_Signer::update__MemoryRegion:byte:CR"></span>void <code class="descclassname"></code><code class="descname">update</code><span class="sig-paren">(</span><em class="property">const</em> MemoryRegion&lt;byte&gt; &amp;<em>in</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N9PK_Signer6updateERK12MemoryRegionI4byteE" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N9PK_Signer6updateE4byte">
<span id="PK_Signer::update__byte"></span>void <code class="descclassname"></code><code class="descname">update</code><span class="sig-paren">(</span>byte <em>in</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N9PK_Signer6updateE4byte" title="Permalink to this definition">¶</a></dt>
<dd><p>These add more data to be included in the signature
computation. Typically, the input will be provided directly to a
hash function.</p>
</dd></dl>

<dl class="function">
<dt id="_CPPv2N9PK_Signer9signatureER21RandomNumberGenerator">
<span id="PK_Signer::signature__RandomNumberGeneratorR"></span>SecureVector&lt;byte&gt; <code class="descclassname"></code><code class="descname">signature</code><span class="sig-paren">(</span>RandomNumberGenerator &amp;<em>rng</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N9PK_Signer9signatureER21RandomNumberGenerator" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates the signature and returns it</p>
</dd></dl>

<dl class="function">
<dt id="_CPPv2N9PK_Signer12sign_messageEPK4byte6size_tR21RandomNumberGenerator">
<span id="PK_Signer::sign_message__byteCP.s.RandomNumberGeneratorR"></span>SecureVector&lt;byte&gt; <code class="descclassname"></code><code class="descname">sign_message</code><span class="sig-paren">(</span><em class="property">const</em> byte *<em>in</em>, size_t <em>length</em>, RandomNumberGenerator &amp;<em>rng</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N9PK_Signer12sign_messageEPK4byte6size_tR21RandomNumberGenerator" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N9PK_Signer12sign_messageERK12MemoryRegionI4byteER21RandomNumberGenerator">
<span id="PK_Signer::sign_message__MemoryRegion:byte:CR.RandomNumberGeneratorR"></span>SecureVector&lt;byte&gt; <code class="descclassname"></code><code class="descname">sign_message</code><span class="sig-paren">(</span><em class="property">const</em> MemoryRegion&lt;byte&gt; &amp;<em>in</em>, RandomNumberGenerator &amp;<em>rng</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N9PK_Signer12sign_messageERK12MemoryRegionI4byteER21RandomNumberGenerator" title="Permalink to this definition">¶</a></dt>
<dd><p>These functions are equivalent to calling
<a class="reference internal" href="#_CPPv2N9PK_Signer6updateEPK4byte6size_t" title="PK_Signer::update"><code class="xref cpp cpp-func docutils literal"><span class="pre">PK_Signer::update</span></code></a> and then
<a class="reference internal" href="#_CPPv2N9PK_Signer9signatureER21RandomNumberGenerator" title="PK_Signer::signature"><code class="xref cpp cpp-func docutils literal"><span class="pre">PK_Signer::signature</span></code></a>. Any data previously provided
using <code class="docutils literal"><span class="pre">update</span></code> will be included.</p>
</dd></dl>

</dd></dl>

<p>Signatures are verified using</p>
<dl class="class">
<dt id="_CPPv211PK_Verifier">
<span id="PK_Verifier"></span><em class="property">class </em><code class="descclassname"></code><code class="descname">PK_Verifier</code><a class="headerlink" href="#_CPPv211PK_Verifier" title="Permalink to this definition">¶</a></dt>
<dd><dl class="function">
<dt id="_CPPv2N11PK_Verifier11PK_VerifierERK10Public_KeyRKNSt6stringE16Signature_Format">
<span id="PK_Verifier::PK_Verifier__Public_KeyCR.ssCR.Signature_Format"></span><code class="descclassname"></code><code class="descname">PK_Verifier</code><span class="sig-paren">(</span><em class="property">const</em> Public_Key &amp;<em>pub_key</em>, <em class="property">const</em> std::string &amp;<em>emsa</em>, Signature_Format <em>format</em> = IEEE_1363<span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N11PK_Verifier11PK_VerifierERK10Public_KeyRKNSt6stringE16Signature_Format" title="Permalink to this definition">¶</a></dt>
<dd><p>Construct a new verifier for signatures assicated with public
key <em>pub_key</em>. The <em>emsa</em> and <em>format</em> should be the same as
that used by the signer.</p>
</dd></dl>

<dl class="function">
<dt id="_CPPv2N11PK_Verifier6updateEPK4byte6size_t">
<span id="PK_Verifier::update__byteCP.s"></span>void <code class="descclassname"></code><code class="descname">update</code><span class="sig-paren">(</span><em class="property">const</em> byte *<em>in</em>, size_t <em>length</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N11PK_Verifier6updateEPK4byte6size_t" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N11PK_Verifier6updateERK12MemoryRegionI4byteE">
<span id="PK_Verifier::update__MemoryRegion:byte:CR"></span>void <code class="descclassname"></code><code class="descname">update</code><span class="sig-paren">(</span><em class="property">const</em> MemoryRegion&lt;byte&gt; &amp;<em>in</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N11PK_Verifier6updateERK12MemoryRegionI4byteE" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N11PK_Verifier6updateE4byte">
<span id="PK_Verifier::update__byte"></span>void <code class="descclassname"></code><code class="descname">update</code><span class="sig-paren">(</span>byte <em>in</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N11PK_Verifier6updateE4byte" title="Permalink to this definition">¶</a></dt>
<dd><p>Add further message data that is purportedly assocated with the
signature that will be checked.</p>
</dd></dl>

<dl class="function">
<dt id="_CPPv2N11PK_Verifier15check_signatureEPK4byte6size_t">
<span id="PK_Verifier::check_signature__byteCP.s"></span>bool <code class="descclassname"></code><code class="descname">check_signature</code><span class="sig-paren">(</span><em class="property">const</em> byte *<em>sig</em>, size_t <em>length</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N11PK_Verifier15check_signatureEPK4byte6size_t" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N11PK_Verifier15check_signatureERK12MemoryRegionI4byteE">
<span id="PK_Verifier::check_signature__MemoryRegion:byte:CR"></span>bool <code class="descclassname"></code><code class="descname">check_signature</code><span class="sig-paren">(</span><em class="property">const</em> MemoryRegion&lt;byte&gt; &amp;<em>sig</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N11PK_Verifier15check_signatureERK12MemoryRegionI4byteE" title="Permalink to this definition">¶</a></dt>
<dd><p>Check to see if <em>sig</em> is a valid signature for the message data
that was written in. Return true if so. This function clears the
internal message state, so after this call you can call
<a class="reference internal" href="#_CPPv2N11PK_Verifier6updateEPK4byte6size_t" title="PK_Verifier::update"><code class="xref cpp cpp-func docutils literal"><span class="pre">PK_Verifier::update</span></code></a> to start verifying another
message.</p>
</dd></dl>

<dl class="function">
<dt id="_CPPv2N11PK_Verifier14verify_messageEPK4byte6size_tPK4byte6size_t">
<span id="PK_Verifier::verify_message__byteCP.s.byteCP.s"></span>bool <code class="descclassname"></code><code class="descname">verify_message</code><span class="sig-paren">(</span><em class="property">const</em> byte *<em>msg</em>, size_t <em>msg_length</em>, <em class="property">const</em> byte *<em>sig</em>, size_t <em>sig_length</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N11PK_Verifier14verify_messageEPK4byte6size_tPK4byte6size_t" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="function">
<dt id="_CPPv2N11PK_Verifier14verify_messageERK12MemoryRegionI4byteERK12MemoryRegionI4byteE">
<span id="PK_Verifier::verify_message__MemoryRegion:byte:CR.MemoryRegion:byte:CR"></span>bool <code class="descclassname"></code><code class="descname">verify_message</code><span class="sig-paren">(</span><em class="property">const</em> MemoryRegion&lt;byte&gt; &amp;<em>msg</em>, <em class="property">const</em> MemoryRegion&lt;byte&gt; &amp;<em>sig</em><span class="sig-paren">)</span><a class="headerlink" href="#_CPPv2N11PK_Verifier14verify_messageERK12MemoryRegionI4byteERK12MemoryRegionI4byteE" title="Permalink to this definition">¶</a></dt>
<dd><p>These are equivalent to calling <a class="reference internal" href="#_CPPv2N11PK_Verifier6updateEPK4byte6size_t" title="PK_Verifier::update"><code class="xref cpp cpp-func docutils literal"><span class="pre">PK_Verifier::update</span></code></a>
on <em>msg</em> and then calling <a class="reference internal" href="#_CPPv2N11PK_Verifier15check_signatureEPK4byte6size_t" title="PK_Verifier::check_signature"><code class="xref cpp cpp-func docutils literal"><span class="pre">PK_Verifier::check_signature</span></code></a>
on <em>sig</em>.</p>
</dd></dl>

</dd></dl>

<p>Here is an example of DSA signature generation</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;iomanip&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;fstream&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;memory&gt;</span><span class="cp"></span>

<span class="cp">#include</span> <span class="cpf">&lt;botan/botan.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;botan/pubkey.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;botan/dsa.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;botan/base64.h&gt;</span><span class="cp"></span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">Botan</span><span class="p">;</span>

<span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">SUFFIX</span> <span class="o">=</span> <span class="s">&quot;.sig&quot;</span><span class="p">;</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="n">argv</span><span class="p">[])</span>
   <span class="p">{</span>
   <span class="k">if</span><span class="p">(</span><span class="n">argc</span> <span class="o">!=</span> <span class="mi">4</span><span class="p">)</span>
      <span class="p">{</span>
      <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Usage: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;&lt;</span> <span class="s">&quot; keyfile messagefile passphrase&quot;</span>
                <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
      <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
      <span class="p">}</span>

   <span class="n">Botan</span><span class="o">::</span><span class="n">LibraryInitializer</span> <span class="n">init</span><span class="p">;</span>

   <span class="k">try</span> <span class="p">{</span>
      <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">passphrase</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">3</span><span class="p">]);</span>

      <span class="n">std</span><span class="o">::</span><span class="n">ifstream</span> <span class="n">message</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">std</span><span class="o">::</span><span class="n">ios</span><span class="o">::</span><span class="n">binary</span><span class="p">);</span>
      <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">message</span><span class="p">)</span>
         <span class="p">{</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Couldn&#39;t read the message file.&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
         <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
         <span class="p">}</span>

      <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">outfile</span> <span class="o">=</span> <span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="n">SUFFIX</span><span class="p">;</span>
      <span class="n">std</span><span class="o">::</span><span class="n">ofstream</span> <span class="n">sigfile</span><span class="p">(</span><span class="n">outfile</span><span class="p">.</span><span class="n">c_str</span><span class="p">());</span>
      <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">sigfile</span><span class="p">)</span>
         <span class="p">{</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Couldn&#39;t write the signature to &quot;</span>
                   <span class="o">&lt;&lt;</span> <span class="n">outfile</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
         <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
         <span class="p">}</span>

      <span class="n">AutoSeeded_RNG</span> <span class="n">rng</span><span class="p">;</span>

      <span class="n">std</span><span class="o">::</span><span class="n">auto_ptr</span><span class="o">&lt;</span><span class="n">PKCS8_PrivateKey</span><span class="o">&gt;</span> <span class="n">key</span><span class="p">(</span>
         <span class="n">PKCS8</span><span class="o">::</span><span class="n">load_key</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">rng</span><span class="p">,</span> <span class="n">passphrase</span><span class="p">)</span>
         <span class="p">);</span>

      <span class="n">DSA_PrivateKey</span><span class="o">*</span> <span class="n">dsakey</span> <span class="o">=</span> <span class="k">dynamic_cast</span><span class="o">&lt;</span><span class="n">DSA_PrivateKey</span><span class="o">*&gt;</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">get</span><span class="p">());</span>

      <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">dsakey</span><span class="p">)</span>
         <span class="p">{</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;The loaded key is not a DSA key!</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>
         <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
         <span class="p">}</span>

      <span class="n">PK_Signer</span> <span class="n">signer</span><span class="p">(</span><span class="o">*</span><span class="n">dsakey</span><span class="p">,</span> <span class="s">&quot;EMSA1(SHA-1)&quot;</span><span class="p">);</span>

      <span class="n">DataSource_Stream</span> <span class="n">in</span><span class="p">(</span><span class="n">message</span><span class="p">);</span>
      <span class="n">byte</span> <span class="n">buf</span><span class="p">[</span><span class="mi">4096</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> <span class="mi">0</span> <span class="p">};</span>
      <span class="k">while</span><span class="p">(</span><span class="kt">size_t</span> <span class="n">got</span> <span class="o">=</span> <span class="n">in</span><span class="p">.</span><span class="n">read</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">buf</span><span class="p">)))</span>
         <span class="n">signer</span><span class="p">.</span><span class="n">update</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="n">got</span><span class="p">);</span>

      <span class="n">sigfile</span> <span class="o">&lt;&lt;</span> <span class="n">base64_encode</span><span class="p">(</span><span class="n">signer</span><span class="p">.</span><span class="n">signature</span><span class="p">(</span><span class="n">rng</span><span class="p">))</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>
   <span class="p">}</span>
   <span class="k">catch</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">exception</span><span class="o">&amp;</span> <span class="n">e</span><span class="p">)</span>
      <span class="p">{</span>
      <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Exception caught: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">e</span><span class="p">.</span><span class="n">what</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
      <span class="p">}</span>
   <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
   <span class="p">}</span>
</pre></div>
</div>
<p>Here is an example that verifies DSA signatures</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;iomanip&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;fstream&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;cstdlib&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;memory&gt;</span><span class="cp"></span>

<span class="cp">#include</span> <span class="cpf">&lt;botan/botan.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;botan/pubkey.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;botan/dsa.h&gt;</span><span class="cp"></span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">Botan</span><span class="p">;</span>

<span class="k">namespace</span> <span class="p">{</span>

<span class="n">SecureVector</span><span class="o">&lt;</span><span class="n">byte</span><span class="o">&gt;</span> <span class="n">b64_decode</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">in</span><span class="p">)</span>
   <span class="p">{</span>
   <span class="n">Pipe</span> <span class="n">pipe</span><span class="p">(</span><span class="k">new</span> <span class="n">Base64_Decoder</span><span class="p">);</span>
   <span class="n">pipe</span><span class="p">.</span><span class="n">process_msg</span><span class="p">(</span><span class="n">in</span><span class="p">);</span>
   <span class="k">return</span> <span class="n">pipe</span><span class="p">.</span><span class="n">read_all</span><span class="p">();</span>
   <span class="p">}</span>

<span class="p">}</span>

<span class="kt">int</span> <span class="n">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="n">argv</span><span class="p">[])</span>
   <span class="p">{</span>
   <span class="k">if</span><span class="p">(</span><span class="n">argc</span> <span class="o">!=</span> <span class="mi">4</span><span class="p">)</span>
      <span class="p">{</span>
      <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Usage: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="o">&lt;&lt;</span> <span class="s">&quot; keyfile messagefile sigfile&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
      <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
      <span class="p">}</span>


   <span class="k">try</span> <span class="p">{</span>
      <span class="n">Botan</span><span class="o">::</span><span class="n">LibraryInitializer</span> <span class="n">init</span><span class="p">;</span>

      <span class="n">std</span><span class="o">::</span><span class="n">ifstream</span> <span class="n">message</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">std</span><span class="o">::</span><span class="n">ios</span><span class="o">::</span><span class="n">binary</span><span class="p">);</span>
      <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">message</span><span class="p">)</span>
         <span class="p">{</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Couldn&#39;t read the message file.&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
         <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
         <span class="p">}</span>

      <span class="n">std</span><span class="o">::</span><span class="n">ifstream</span> <span class="n">sigfile</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">3</span><span class="p">]);</span>
      <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">sigfile</span><span class="p">)</span>
         <span class="p">{</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Couldn&#39;t read the signature file.&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
         <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
         <span class="p">}</span>

      <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">sigstr</span><span class="p">;</span>
      <span class="n">getline</span><span class="p">(</span><span class="n">sigfile</span><span class="p">,</span> <span class="n">sigstr</span><span class="p">);</span>

      <span class="n">std</span><span class="o">::</span><span class="n">auto_ptr</span><span class="o">&lt;</span><span class="n">X509_PublicKey</span><span class="o">&gt;</span> <span class="n">key</span><span class="p">(</span><span class="n">X509</span><span class="o">::</span><span class="n">load_key</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]));</span>
      <span class="n">DSA_PublicKey</span><span class="o">*</span> <span class="n">dsakey</span> <span class="o">=</span> <span class="k">dynamic_cast</span><span class="o">&lt;</span><span class="n">DSA_PublicKey</span><span class="o">*&gt;</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">get</span><span class="p">());</span>

      <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">dsakey</span><span class="p">)</span>
         <span class="p">{</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;The loaded key is not a DSA key!</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>
         <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
         <span class="p">}</span>

      <span class="n">SecureVector</span><span class="o">&lt;</span><span class="n">byte</span><span class="o">&gt;</span> <span class="n">sig</span> <span class="o">=</span> <span class="n">b64_decode</span><span class="p">(</span><span class="n">sigstr</span><span class="p">);</span>

      <span class="n">PK_Verifier</span> <span class="nf">ver</span><span class="p">(</span><span class="o">*</span><span class="n">dsakey</span><span class="p">,</span> <span class="s">&quot;EMSA1(SHA-1)&quot;)</span><span class="p">;</span>

      <span class="n">DataSource_Stream</span> <span class="nf">in</span><span class="p">(</span><span class="n">message</span><span class="p">);</span>
      <span class="n">byte</span> <span class="n">buf</span><span class="p">[</span><span class="mi">4096</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> <span class="mi">0</span> <span class="p">};</span>
      <span class="k">while</span><span class="p">(</span><span class="kt">size_t</span> <span class="n">got</span> <span class="o">=</span> <span class="n">in</span><span class="p">.</span><span class="n">read</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">buf</span><span class="p">)))</span>
         <span class="n">ver</span><span class="p">.</span><span class="n">update</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="n">got</span><span class="p">);</span>

      <span class="k">const</span> <span class="kt">bool</span> <span class="n">ok</span> <span class="o">=</span> <span class="n">ver</span><span class="p">.</span><span class="n">check_signature</span><span class="p">(</span><span class="n">sig</span><span class="p">);</span>

      <span class="k">if</span><span class="p">(</span><span class="n">ok</span><span class="p">)</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Signature verified</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>
      <span class="k">else</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Signature did NOT verify</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>
   <span class="p">}</span>
   <span class="k">catch</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">exception</span><span class="o">&amp;</span> <span class="n">e</span><span class="p">)</span>
      <span class="p">{</span>
      <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Exception caught: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">e</span><span class="p">.</span><span class="n">what</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
      <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
      <span class="p">}</span>
   <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
   <span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="key-agreement">
<h2>Key Agreement<a class="headerlink" href="#key-agreement" title="Permalink to this headline">¶</a></h2>
<p>You can get a hold of a <code class="docutils literal"><span class="pre">PK_Key_Agreement_Scheme</span></code> object by calling
<code class="docutils literal"><span class="pre">get_pk_kas</span></code> with a key that is of a type that supports key
agreement (such as a Diffie-Hellman key stored in a <code class="docutils literal"><span class="pre">DH_PrivateKey</span></code>
object), and the name of a key derivation function. This can be &#8220;Raw&#8221;,
meaning the output of the primitive itself is returned as the key, or
&#8220;KDF1(hash)&#8221; or &#8220;KDF2(hash)&#8221; where &#8220;hash&#8221; is any string you happen to
like (hopefully you like strings like &#8220;SHA-256&#8221; or &#8220;RIPEMD-160&#8221;), or
&#8220;X9.42-PRF(keywrap)&#8221;, which uses the PRF specified in ANSI X9.42. It
takes the name or OID of the key wrap algorithm that will be used to
encrypt a content encryption key.</p>
<p>How key agreement works is that you trade public values with some
other party, and then each of you runs a computation with the other&#8217;s
value and your key (this should return the same result to both
parties). This computation can be called by using
<code class="docutils literal"><span class="pre">derive_key</span></code> with either a byte array/length pair, or a
<code class="docutils literal"><span class="pre">SecureVector&lt;byte&gt;</span></code> than holds the public value of the other
party. The last argument to either call is a number that specifies how
long a key you want.</p>
<p>Depending on the KDF you&#8217;re using, you <em>might not</em> get back a key
of the size you requested. In particular &#8220;Raw&#8221; will return a number
about the size of the Diffie-Hellman modulus, and KDF1 can only return
a key that is the same size as the output of the hash. KDF2, on the
other hand, will always give you a key exactly as long as you request,
regardless of the underlying hash used with it. The key returned is a
<code class="docutils literal"><span class="pre">SymmetricKey</span></code>, ready to pass to a block cipher, MAC, or other
symmetric algorithm.</p>
<p>The public value that should be used can be obtained by calling
<code class="docutils literal"><span class="pre">public_data</span></code>, which exists for any key that is associated with a
key agreement algorithm. It returns a <code class="docutils literal"><span class="pre">SecureVector&lt;byte&gt;</span></code>.</p>
<p>&#8220;KDF2(SHA-256)&#8221; is by far the preferred algorithm for key derivation
in new applications. The X9.42 algorithm may be useful in some
circumstances, but unless you need X9.42 compatibility, KDF2 is easier
to use.</p>
<p>An example of using Diffie-Hellman:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;botan/botan.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;botan/dh.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;botan/pubkey.h&gt;</span><span class="cp"></span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">Botan</span><span class="p">;</span>

<span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;memory&gt;</span><span class="cp"></span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
   <span class="p">{</span>
   <span class="k">try</span>
      <span class="p">{</span>
      <span class="n">LibraryInitializer</span> <span class="n">init</span><span class="p">;</span>

      <span class="n">AutoSeeded_RNG</span> <span class="n">rng</span><span class="p">;</span>

      <span class="c1">// Alice and Bob agree on a DH domain to use</span>
      <span class="n">DL_Group</span> <span class="n">shared_domain</span><span class="p">(</span><span class="s">&quot;modp/ietf/2048&quot;</span><span class="p">);</span>

      <span class="c1">// Alice creates a DH key</span>
      <span class="n">DH_PrivateKey</span> <span class="n">private_a</span><span class="p">(</span><span class="n">rng</span><span class="p">,</span> <span class="n">shared_domain</span><span class="p">);</span>

      <span class="c1">// Bob creates a key with a matching group</span>
      <span class="n">DH_PrivateKey</span> <span class="n">private_b</span><span class="p">(</span><span class="n">rng</span><span class="p">,</span> <span class="n">shared_domain</span><span class="p">);</span>

      <span class="c1">// Alice sends to Bob her public key and a session parameter</span>
      <span class="n">MemoryVector</span><span class="o">&lt;</span><span class="n">byte</span><span class="o">&gt;</span> <span class="n">public_a</span> <span class="o">=</span> <span class="n">private_a</span><span class="p">.</span><span class="n">public_value</span><span class="p">();</span>
      <span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">session_param</span> <span class="o">=</span>
         <span class="s">&quot;Alice and Bob&#39;s shared session parameter&quot;</span><span class="p">;</span>

      <span class="c1">// Bob sends his public key to Alice</span>
      <span class="n">MemoryVector</span><span class="o">&lt;</span><span class="n">byte</span><span class="o">&gt;</span> <span class="n">public_b</span> <span class="o">=</span> <span class="n">private_b</span><span class="p">.</span><span class="n">public_value</span><span class="p">();</span>

      <span class="c1">// Now Alice performs the key agreement operation</span>
      <span class="n">PK_Key_Agreement</span> <span class="n">ka_alice</span><span class="p">(</span><span class="n">private_a</span><span class="p">,</span> <span class="s">&quot;KDF2(SHA-256)&quot;</span><span class="p">);</span>
      <span class="n">SymmetricKey</span> <span class="n">alice_key</span> <span class="o">=</span> <span class="n">ka_alice</span><span class="p">.</span><span class="n">derive_key</span><span class="p">(</span><span class="mi">32</span><span class="p">,</span> <span class="n">public_b</span><span class="p">,</span> <span class="n">session_param</span><span class="p">);</span>

      <span class="c1">// Bob does the same:</span>
      <span class="n">PK_Key_Agreement</span> <span class="n">ka_bob</span><span class="p">(</span><span class="n">private_b</span><span class="p">,</span> <span class="s">&quot;KDF2(SHA-256)&quot;</span><span class="p">);</span>
      <span class="n">SymmetricKey</span> <span class="n">bob_key</span> <span class="o">=</span> <span class="n">ka_bob</span><span class="p">.</span><span class="n">derive_key</span><span class="p">(</span><span class="mi">32</span><span class="p">,</span> <span class="n">public_a</span><span class="p">,</span> <span class="n">session_param</span><span class="p">);</span>

      <span class="k">if</span><span class="p">(</span><span class="n">alice_key</span> <span class="o">==</span> <span class="n">bob_key</span><span class="p">)</span>
         <span class="p">{</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;The two keys matched, everything worked</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;The shared key was: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">alice_key</span><span class="p">.</span><span class="n">as_string</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>
         <span class="p">}</span>
      <span class="k">else</span>
         <span class="p">{</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;The two keys didn&#39;t match! Hmmm...</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Alice&#39;s key was: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">alice_key</span><span class="p">.</span><span class="n">as_string</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>
         <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Bob&#39;s key was: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">bob_key</span><span class="p">.</span><span class="n">as_string</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span>
         <span class="p">}</span>

      <span class="c1">// Now use the shared key for encryption or MACing or whatever</span>
   <span class="p">}</span>
   <span class="k">catch</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">exception</span><span class="o">&amp;</span> <span class="n">e</span><span class="p">)</span>
      <span class="p">{</span>
      <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">e</span><span class="p">.</span><span class="n">what</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
      <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
      <span class="p">}</span>
   <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
   <span class="p">}</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
        </div>
        <div class="sidebar">
          <h3>Table Of Contents</h3>
          <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="index.html">Welcome</a></li>
<li class="toctree-l1"><a class="reference internal" href="reading.html">Recommended Reading</a></li>
<li class="toctree-l1"><a class="reference internal" href="building.html">Building The Library</a></li>
<li class="toctree-l1"><a class="reference internal" href="firststep.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="filters.html">Information Flow: Pipes and Filters</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Public Key Cryptography</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#key-objects">Key Objects</a></li>
<li class="toctree-l2"><a class="reference internal" href="#creating-new-private-keys">Creating New Private Keys</a></li>
<li class="toctree-l2"><a class="reference internal" href="#serializing-private-keys-using-pkcs-8">Serializing Private Keys Using PKCS #8</a></li>
<li class="toctree-l2"><a class="reference internal" href="#key-checking">Key Checking</a></li>
<li class="toctree-l2"><a class="reference internal" href="#encryption">Encryption</a></li>
<li class="toctree-l2"><a class="reference internal" href="#signatures">Signatures</a></li>
<li class="toctree-l2"><a class="reference internal" href="#key-agreement">Key Agreement</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="x509.html">Certificate Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="ssl.html">SSL and TLS</a></li>
<li class="toctree-l1"><a class="reference internal" href="bigint.html">BigInt</a></li>
<li class="toctree-l1"><a class="reference internal" href="lowlevel.html">The Low-Level Interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="secmem.html">Secure Memory Containers</a></li>
<li class="toctree-l1"><a class="reference internal" href="kdf.html">Key Derivation Functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="pbkdf.html">PBKDF Algorithms</a></li>
<li class="toctree-l1"><a class="reference internal" href="passhash.html">Password Hashing</a></li>
<li class="toctree-l1"><a class="reference internal" href="rng.html">Random Number Generators</a></li>
<li class="toctree-l1"><a class="reference internal" href="fpe.html">Format Preserving Encryption</a></li>
<li class="toctree-l1"><a class="reference internal" href="python.html">Python Binding</a></li>
</ul>

          <div role="search">
            <h3 style="margin-top: 1.5em;">Search</h3>
            <form class="search" action="search.html" method="get">
                <input type="text" name="q" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
            </form>
          </div>
        </div>
        <div class="clearer"></div>
      </div>
    </div>

    <div class="footer-wrapper">
      <div class="footer">
        <div class="left">
          <div role="navigation" aria-label="related navigaton">
            <a href="filters.html" title="Information Flow: Pipes and Filters"
              accesskey="P">previous</a> |
            <a href="x509.html" title="Certificate Handling"
              accesskey="N">next</a> |
            <a href="genindex.html" title="General Index"
              accesskey="I">index</a>
          </div>
          <div role="note" aria-label="source link">
              <br/>
              <a href="_sources/pubkey.txt"
                rel="nofollow">Show Source</a>
          </div>
        </div>

        <div class="right">
          
    <div class="footer" role="contentinfo">
        &#169; Copyright 2000-2011, Jack Lloyd.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.9.
    </div>
        </div>
        <div class="clearer"></div>
      </div>
    </div>

  </body>
</html>