⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sapphire_8cpp-source.html

📁 Crypto++是一个非常强大的密码学库,主要是功能全
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Crypto++: sapphire.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.2 --><div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Compound&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Compound&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a></div><h1>sapphire.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// sapphire.cpp -- modified by Wei Dai from:</span>00002 00003 <span class="comment">/* sapphire.cpp -- the Saphire II stream cipher class.</span>00004 <span class="comment">   Dedicated to the Public Domain the author and inventor:</span>00005 <span class="comment">   (Michael Paul Johnson).  This code comes with no warranty.</span>00006 <span class="comment">   Use it at your own risk.</span>00007 <span class="comment">   Ported from the Pascal implementation of the Sapphire Stream</span>00008 <span class="comment">   Cipher 9 December 1994.</span>00009 <span class="comment">   Added hash pre- and post-processing 27 December 1994.</span>00010 <span class="comment">   Modified initialization to make index variables key dependent,</span>00011 <span class="comment">   made the output function more resistant to cryptanalysis,</span>00012 <span class="comment">   and renamed to Sapphire II 2 January 1995</span>00013 <span class="comment">*/</span>00014 00015 <span class="preprocessor">#include "pch.h"</span>00016 <span class="preprocessor">#include "sapphire.h"</span>00017 00018 NAMESPACE_BEGIN(CryptoPP)00019 00020 byte SapphireBase::keyrand(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> limit,00021                                                    <span class="keyword">const</span> byte *user_key,00022                                                    byte keysize,00023                                                    byte *rsum,00024                                                    <span class="keywordtype">unsigned</span> *keypos)00025 {00026         <span class="keywordtype">unsigned</span> u,             <span class="comment">// Value from 0 to limit to return.</span>00027                 retry_limiter,      <span class="comment">// No infinite loops allowed.</span>00028                 mask;               <span class="comment">// Select just enough bits.</span>00029 00030         retry_limiter = 0;00031         mask = 1;               <span class="comment">// Fill mask with enough bits to cover</span>00032         <span class="keywordflow">while</span> (mask &lt; limit)    <span class="comment">// the desired range.</span>00033                 mask = (mask &lt;&lt; 1) + 1;00034         <span class="keywordflow">do</span>00035                 {00036                 *rsum = cards[*rsum] + user_key[(*keypos)++];00037                 <span class="keywordflow">if</span> (*keypos &gt;= keysize)00038                         {00039                         *keypos = 0;            <span class="comment">// Recycle the user key.</span>00040                         *rsum += keysize;   <span class="comment">// key "aaaa" != key "aaaaaaaa"</span>00041                         }00042                 u = mask &amp; *rsum;00043                 <span class="keywordflow">if</span> (++retry_limiter &gt; 11)00044                         u %= limit;     <span class="comment">// Prevent very rare long loops.</span>00045                 }00046         <span class="keywordflow">while</span> (u &gt; limit);00047         <span class="keywordflow">return</span> u;00048 }00049 00050 SapphireBase::SapphireBase()00051         : cards(256)00052 {00053 }00054 00055 SapphireBase::SapphireBase(<span class="keyword">const</span> byte *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> keysize)00056         : cards(256)00057 {00058         assert(keysize &lt; 256);00059         <span class="comment">// Key size may be up to 256 bytes.</span>00060         <span class="comment">// Pass phrases may be used directly, with longer length</span>00061         <span class="comment">// compensating for the low entropy expected in such keys.</span>00062         <span class="comment">// Alternatively, shorter keys hashed from a pass phrase or</span>00063         <span class="comment">// generated randomly may be used. For random keys, lengths</span>00064         <span class="comment">// of from 4 to 16 bytes are recommended, depending on how</span>00065         <span class="comment">// secure you want this to be.</span>00066 00067         <span class="keywordtype">int</span> i;00068         byte rsum;00069         <span class="keywordtype">unsigned</span> keypos;00070 00071         <span class="comment">// Start with cards all in order, one of each.</span>00072 00073         <span class="keywordflow">for</span> (i=0;i&lt;256;i++)00074                 cards[i] = i;00075 00076         <span class="comment">// Swap the card at each position with some other card.</span>00077 00078         keypos = 0;         <span class="comment">// Start with first byte of user key.</span>00079         rsum = 0;00080         <span class="keywordflow">for</span> (i=255;i;i--)00081                 std::swap(cards[i], cards[keyrand(i, key, keysize, &amp;rsum, &amp;keypos)]);00082 00083         <span class="comment">// Initialize the indices and data dependencies.</span>00084         <span class="comment">// Indices are set to different values instead of all 0</span>00085         <span class="comment">// to reduce what is known about the state of the cards</span>00086         <span class="comment">// when the first byte is emitted.</span>00087 00088         rotor = cards[1];00089         ratchet = cards[3];00090         avalanche = cards[5];00091         last_plain = cards[7];00092         last_cipher = cards[rsum];00093 00094         rsum = 0;00095         keypos = 0;00096 }00097 00098 SapphireBase::~SapphireBase()00099 {00100         rotor = ratchet = avalanche = last_plain = last_cipher = 0;00101 }00102 <a name="l00103"></a><a class="code" href="class_sapphire_encryption.html#_sapphire_r_n_gd1">00103</a> <span class="keywordtype">void</span> <a class="code" href="class_sapphire_encryption.html#_sapphire_r_n_gd1">SapphireEncryption::ProcessString</a>(byte *outString, <span class="keyword">const</span> byte *inString, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00104 {00105         <span class="keywordflow">while</span>(length--)00106                 *outString++ = <a class="code" href="class_sapphire_encryption.html#_sapphire_r_n_gd0">SapphireEncryption::ProcessByte</a>(*inString++);00107 }00108 <a name="l00109"></a><a class="code" href="class_sapphire_encryption.html#_sapphire_r_n_gd2">00109</a> <span class="keywordtype">void</span> <a class="code" href="class_sapphire_encryption.html#_sapphire_r_n_gd1">SapphireEncryption::ProcessString</a>(byte *inoutString, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00110 {00111         <span class="keywordflow">while</span>(length--)00112         {00113                 *inoutString = <a class="code" href="class_sapphire_encryption.html#_sapphire_r_n_gd0">SapphireEncryption::ProcessByte</a>(*inoutString);00114                 inoutString++;00115         }00116 }00117 <a name="l00118"></a><a class="code" href="class_sapphire_decryption.html#_sapphire_decryptiona2">00118</a> <span class="keywordtype">void</span> <a class="code" href="class_sapphire_decryption.html#_sapphire_decryptiona2">SapphireDecryption::ProcessString</a>(byte *outString, <span class="keyword">const</span> byte *inString, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00119 {00120         <span class="keywordflow">while</span>(length--)00121                 *outString++ = <a class="code" href="class_sapphire_decryption.html#_sapphire_decryptiona1">SapphireDecryption::ProcessByte</a>(*inString++);00122 }00123 <a name="l00124"></a><a class="code" href="class_sapphire_decryption.html#_sapphire_decryptiona3">00124</a> <span class="keywordtype">void</span> <a class="code" href="class_sapphire_decryption.html#_sapphire_decryptiona2">SapphireDecryption::ProcessString</a>(byte *inoutString, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00125 {00126         <span class="keywordflow">while</span>(length--)00127         {00128                 *inoutString = <a class="code" href="class_sapphire_decryption.html#_sapphire_decryptiona1">SapphireDecryption::ProcessByte</a>(*inoutString);00129                 inoutString++;00130         }00131 }00132 00133 SapphireHash::SapphireHash(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> hashLength)00134         : hashLength(hashLength)00135 {00136         Init();00137 }00138 00139 <span class="keywordtype">void</span> SapphireHash::Init()00140 {00141         <span class="comment">// This function is used to initialize non-keyed hash</span>00142         <span class="comment">// computation.</span>00143 00144         <span class="keywordtype">int</span> i, j;00145 00146         <span class="comment">// Initialize the indices and data dependencies.</span>00147 00148         rotor = 1;00149         ratchet = 3;00150         avalanche = 5;00151         last_plain = 7;00152         last_cipher = 11;00153 00154         <span class="comment">// Start with cards all in inverse order.</span>00155 00156         <span class="keywordflow">for</span> (i=0, j=255;i&lt;256;i++,j--)00157                 cards[i] = (byte) j;00158 }00159 <a name="l00160"></a><a class="code" href="class_sapphire_hash.html#_sapphire_hasha1">00160</a> <span class="keywordtype">void</span> <a class="code" href="class_sapphire_hash.html#_sapphire_hasha1">SapphireHash::Update</a>(<span class="keyword">const</span> byte *input, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00161 {00162         <span class="keywordflow">while</span>(length--)00163                 <a class="code" href="class_sapphire_encryption.html#_sapphire_r_n_gd0">SapphireEncryption::ProcessByte</a>(*input++);00164 }00165 <a name="l00166"></a><a class="code" href="class_sapphire_hash.html#_sapphire_hasha2">00166</a> <span class="keywordtype">void</span> <a class="code" href="class_sapphire_hash.html#_sapphire_hasha2">SapphireHash::TruncatedFinal</a>(byte *hash, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> size)00167 {00168         ThrowIfInvalidTruncatedSize(size);00169 00170         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=255; i&gt;=0; i--)00171                 <a class="code" href="class_sapphire_encryption.html#_sapphire_r_n_gd0">ProcessByte</a>((byte) i);00172 00173         <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> j=0; j&lt;size; j++)00174                 hash[j] = <a class="code" href="class_sapphire_encryption.html#_sapphire_r_n_gd0">ProcessByte</a>(0);00175 00176         Init();00177 }00178 00179 NAMESPACE_END</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:24 2003 for Crypto++ by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 > </a>1.3.2 </small></address></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -