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

📄 des_8cpp-source.html

📁 著名的密码库Crypto++的文档 C++语言的杰作。程序员必备。
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<a name="l00298"></a>00298                                 <span class="comment">/* mask it in if it's there */</span><a name="l00299"></a>00299                                 l= j % 6;<a name="l00300"></a>00300                                 ks[j/6] |= bytebit[l] &gt;&gt; 2;<a name="l00301"></a>00301                         }<a name="l00302"></a>00302                 }<a name="l00303"></a>00303                 <span class="comment">/* Now convert to odd/even interleaved form for use in F */</span><a name="l00304"></a>00304                 <a class="code" href="class_raw_d_e_s.html#d0c0e07e75be147837e11846785f643b">k</a>[2*i] = ((word32)ks[0] &lt;&lt; 24)<a name="l00305"></a>00305                         | ((word32)ks[2] &lt;&lt; 16)<a name="l00306"></a>00306                         | ((word32)ks[4] &lt;&lt; 8)<a name="l00307"></a>00307                         | ((word32)ks[6]);<a name="l00308"></a>00308                 <a class="code" href="class_raw_d_e_s.html#d0c0e07e75be147837e11846785f643b">k</a>[2*i+1] = ((word32)ks[1] &lt;&lt; 24)<a name="l00309"></a>00309                         | ((word32)ks[3] &lt;&lt; 16)<a name="l00310"></a>00310                         | ((word32)ks[5] &lt;&lt; 8)<a name="l00311"></a>00311                         | ((word32)ks[7]);<a name="l00312"></a>00312         }<a name="l00313"></a>00313         <a name="l00314"></a>00314         <span class="keywordflow">if</span> (dir==<a class="code" href="cryptlib_8h.html#353ccabf5ddc119a6a33e92f7b9961c7b5f88847da182e7a1a8af64f4e18f048">DECRYPTION</a>)     <span class="comment">// reverse key schedule order</span><a name="l00315"></a>00315                 <span class="keywordflow">for</span> (i=0; i&lt;16; i+=2)<a name="l00316"></a>00316                 {<a name="l00317"></a>00317                         <a class="code" href="gf2n_8h.html#cd9c045f0b5c2a7595a8a0872dc80f59">std::swap</a>(<a class="code" href="class_raw_d_e_s.html#d0c0e07e75be147837e11846785f643b">k</a>[i], <a class="code" href="class_raw_d_e_s.html#d0c0e07e75be147837e11846785f643b">k</a>[32-2-i]);<a name="l00318"></a>00318                         <a class="code" href="gf2n_8h.html#cd9c045f0b5c2a7595a8a0872dc80f59">std::swap</a>(<a class="code" href="class_raw_d_e_s.html#d0c0e07e75be147837e11846785f643b">k</a>[i+1], <a class="code" href="class_raw_d_e_s.html#d0c0e07e75be147837e11846785f643b">k</a>[32-1-i]);<a name="l00319"></a>00319                 }<a name="l00320"></a>00320 }<a name="l00321"></a>00321 <a name="l00322"></a><a class="code" href="class_raw_d_e_s.html#cc5b68f4b6f6747b89dbcf93d61ce22d">00322</a> <span class="keywordtype">void</span> <a class="code" href="class_raw_d_e_s.html#cc5b68f4b6f6747b89dbcf93d61ce22d">RawDES::RawProcessBlock</a>(word32 &amp;l_, word32 &amp;r_)<span class="keyword"> const</span><a name="l00323"></a>00323 <span class="keyword"></span>{<a name="l00324"></a>00324         word32 l = l_, r = r_;<a name="l00325"></a>00325         <span class="keyword">const</span> word32 *kptr=<a class="code" href="class_raw_d_e_s.html#d0c0e07e75be147837e11846785f643b">k</a>;<a name="l00326"></a>00326 <a name="l00327"></a>00327         <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i&lt;8; i++)<a name="l00328"></a>00328         {<a name="l00329"></a>00329                 word32 work = rotrFixed(r, 4U) ^ kptr[4*i+0];<a name="l00330"></a>00330                 l ^= <a class="code" href="class_raw_d_e_s.html#230d29d184a38e4ce4006ae8863dc441">Spbox</a>[6][(work) &amp; 0x3f]<a name="l00331"></a>00331                   ^  <a class="code" href="class_raw_d_e_s.html#230d29d184a38e4ce4006ae8863dc441">Spbox</a>[4][(work &gt;&gt; 8) &amp; 0x3f]<a name="l00332"></a>00332                   ^  <a class="code" href="class_raw_d_e_s.html#230d29d184a38e4ce4006ae8863dc441">Spbox</a>[2][(work &gt;&gt; 16) &amp; 0x3f]<a name="l00333"></a>00333                   ^  <a class="code" href="class_raw_d_e_s.html#230d29d184a38e4ce4006ae8863dc441">Spbox</a>[0][(work &gt;&gt; 24) &amp; 0x3f];<a name="l00334"></a>00334                 work = r ^ kptr[4*i+1];<a name="l00335"></a>00335                 l ^= <a class="code" href="class_raw_d_e_s.html#230d29d184a38e4ce4006ae8863dc441">Spbox</a>[7][(work) &amp; 0x3f]<a name="l00336"></a>00336                   ^  <a class="code" href="class_raw_d_e_s.html#230d29d184a38e4ce4006ae8863dc441">Spbox</a>[5][(work &gt;&gt; 8) &amp; 0x3f]<a name="l00337"></a>00337                   ^  <a class="code" href="class_raw_d_e_s.html#230d29d184a38e4ce4006ae8863dc441">Spbox</a>[3][(work &gt;&gt; 16) &amp; 0x3f]<a name="l00338"></a>00338                   ^  <a class="code" href="class_raw_d_e_s.html#230d29d184a38e4ce4006ae8863dc441">Spbox</a>[1][(work &gt;&gt; 24) &amp; 0x3f];<a name="l00339"></a>00339 <a name="l00340"></a>00340                 work = rotrFixed(l, 4U) ^ kptr[4*i+2];<a name="l00341"></a>00341                 r ^= <a class="code" href="class_raw_d_e_s.html#230d29d184a38e4ce4006ae8863dc441">Spbox</a>[6][(work) &amp; 0x3f]<a name="l00342"></a>00342                   ^  <a class="code" href="class_raw_d_e_s.html#230d29d184a38e4ce4006ae8863dc441">Spbox</a>[4][(work &gt;&gt; 8) &amp; 0x3f]<a name="l00343"></a>00343                   ^  <a class="code" href="class_raw_d_e_s.html#230d29d184a38e4ce4006ae8863dc441">Spbox</a>[2][(work &gt;&gt; 16) &amp; 0x3f]<a name="l00344"></a>00344                   ^  <a class="code" href="class_raw_d_e_s.html#230d29d184a38e4ce4006ae8863dc441">Spbox</a>[0][(work &gt;&gt; 24) &amp; 0x3f];<a name="l00345"></a>00345                 work = l ^ kptr[4*i+3];<a name="l00346"></a>00346                 r ^= Spbox[7][(work) &amp; 0x3f]<a name="l00347"></a>00347                   ^  Spbox[5][(work &gt;&gt; 8) &amp; 0x3f]<a name="l00348"></a>00348                   ^  Spbox[3][(work &gt;&gt; 16) &amp; 0x3f]<a name="l00349"></a>00349                   ^  Spbox[1][(work &gt;&gt; 24) &amp; 0x3f];<a name="l00350"></a>00350         }<a name="l00351"></a>00351 <a name="l00352"></a>00352         l_ = l; r_ = r;<a name="l00353"></a>00353 }<a name="l00354"></a>00354 <a name="l00355"></a>00355 <span class="keywordtype">void</span> DES_EDE2::Base::UncheckedSetKey(<span class="keyword">const</span> byte *userKey, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length, <span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html" title="interface for retrieving values given their names">NameValuePairs</a> &amp;)<a name="l00356"></a>00356 {<a name="l00357"></a>00357         AssertValidKeyLength(length);<a name="l00358"></a>00358 <a name="l00359"></a>00359         m_des1.RawSetKey(GetCipherDirection(), userKey);<a name="l00360"></a>00360         m_des2.RawSetKey(ReverseCipherDir(GetCipherDirection()), userKey+8);<a name="l00361"></a>00361 }<a name="l00362"></a>00362 <a name="l00363"></a>00363 <span class="keywordtype">void</span> DES_EDE2::Base::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span><a name="l00364"></a>00364 <span class="keyword"></span>{<a name="l00365"></a>00365         word32 l,r;<a name="l00366"></a>00366         <a class="code" href="struct_block_get_and_put.html#07781cc0817613fe47e8070999cf4b95">Block::Get</a>(inBlock)(l)(r);<a name="l00367"></a>00367         IPERM(l,r);<a name="l00368"></a>00368         m_des1.RawProcessBlock(l, r);<a name="l00369"></a>00369         m_des2.RawProcessBlock(r, l);<a name="l00370"></a>00370         m_des1.RawProcessBlock(l, r);<a name="l00371"></a>00371         FPERM(l,r);<a name="l00372"></a>00372         <a class="code" href="struct_block_get_and_put.html#63d96a17e8118407e02896d0b0f8d92d">Block::Put</a>(xorBlock, outBlock)(r)(l);<a name="l00373"></a>00373 }<a name="l00374"></a>00374 <a name="l00375"></a>00375 <span class="keywordtype">void</span> DES_EDE3::Base::UncheckedSetKey(<span class="keyword">const</span> byte *userKey, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length, <span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html" title="interface for retrieving values given their names">NameValuePairs</a> &amp;)<a name="l00376"></a>00376 {<a name="l00377"></a>00377         AssertValidKeyLength(length);<a name="l00378"></a>00378 <a name="l00379"></a>00379         m_des1.RawSetKey(GetCipherDirection(), userKey + (IsForwardTransformation() ? 0 : 16));<a name="l00380"></a>00380         m_des2.RawSetKey(ReverseCipherDir(GetCipherDirection()), userKey + 8);<a name="l00381"></a>00381         m_des3.RawSetKey(GetCipherDirection(), userKey + (IsForwardTransformation() ? 16 : 0));<a name="l00382"></a>00382 }<a name="l00383"></a>00383 <a name="l00384"></a>00384 <span class="keywordtype">void</span> DES_EDE3::Base::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span><a name="l00385"></a>00385 <span class="keyword"></span>{<a name="l00386"></a>00386         word32 l,r;<a name="l00387"></a>00387         <a class="code" href="struct_block_get_and_put.html#07781cc0817613fe47e8070999cf4b95">Block::Get</a>(inBlock)(l)(r);<a name="l00388"></a>00388         IPERM(l,r);<a name="l00389"></a>00389         m_des1.RawProcessBlock(l, r);<a name="l00390"></a>00390         m_des2.RawProcessBlock(r, l);<a name="l00391"></a>00391         m_des3.RawProcessBlock(l, r);<a name="l00392"></a>00392         FPERM(l,r);<a name="l00393"></a>00393         <a class="code" href="struct_block_get_and_put.html#63d96a17e8118407e02896d0b0f8d92d">Block::Put</a>(xorBlock, outBlock)(r)(l);<a name="l00394"></a>00394 }<a name="l00395"></a>00395 <a name="l00396"></a>00396 <span class="preprocessor">#endif  // #ifndef CRYPTOPP_IMPORTS</span><a name="l00397"></a>00397 <span class="preprocessor"></span><a name="l00398"></a>00398 <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">bool</span> CheckParity(byte b)<a name="l00399"></a>00399 {<a name="l00400"></a>00400         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> a = b ^ (b &gt;&gt; 4);<a name="l00401"></a>00401         <span class="keywordflow">return</span> ((a ^ (a&gt;&gt;1) ^ (a&gt;&gt;2) ^ (a&gt;&gt;3)) &amp; 1) == 1;<a name="l00402"></a>00402 }<a name="l00403"></a>00403 <a name="l00404"></a><a class="code" href="class_d_e_s.html#6f490a975dee8704a9c68771ce746335">00404</a> <span class="keywordtype">bool</span> <a class="code" href="class_d_e_s.html#6f490a975dee8704a9c68771ce746335" title="check DES key parity bits">DES::CheckKeyParityBits</a>(<span class="keyword">const</span> byte *key)<a name="l00405"></a>00405 {<a name="l00406"></a>00406         <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i&lt;8; i++)<a name="l00407"></a>00407                 <span class="keywordflow">if</span> (!CheckParity(key[i]))<a name="l00408"></a>00408                         <span class="keywordflow">return</span> <span class="keyword">false</span>;<a name="l00409"></a>00409         <span class="keywordflow">return</span> <span class="keyword">true</span>;<a name="l00410"></a>00410 }<a name="l00411"></a>00411 <a name="l00412"></a><a class="code" href="class_d_e_s.html#b4b48b141706addb33c955c694dd9ead">00412</a> <span class="keywordtype">void</span> <a class="code" href="class_d_e_s.html#b4b48b141706addb33c955c694dd9ead" title="correct DES key parity bits">DES::CorrectKeyParityBits</a>(byte *key)<a name="l00413"></a>00413 {<a name="l00414"></a>00414         <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i&lt;8; i++)<a name="l00415"></a>00415                 <span class="keywordflow">if</span> (!CheckParity(key[i]))<a name="l00416"></a>00416                         key[i] ^= 1;<a name="l00417"></a>00417 }<a name="l00418"></a>00418 <a name="l00419"></a>00419 <span class="comment">// Encrypt or decrypt a block of data in ECB mode</span><a name="l00420"></a>00420 <span class="keywordtype">void</span> DES::Base::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span><a name="l00421"></a>00421 <span class="keyword"></span>{<a name="l00422"></a>00422         word32 l,r;<a name="l00423"></a>00423         <a class="code" href="struct_block_get_and_put.html#07781cc0817613fe47e8070999cf4b95">Block::Get</a>(inBlock)(l)(r);<a name="l00424"></a>00424         IPERM(l,r);<a name="l00425"></a>00425         RawProcessBlock(l, r);<a name="l00426"></a>00426         FPERM(l,r);<a name="l00427"></a>00427         <a class="code" href="struct_block_get_and_put.html#63d96a17e8118407e02896d0b0f8d92d">Block::Put</a>(xorBlock, outBlock)(r)(l);<a name="l00428"></a>00428 }<a name="l00429"></a>00429 <a name="l00430"></a>00430 <span class="keywordtype">void</span> DES_XEX3::Base::UncheckedSetKey(<span class="keyword">const</span> byte *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length, <span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html" title="interface for retrieving values given their names">NameValuePairs</a> &amp;)<a name="l00431"></a>00431 {<a name="l00432"></a>00432         AssertValidKeyLength(length);<a name="l00433"></a>00433 <a name="l00434"></a>00434         <span class="keywordflow">if</span> (!m_des.get())<a name="l00435"></a>00435                 m_des.reset(<span class="keyword">new</span> <a class="code" href="class_block_cipher_final.html" title="_">DES::Encryption</a>);<a name="l00436"></a>00436 <a name="l00437"></a>00437         memcpy(m_x1, key + (IsForwardTransformation() ? 0 : 16), <a class="code" href="class_fixed_block_size.html#2b051f369471fbb83d9b61a90b06e99f">BLOCKSIZE</a>);<a name="l00438"></a>00438         m_des-&gt;RawSetKey(GetCipherDirection(), key + 8);<a name="l00439"></a>00439         memcpy(m_x3, key + (IsForwardTransformation() ? 16 : 0), <a class="code" href="class_fixed_block_size.html#2b051f369471fbb83d9b61a90b06e99f">BLOCKSIZE</a>);<a name="l00440"></a>00440 }<a name="l00441"></a>00441 <a name="l00442"></a>00442 <span class="keywordtype">void</span> DES_XEX3::Base::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span><a name="l00443"></a>00443 <span class="keyword"></span>{<a name="l00444"></a>00444         xorbuf(outBlock, inBlock, m_x1, <a class="code" href="class_fixed_block_size.html#2b051f369471fbb83d9b61a90b06e99f">BLOCKSIZE</a>);<a name="l00445"></a>00445         m_des-&gt;ProcessAndXorBlock(outBlock, xorBlock, outBlock);<a name="l00446"></a>00446         xorbuf(outBlock, m_x3, <a class="code" href="class_fixed_block_size.html#2b051f369471fbb83d9b61a90b06e99f">BLOCKSIZE</a>);<a name="l00447"></a>00447 }<a name="l00448"></a>00448 <a name="l00449"></a>00449 NAMESPACE_END</pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Fri Jun 1 11:11:20 2007 for Crypto++ by&nbsp;<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.2 </small></address></body></html>

⌨️ 快捷键说明

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