📄 rc2_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++: rc2.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>rc2.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// rc2.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="rc2_8h.html">rc2.h</a>"</span>00005 <span class="preprocessor">#include "misc.h"</span>00006 00007 NAMESPACE_BEGIN(CryptoPP)00008 00009 <span class="keywordtype">void</span> RC2::Base::UncheckedSetKey(CipherDir direction, <span class="keyword">const</span> byte *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> keyLen, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> effectiveLen)00010 {00011 AssertValidKeyLength(keyLen);00012 00013 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> PITABLE[256] = {00014 217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157,00015 198,126, 55,131, 43,118, 83,142, 98, 76,100,136, 68,139,251,162,00016 23,154, 89,245,135,179, 79, 19, 97, 69,109,141, 9,129,125, 50,00017 189,143, 64,235,134,183,123, 11,240,149, 33, 34, 92,107, 78,130,00018 84,214,101,147,206, 96,178, 28,115, 86,192, 20,167,140,241,220,00019 18,117,202, 31, 59,190,228,209, 66, 61,212, 48,163, 60,182, 38,00020 111,191, 14,218, 70,105, 7, 87, 39,242, 29,155,188,148, 67, 3,00021 248, 17,199,246,144,239, 62,231, 6,195,213, 47,200,102, 30,215,00022 8,232,234,222,128, 82,238,247,132,170,114,172, 53, 77,106, 42,00023 150, 26,210,113, 90, 21, 73,116, 75,159,208, 94, 4, 24,164,236,00024 194,224, 65,110, 15, 81,203,204, 36,145,175, 80,161,244,112, 57,00025 153,124, 58,133, 35,184,180,122,252, 2, 54, 91, 37, 85,151, 49,00026 45, 93,250,152,227,138,146,174, 5,223, 41, 16,103,108,186,201,00027 211, 0,230,207,225,158,168, 44, 99, 22, 1, 63, 88,226,137,169,00028 13, 56, 52, 27,171, 51,255,176,187, 72, 12, 95,185,177,205, 46,00029 197,243,219, 71,229,165,156,119, 10,166, 32,104,254,127,193,173};00030 00031 <a class="code" href="class_sec_block.html">SecByteBlock</a> L(128);00032 memcpy(L, key, keyLen);00033 00034 <span class="keywordtype">int</span> i;00035 <span class="keywordflow">for</span> (i=keyLen; i<128; i++)00036 L[i] = PITABLE[(L[i-1] + L[i-keyLen]) & 255];00037 00038 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> T8 = (effectiveLen+7) / 8;00039 byte TM = 255 >> ((8-(effectiveLen%8))%8);00040 L[128-T8] = PITABLE[L[128-T8] & TM];00041 00042 <span class="keywordflow">for</span> (i=127-T8; i>=0; i--)00043 L[i] = PITABLE[L[i+1] ^ L[i+T8]];00044 00045 <span class="keywordflow">for</span> (i=0; i<64; i++)00046 K[i] = L[2*i] + (L[2*i+1] << 8);00047 }00048 00049 <span class="keywordtype">void</span> RC2::Base::SetKeyWithEffectiveKeyLength(<span class="keyword">const</span> byte *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> effectiveKeyLength)00050 {00051 <span class="keywordflow">if</span> (effectiveKeyLength > MAX_EFFECTIVE_KEYLENGTH)00052 <span class="keywordflow">throw</span> <a class="code" href="class_invalid_argument.html">InvalidArgument</a>(<span class="stringliteral">"RC2: effective key length parameter exceeds maximum"</span>);00053 UncheckedSetKey(ENCRYPTION, key, length, effectiveKeyLength);00054 }00055 00056 <span class="keyword">typedef</span> BlockGetAndPut<word16, LittleEndian> Block;00057 00058 <span class="keywordtype">void</span> RC2::Enc::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00059 <span class="keyword"></span>{00060 word16 R0, R1, R2, R3;00061 Block::Get(inBlock)(R0)(R1)(R2)(R3);00062 00063 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < 16; i++)00064 {00065 R0 += (R1 & ~R3) + (R2 & R3) + K[4*i+0];00066 R0 = rotlFixed(R0, 1);00067 00068 R1 += (R2 & ~R0) + (R3 & R0) + K[4*i+1];00069 R1 = rotlFixed(R1, 2);00070 00071 R2 += (R3 & ~R1) + (R0 & R1) + K[4*i+2];00072 R2 = rotlFixed(R2, 3);00073 00074 R3 += (R0 & ~R2) + (R1 & R2) + K[4*i+3];00075 R3 = rotlFixed(R3, 5);00076 00077 <span class="keywordflow">if</span> (i == 4 || i == 10)00078 {00079 R0 += K[R3 & 63];00080 R1 += K[R0 & 63];00081 R2 += K[R1 & 63];00082 R3 += K[R2 & 63];00083 }00084 }00085 00086 Block::Put(xorBlock, outBlock)(R0)(R1)(R2)(R3);00087 }00088 00089 <span class="keywordtype">void</span> RC2::Dec::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00090 <span class="keyword"></span>{00091 word16 R0, R1, R2, R3;00092 Block::Get(inBlock)(R0)(R1)(R2)(R3);00093 00094 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 15; i >= 0; i--)00095 {00096 <span class="keywordflow">if</span> (i == 4 || i == 10)00097 {00098 R3 -= K[R2 & 63];00099 R2 -= K[R1 & 63];00100 R1 -= K[R0 & 63];00101 R0 -= K[R3 & 63];00102 }00103 00104 R3 = rotrFixed(R3, 5);00105 R3 -= (R0 & ~R2) + (R1 & R2) + K[4*i+3];00106 00107 R2 = rotrFixed(R2, 3);00108 R2 -= (R3 & ~R1) + (R0 & R1) + K[4*i+2];00109 00110 R1 = rotrFixed(R1, 2);00111 R1 -= (R2 & ~R0) + (R3 & R0) + K[4*i+1];00112 00113 R0 = rotrFixed(R0, 1);00114 R0 -= (R1 & ~R3) + (R2 & R3) + K[4*i+0];00115 }00116 00117 Block::Put(xorBlock, outBlock)(R0)(R1)(R2)(R3);00118 }00119 00120 NAMESPACE_END</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:23 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 + -