📄 blowfish_8cpp-source.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++: blowfish.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 Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Compound List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="namespacemembers.html">Namespace Members</a> | <a class="qindex" href="functions.html">Compound Members</a> | <a class="qindex" href="globals.html">File Members</a></div><h1>blowfish.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// blowfish.cpp - written and placed in the public domain by Wei Dai</span>00002 00003 <span class="preprocessor">#include "pch.h"</span>00004 <span class="preprocessor">#include "<a class="code" href="blowfish_8h.html">blowfish.h</a>"</span>00005 <span class="preprocessor">#include "misc.h"</span>00006 00007 NAMESPACE_BEGIN(CryptoPP)00008 00009 <span class="keywordtype">void</span> Blowfish::Base::UncheckedSetKey(CipherDir dir, <span class="keyword">const</span> byte *key_string, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> keylength)00010 {00011 AssertValidKeyLength(keylength);00012 00013 <span class="keywordtype">unsigned</span> i, j=0, k;00014 word32 data, dspace[2] = {0, 0};00015 00016 memcpy(pbox, p_init, <span class="keyword">sizeof</span>(p_init));00017 memcpy(sbox, s_init, <span class="keyword">sizeof</span>(s_init));00018 00019 <span class="comment">// Xor key string into encryption key vector</span>00020 <span class="keywordflow">for</span> (i=0 ; i<ROUNDS+2 ; ++i)00021 {00022 data = 0 ;00023 <span class="keywordflow">for</span> (k=0 ; k<4 ; ++k )00024 data = (data << 8) | key_string[j++ % keylength];00025 pbox[i] ^= data;00026 }00027 00028 crypt_block(dspace, pbox);00029 00030 <span class="keywordflow">for</span> (i=0; i<ROUNDS; i+=2)00031 crypt_block(pbox+i, pbox+i+2);00032 00033 crypt_block(pbox+ROUNDS, sbox);00034 00035 <span class="keywordflow">for</span> (i=0; i<4*256-2; i+=2)00036 crypt_block(sbox+i, sbox+i+2);00037 00038 <span class="keywordflow">if</span> (dir==DECRYPTION)00039 <span class="keywordflow">for</span> (i=0; i<(ROUNDS+2)/2; i++)00040 std::swap(pbox[i], pbox[ROUNDS+1-i]);00041 }00042 00043 <span class="comment">// this version is only used to make pbox and sbox</span>00044 <span class="keywordtype">void</span> Blowfish::Base::crypt_block(<span class="keyword">const</span> word32 in[2], word32 out[2])<span class="keyword"> const</span>00045 <span class="keyword"></span>{00046 word32 left = in[0];00047 word32 right = in[1];00048 00049 <span class="keyword">const</span> word32 *<span class="keyword">const</span> s=sbox;00050 <span class="keyword">const</span> word32 *p=pbox;00051 00052 left ^= p[0];00053 00054 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i<ROUNDS/2; i++)00055 {00056 right ^= (((s[GETBYTE(left,3)] + s[256+GETBYTE(left,2)])00057 ^ s[2*256+GETBYTE(left,1)]) + s[3*256+GETBYTE(left,0)])00058 ^ p[2*i+1];00059 00060 left ^= (((s[GETBYTE(right,3)] + s[256+GETBYTE(right,2)])00061 ^ s[2*256+GETBYTE(right,1)]) + s[3*256+GETBYTE(right,0)])00062 ^ p[2*i+2];00063 }00064 00065 right ^= p[ROUNDS+1];00066 00067 out[0] = right;00068 out[1] = left;00069 }00070 00071 <span class="keywordtype">void</span> Blowfish::Base::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00072 <span class="keyword"></span>{00073 <span class="keyword">typedef</span> BlockGetAndPut<word32, BigEndian> Block;00074 00075 word32 left, right;00076 Block::Get(inBlock)(left)(right);00077 00078 <span class="keyword">const</span> word32 *<span class="keyword">const</span> s=sbox;00079 <span class="keyword">const</span> word32 *p=pbox;00080 00081 left ^= p[0];00082 00083 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i<ROUNDS/2; i++)00084 {00085 right ^= (((s[GETBYTE(left,3)] + s[256+GETBYTE(left,2)])00086 ^ s[2*256+GETBYTE(left,1)]) + s[3*256+GETBYTE(left,0)])00087 ^ p[2*i+1];00088 00089 left ^= (((s[GETBYTE(right,3)] + s[256+GETBYTE(right,2)])00090 ^ s[2*256+GETBYTE(right,1)]) + s[3*256+GETBYTE(right,0)])00091 ^ p[2*i+2];00092 }00093 00094 right ^= p[ROUNDS+1];00095 00096 Block::Put(xorBlock, outBlock)(right)(left);00097 }00098 00099 NAMESPACE_END</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:09 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 + -