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

📄 cryptlib_8cpp-source.html

📁 著名的密码库Crypto++的文档 C++语言的杰作。程序员必备。
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<a name="l00066"></a>00066 }<a name="l00067"></a>00067 <a name="l00068"></a><a class="code" href="class_simple_keying_interface.html#32b2095542b18025814118734bf34da2">00068</a> <span class="keywordtype">void</span> <a class="code" href="class_simple_keying_interface.html#32b2095542b18025814118734bf34da2">SimpleKeyingInterface::ThrowIfInvalidKeyLength</a>(<span class="keywordtype">size_t</span> length)<a name="l00069"></a>00069 {<a name="l00070"></a>00070         <span class="keywordflow">if</span> (!<a class="code" href="class_simple_keying_interface.html#2229cdb9d7ac7e2cf3b02b39ae59ad77" title="returns whether n is a valid key length">IsValidKeyLength</a>(length))<a name="l00071"></a>00071                 <span class="keywordflow">throw</span> <a class="code" href="class_invalid_key_length.html" title="_">InvalidKeyLength</a>(GetAlgorithm().AlgorithmName(), length);<a name="l00072"></a>00072 }<a name="l00073"></a>00073 <a name="l00074"></a><a class="code" href="class_simple_keying_interface.html#e60d9774af66fc673fe6139f5880c477">00074</a> <span class="keywordtype">void</span> <a class="code" href="class_simple_keying_interface.html#e60d9774af66fc673fe6139f5880c477">SimpleKeyingInterface::ThrowIfResynchronizable</a>()<a name="l00075"></a>00075 {<a name="l00076"></a>00076         <span class="keywordflow">if</span> (<a class="code" href="class_simple_keying_interface.html#71039112353cc70e9859ebd95037cc1a" title="returns whether this object can be resynchronized (i.e. supports initialization vectors)...">IsResynchronizable</a>())<a name="l00077"></a>00077                 <span class="keywordflow">throw</span> <a class="code" href="class_invalid_argument.html" title="exception thrown when an invalid argument is detected">InvalidArgument</a>(GetAlgorithm().AlgorithmName() + <span class="stringliteral">": this object requires an IV"</span>);<a name="l00078"></a>00078 }<a name="l00079"></a>00079 <a name="l00080"></a><a class="code" href="class_simple_keying_interface.html#c687add7d0a09c626d85b77714bbd114">00080</a> <span class="keywordtype">void</span> <a class="code" href="class_simple_keying_interface.html#c687add7d0a09c626d85b77714bbd114">SimpleKeyingInterface::ThrowIfInvalidIV</a>(<span class="keyword">const</span> byte *iv)<a name="l00081"></a>00081 {<a name="l00082"></a>00082         <span class="keywordflow">if</span> (!iv &amp;&amp; !(<a class="code" href="class_simple_keying_interface.html#22087180aa396209403b0852e9efc431" title="returns the minimal requirement for secure IVs">IVRequirement</a>() == <a class="code" href="class_simple_keying_interface.html#88201c180fbfaa9fef2715252546ad6eb659bf6d2c470c50c14bfbab5b2d45ab">INTERNALLY_GENERATED_IV</a> || <a class="code" href="class_simple_keying_interface.html#22087180aa396209403b0852e9efc431" title="returns the minimal requirement for secure IVs">IVRequirement</a>() == <a class="code" href="class_simple_keying_interface.html#88201c180fbfaa9fef2715252546ad6e1cc32e69fe4f385e54d9d482a2ab5a56">UNIQUE_IV</a> || !<a class="code" href="class_simple_keying_interface.html#71039112353cc70e9859ebd95037cc1a" title="returns whether this object can be resynchronized (i.e. supports initialization vectors)...">IsResynchronizable</a>()))<a name="l00083"></a>00083                 <span class="keywordflow">throw</span> <a class="code" href="class_invalid_argument.html" title="exception thrown when an invalid argument is detected">InvalidArgument</a>(GetAlgorithm().AlgorithmName() + <span class="stringliteral">": this object cannot use a null IV"</span>);<a name="l00084"></a>00084 }<a name="l00085"></a>00085 <a name="l00086"></a><a class="code" href="class_simple_keying_interface.html#8d63825e000442603429b6f433b798f5">00086</a> <span class="keyword">const</span> byte * <a class="code" href="class_simple_keying_interface.html#8d63825e000442603429b6f433b798f5">SimpleKeyingInterface::GetIVAndThrowIfInvalid</a>(<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;params)<a name="l00087"></a>00087 {<a name="l00088"></a>00088         <span class="keyword">const</span> byte *iv;<a name="l00089"></a>00089         <span class="keywordflow">if</span> (params.<a class="code" href="class_name_value_pairs.html#96686e9f8d6ce3ab870e516fb72b608e" title="get a named value, returns true if the name exists">GetValue</a>(<a class="code" href="namespace_name.html#9b6839240a8bdf1f5bcbebf1866f4733" title="const byte *">Name::IV</a>(), iv))<a name="l00090"></a>00090                 <a class="code" href="class_simple_keying_interface.html#c687add7d0a09c626d85b77714bbd114">ThrowIfInvalidIV</a>(iv);<a name="l00091"></a>00091         <span class="keywordflow">else</span><a name="l00092"></a>00092                 <a class="code" href="class_simple_keying_interface.html#e60d9774af66fc673fe6139f5880c477">ThrowIfResynchronizable</a>();<a name="l00093"></a>00093         <span class="keywordflow">return</span> iv;<a name="l00094"></a>00094 }<a name="l00095"></a>00095 <a name="l00096"></a><a class="code" href="class_simple_keying_interface.html#917224bb6df7431d6469e7a10feac5fa">00096</a> <span class="keywordtype">void</span> <a class="code" href="class_simple_keying_interface.html#917224bb6df7431d6469e7a10feac5fa" title="get a secure IV for the next message">SimpleKeyingInterface::GetNextIV</a>(<a class="code" href="class_random_number_generator.html" title="interface for random number generators">RandomNumberGenerator</a> &amp;rng, byte *IV)<a name="l00097"></a>00097 {<a name="l00098"></a>00098         rng.<a class="code" href="class_random_number_generator.html#497145546d24e6d4abaf10b7e0f1ba17" title="generate random array of bytes">GenerateBlock</a>(IV, <a class="code" href="class_simple_keying_interface.html#c6da9a8d3c783014ab54a2a3ee9e676a" title="returns size of IVs used by this object">IVSize</a>());<a name="l00099"></a>00099 }<a name="l00100"></a>00100 <a name="l00101"></a><a class="code" href="class_block_transformation.html#9efa0ea24795a19d15c9aa0d7f134562">00101</a> <span class="keywordtype">void</span> <a class="code" href="class_block_transformation.html#9efa0ea24795a19d15c9aa0d7f134562" title="encrypt or decrypt multiple blocks, for bit-slicing implementations">BlockTransformation::ProcessAndXorMultipleBlocks</a>(<span class="keyword">const</span> byte *inBlocks, <span class="keyword">const</span> byte *xorBlocks, byte *outBlocks, <span class="keywordtype">size_t</span> numberOfBlocks)<span class="keyword"> const</span><a name="l00102"></a>00102 <span class="keyword"></span>{<a name="l00103"></a>00103         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> blockSize = <a class="code" href="class_block_transformation.html#4936bef4368804cf5cadab9eb6eb37e4" title="block size of the cipher in bytes">BlockSize</a>();<a name="l00104"></a>00104         <span class="keywordflow">while</span> (numberOfBlocks--)<a name="l00105"></a>00105         {<a name="l00106"></a>00106                 <a class="code" href="class_block_transformation.html#1904cf145e4c5c650dd7aff32f54e153" title="encrypt or decrypt inBlock, xor with xorBlock, and write to outBlock">ProcessAndXorBlock</a>(inBlocks, xorBlocks, outBlocks);<a name="l00107"></a>00107                 inBlocks += blockSize;<a name="l00108"></a>00108                 outBlocks += blockSize;<a name="l00109"></a>00109                 <span class="keywordflow">if</span> (xorBlocks)<a name="l00110"></a>00110                         xorBlocks += blockSize;<a name="l00111"></a>00111         }<a name="l00112"></a>00112 }<a name="l00113"></a>00113 <a name="l00114"></a><a class="code" href="class_block_transformation.html#f2da7e22f6b6ddda72bc7db2aceaa67a">00114</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_block_transformation.html#f2da7e22f6b6ddda72bc7db2aceaa67a" title="block pointers must be divisible by this">BlockTransformation::BlockAlignment</a>()<span class="keyword"> const</span><a name="l00115"></a>00115 <span class="keyword"></span>{<a name="l00116"></a>00116         <span class="keywordflow">return</span> GetAlignmentOf&lt;word32&gt;();<a name="l00117"></a>00117 }<a name="l00118"></a>00118 <a name="l00119"></a><a class="code" href="class_stream_transformation.html#0668b78edaea082f87bf3e878c4c02f7">00119</a> <span class="keywordtype">void</span> <a class="code" href="class_stream_transformation.html#0668b78edaea082f87bf3e878c4c02f7" title="for ciphers where the last block of data is special, encrypt or decrypt the last...">StreamTransformation::ProcessLastBlock</a>(byte *outString, <span class="keyword">const</span> byte *inString, <span class="keywordtype">size_t</span> length)<a name="l00120"></a>00120 {<a name="l00121"></a>00121         assert(<a class="code" href="class_stream_transformation.html#0b7c5bc41b9b5f6443ee32d65ebc044c" title="returns the minimum size of the last block, 0 indicating the last block is not special...">MinLastBlockSize</a>() == 0);        <span class="comment">// this function should be overriden otherwise</span><a name="l00122"></a>00122 <a name="l00123"></a>00123         <span class="keywordflow">if</span> (length == <a class="code" href="class_stream_transformation.html#e224d250f716e82c54bc4e4c8930d188" title="returns block size, if input must be processed in blocks, otherwise 1">MandatoryBlockSize</a>())<a name="l00124"></a>00124                 <a class="code" href="class_stream_transformation.html#26feabde21bc4d1783195969733e3bb0" title="encrypt or decrypt an array of bytes of specified length">ProcessData</a>(outString, inString, length);<a name="l00125"></a>00125         <span class="keywordflow">else</span> <span class="keywordflow">if</span> (length != 0)<a name="l00126"></a>00126                 <span class="keywordflow">throw</span> <a class="code" href="class_not_implemented.html" title="exception thrown by a class if a non-implemented method is called">NotImplemented</a>(<span class="stringliteral">"StreamTransformation: this object does't support a special last block"</span>);<a name="l00127"></a>00127 }<a name="l00128"></a>00128 <a name="l00129"></a><a class="code" href="class_random_number_generator.html#ce592244cfbd6c18398ea6c75857532e">00129</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="class_random_number_generator.html#ce592244cfbd6c18398ea6c75857532e" title="generate new random bit and return it">RandomNumberGenerator::GenerateBit</a>()<a name="l00130"></a>00130 {<a name="l00131"></a>00131         <span class="keywordflow">return</span> <a class="code" href="class_random_number_generator.html#da0f7c437b0c8e6ba94ebcd75bff4bda" title="generate new random byte and return it">GenerateByte</a>() &amp; 1;<a name="l00132"></a>00132 }<a name="l00133"></a>00133 <a name="l00134"></a><a class="code" href="class_random_number_generator.html#da0f7c437b0c8e6ba94ebcd75bff4bda">00134</a> byte <a class="code" href="class_random_number_generator.html#da0f7c437b0c8e6ba94ebcd75bff4bda" title="generate new random byte and return it">RandomNumberGenerator::GenerateByte</a>()<a name="l00135"></a>00135 {<a name="l00136"></a>00136         byte b;<a name="l00137"></a>00137         <a class="code" href="class_random_number_generator.html#497145546d24e6d4abaf10b7e0f1ba17" title="generate random array of bytes">GenerateBlock</a>(&amp;b, 1);<a name="l00138"></a>00138         <span class="keywordflow">return</span> b;<a name="l00139"></a>00139 }<a name="l00140"></a>00140 <a name="l00141"></a><a class="code" href="class_random_number_generator.html#c06c9f5e66f35d643e4192f231b8a4cd">00141</a> word32 <a class="code" href="class_random_number_generator.html#c06c9f5e66f35d643e4192f231b8a4cd" title="generate a random 32 bit word in the range min to max, inclusive">RandomNumberGenerator::GenerateWord32</a>(word32 min, word32 max)<a name="l00142"></a>00142 {<a name="l00143"></a>00143         word32 range = max-min;<a name="l00144"></a>00144         <span class="keyword">const</span> <span class="keywordtype">int</span> maxBits = BitPrecision(range);<a name="l00145"></a>00145 <a name="l00146"></a>00146         word32 value;<a name="l00147"></a>00147 <a name="l00148"></a>00148         <span class="keywordflow">do</span><a name="l00149"></a>00149         {<a name="l00150"></a>00150                 <a class="code" href="class_random_number_generator.html#497145546d24e6d4abaf10b7e0f1ba17" title="generate random array of bytes">GenerateBlock</a>((byte *)&amp;value, <span class="keyword">sizeof</span>(value));<a name="l00151"></a>00151                 value = Crop(value, maxBits);

⌨️ 快捷键说明

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