arc4_8cpp-source.html
来自「Crypto++是一个非常强大的密码学库,主要是功能全」· HTML 代码 · 共 130 行
HTML
130 行
<!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++: arc4.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>arc4.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// arc4.cpp - written and placed in the public domain by Wei Dai</span>00002 00003 <span class="comment">// The ARC4 algorithm was first revealed in an anonymous email to the</span>00004 <span class="comment">// cypherpunks mailing list. This file originally contained some</span>00005 <span class="comment">// code copied from this email. The code has since been rewritten in order</span>00006 <span class="comment">// to clarify the copyright status of this file. It should now be</span>00007 <span class="comment">// completely in the public domain.</span>00008 00009 <span class="preprocessor">#include "pch.h"</span>00010 <span class="preprocessor">#include "arc4.h"</span>00011 00012 NAMESPACE_BEGIN(CryptoPP)00013 00014 <span class="keywordtype">void</span> ARC4_TestInstantiations()00015 {00016 ARC4 x;00017 }00018 00019 ARC4_Base::~ARC4_Base()00020 {00021 m_x = m_y = 0;00022 }00023 00024 <span class="keywordtype">void</span> ARC4_Base::UncheckedSetKey(<span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html">NameValuePairs</a> &params, <span class="keyword">const</span> byte *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> keyLen)00025 {00026 AssertValidKeyLength(keyLen);00027 00028 m_x = 1;00029 m_y = 0;00030 00031 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;00032 <span class="keywordflow">for</span> (i=0; i<256; i++)00033 m_state[i] = i;00034 00035 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> keyIndex = 0, stateIndex = 0;00036 <span class="keywordflow">for</span> (i=0; i<256; i++)00037 {00038 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> a = m_state[i];00039 stateIndex += key[keyIndex] + a;00040 stateIndex &= 0xff;00041 m_state[i] = m_state[stateIndex];00042 m_state[stateIndex] = a;00043 <span class="keywordflow">if</span> (++keyIndex >= keyLen)00044 keyIndex = 0;00045 }00046 00047 <span class="keywordtype">int</span> discardBytes = params.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha43">GetIntValueWithDefault</a>(<span class="stringliteral">"DiscardBytes"</span>, GetDefaultDiscardBytes());00048 <a class="code" href="class_a_r_c4___base.html#_m_a_r_c4___basea1">DiscardBytes</a>(discardBytes);00049 }00050 00051 <span class="keyword">template</span> <<span class="keyword">class</span> T>00052 <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> MakeByte(T &x, T &y, byte *s)00053 {00054 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> a = s[x];00055 y = (y+a) & 0xff;00056 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> b = s[y];00057 s[x] = b;00058 s[y] = a;00059 x = (x+1) & 0xff;00060 <span class="keywordflow">return</span> s[(a+b) & 0xff];00061 }00062 <a name="l00063"></a><a class="code" href="class_a_r_c4___base.html#_m_a_r_c4___basea0">00063</a> byte <a class="code" href="class_a_r_c4___base.html#_m_a_r_c4___basea0">ARC4_Base::GenerateByte</a>()00064 {00065 <span class="keywordflow">return</span> MakeByte(m_x, m_y, m_state);00066 }00067 <a name="l00068"></a><a class="code" href="class_a_r_c4___base.html#_m_a_r_c4___basea2">00068</a> <span class="keywordtype">void</span> <a class="code" href="class_a_r_c4___base.html#_m_a_r_c4___basea2">ARC4_Base::ProcessData</a>(byte *outString, <span class="keyword">const</span> byte *inString, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00069 {00070 <span class="keywordflow">if</span> (length == 0)00071 <span class="keywordflow">return</span>;00072 00073 byte *<span class="keyword">const</span> s = m_state;00074 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> x = m_x;00075 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> y = m_y;00076 00077 <span class="keywordflow">if</span> (inString == outString)00078 {00079 <span class="keywordflow">do</span>00080 {00081 *outString++ ^= MakeByte(x, y, s);00082 } <span class="keywordflow">while</span> (--length);00083 }00084 <span class="keywordflow">else</span>00085 {00086 <span class="keywordflow">do</span>00087 {00088 *outString++ = *inString++ ^ MakeByte(x, y, s);00089 }00090 <span class="keywordflow">while</span>(--length);00091 }00092 00093 m_x = x;00094 m_y = y;00095 }00096 <a name="l00097"></a><a class="code" href="class_a_r_c4___base.html#_m_a_r_c4___basea1">00097</a> <span class="keywordtype">void</span> <a class="code" href="class_a_r_c4___base.html#_m_a_r_c4___basea1">ARC4_Base::DiscardBytes</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00098 {00099 <span class="keywordflow">if</span> (length == 0)00100 <span class="keywordflow">return</span>;00101 00102 byte *<span class="keyword">const</span> s = m_state;00103 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> x = m_x;00104 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> y = m_y;00105 00106 <span class="keywordflow">do</span>00107 {00108 MakeByte(x, y, s);00109 }00110 <span class="keywordflow">while</span>(--length);00111 00112 m_x = x;00113 m_y = y;00114 }00115 00116 NAMESPACE_END</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:07 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 + =
减小字号Ctrl + -
显示快捷键?