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

📄 rabin_8cpp-source.html

📁 著名的密码库Crypto++的文档 C++语言的杰作。程序员必备。
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<a name="l00063"></a>00063 {<a name="l00064"></a>00064         AssignFromHelper(<span class="keyword">this</span>, source)<a name="l00065"></a>00065                 CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)<a name="l00066"></a>00066                 CRYPTOPP_SET_FUNCTION_ENTRY(QuadraticResidueModPrime1)<a name="l00067"></a>00067                 CRYPTOPP_SET_FUNCTION_ENTRY(QuadraticResidueModPrime2)<a name="l00068"></a>00068                 ;<a name="l00069"></a>00069 }<a name="l00070"></a>00070 <a name="l00071"></a>00071 <span class="comment">// *****************************************************************************</span><a name="l00072"></a>00072 <span class="comment">// private key operations:</span><a name="l00073"></a>00073 <a name="l00074"></a>00074 <span class="comment">// generate a random private key</span><a name="l00075"></a><a class="code" href="class_invertible_rabin_function.html#571737380b956b4add10c855484af670">00075</a> <span class="keywordtype">void</span> <a class="code" href="class_invertible_rabin_function.html#571737380b956b4add10c855484af670">InvertibleRabinFunction::GenerateRandom</a>(<a class="code" href="class_random_number_generator.html" title="interface for random number generators">RandomNumberGenerator</a> &amp;rng, <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;alg)<a name="l00076"></a>00076 {<a name="l00077"></a>00077         <span class="keywordtype">int</span> modulusSize = 2048;<a name="l00078"></a>00078         alg.<a class="code" href="class_name_value_pairs.html#39b6daefcabcdd07f5ae482a075e1728" title="get a named value with type int">GetIntValue</a>(<span class="stringliteral">"ModulusSize"</span>, modulusSize) || alg.<a class="code" href="class_name_value_pairs.html#39b6daefcabcdd07f5ae482a075e1728" title="get a named value with type int">GetIntValue</a>(<span class="stringliteral">"KeySize"</span>, modulusSize);<a name="l00079"></a>00079 <a name="l00080"></a>00080         <span class="keywordflow">if</span> (modulusSize &lt; 16)<a name="l00081"></a>00081                 <span class="keywordflow">throw</span> <a class="code" href="class_invalid_argument.html" title="exception thrown when an invalid argument is detected">InvalidArgument</a>(<span class="stringliteral">"InvertibleRabinFunction: specified modulus size is too small"</span>);<a name="l00082"></a>00082 <a name="l00083"></a>00083         <span class="comment">// VC70 workaround: putting these after primeParam causes overlapped stack allocation</span><a name="l00084"></a>00084         <span class="keywordtype">bool</span> rFound=<span class="keyword">false</span>, sFound=<span class="keyword">false</span>;<a name="l00085"></a>00085         <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> t=2;<a name="l00086"></a>00086 <a name="l00087"></a>00087         <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;primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize)<a name="l00088"></a>00088                 (<span class="stringliteral">"EquivalentTo"</span>, 3)(<span class="stringliteral">"Mod"</span>, 4);<a name="l00089"></a>00089         <a class="code" href="class_invertible_rabin_function.html#7265f1577c7c60962100bf048c532b65">m_p</a>.<a class="code" href="class_integer.html#0d9a427fe64a454b11b24fe61c10dbeb">GenerateRandom</a>(rng, primeParam);<a name="l00090"></a>00090         <a class="code" href="class_invertible_rabin_function.html#6f7f68cf7b9299b85da9df3241aa8096">m_q</a>.<a class="code" href="class_integer.html#0d9a427fe64a454b11b24fe61c10dbeb">GenerateRandom</a>(rng, primeParam);<a name="l00091"></a>00091 <a name="l00092"></a>00092         <span class="keywordflow">while</span> (!(rFound &amp;&amp; sFound))<a name="l00093"></a>00093         {<a name="l00094"></a>00094                 <span class="keywordtype">int</span> jp = Jacobi(t, <a class="code" href="class_invertible_rabin_function.html#7265f1577c7c60962100bf048c532b65">m_p</a>);<a name="l00095"></a>00095                 <span class="keywordtype">int</span> jq = Jacobi(t, <a class="code" href="class_invertible_rabin_function.html#6f7f68cf7b9299b85da9df3241aa8096">m_q</a>);<a name="l00096"></a>00096 <a name="l00097"></a>00097                 <span class="keywordflow">if</span> (!rFound &amp;&amp; jp==1 &amp;&amp; jq==-1)<a name="l00098"></a>00098                 {<a name="l00099"></a>00099                         <a class="code" href="class_rabin_function.html#c98a931385ae02de294a7a7da66fddfd">m_r</a> = t;<a name="l00100"></a>00100                         rFound = <span class="keyword">true</span>;<a name="l00101"></a>00101                 }<a name="l00102"></a>00102 <a name="l00103"></a>00103                 <span class="keywordflow">if</span> (!sFound &amp;&amp; jp==-1 &amp;&amp; jq==1)<a name="l00104"></a>00104                 {<a name="l00105"></a>00105                         <a class="code" href="class_rabin_function.html#601264d1c703941468aaaa7c1918dec2">m_s</a> = t;<a name="l00106"></a>00106                         sFound = <span class="keyword">true</span>;<a name="l00107"></a>00107                 }<a name="l00108"></a>00108 <a name="l00109"></a>00109                 ++t;<a name="l00110"></a>00110         }<a name="l00111"></a>00111 <a name="l00112"></a>00112         <a class="code" href="class_rabin_function.html#f361185b5962701c78c410945f6280bc">m_n</a> = <a class="code" href="class_invertible_rabin_function.html#7265f1577c7c60962100bf048c532b65">m_p</a> * <a class="code" href="class_invertible_rabin_function.html#6f7f68cf7b9299b85da9df3241aa8096">m_q</a>;<a name="l00113"></a>00113         <a class="code" href="class_invertible_rabin_function.html#39d937e65da2073ea867b8a93f8b25e7">m_u</a> = m_q.<a class="code" href="class_integer.html#881f9c714ee42f35718725a43d4d7db3" title="calculate multiplicative inverse of *this mod n">InverseMod</a>(<a class="code" href="class_invertible_rabin_function.html#7265f1577c7c60962100bf048c532b65">m_p</a>);<a name="l00114"></a>00114 }<a name="l00115"></a>00115 <a name="l00116"></a><a class="code" href="class_invertible_rabin_function.html#d770bc9e03d2f5b812655b6d8dd1d796">00116</a> <span class="keywordtype">void</span> <a class="code" href="class_invertible_rabin_function.html#d770bc9e03d2f5b812655b6d8dd1d796">InvertibleRabinFunction::BERDecode</a>(<a class="code" href="class_buffered_transformation.html" title="interface for buffered transformations">BufferedTransformation</a> &amp;bt)<a name="l00117"></a>00117 {<a name="l00118"></a>00118         <a class="code" href="class_b_e_r_sequence_decoder.html" title="BER Sequence Decoder.">BERSequenceDecoder</a> seq(bt);<a name="l00119"></a>00119         <a class="code" href="class_rabin_function.html#f361185b5962701c78c410945f6280bc">m_n</a>.<a class="code" href="class_integer.html#810fc0382f8928893fe192ab79b1972c">BERDecode</a>(seq);<a name="l00120"></a>00120         <a class="code" href="class_rabin_function.html#c98a931385ae02de294a7a7da66fddfd">m_r</a>.<a class="code" href="class_integer.html#810fc0382f8928893fe192ab79b1972c">BERDecode</a>(seq);<a name="l00121"></a>00121         <a class="code" href="class_rabin_function.html#601264d1c703941468aaaa7c1918dec2">m_s</a>.<a class="code" href="class_integer.html#810fc0382f8928893fe192ab79b1972c">BERDecode</a>(seq);<a name="l00122"></a>00122         <a class="code" href="class_invertible_rabin_function.html#7265f1577c7c60962100bf048c532b65">m_p</a>.<a class="code" href="class_integer.html#810fc0382f8928893fe192ab79b1972c">BERDecode</a>(seq);<a name="l00123"></a>00123         <a class="code" href="class_invertible_rabin_function.html#6f7f68cf7b9299b85da9df3241aa8096">m_q</a>.<a class="code" href="class_integer.html#810fc0382f8928893fe192ab79b1972c">BERDecode</a>(seq);<a name="l00124"></a>00124         <a class="code" href="class_invertible_rabin_function.html#39d937e65da2073ea867b8a93f8b25e7">m_u</a>.<a class="code" href="class_integer.html#810fc0382f8928893fe192ab79b1972c">BERDecode</a>(seq);<a name="l00125"></a>00125         seq.<a class="code" href="class_b_e_r_general_decoder.html#40b14625c98ac7febb8ca218d02358ad">MessageEnd</a>();<a name="l00126"></a>00126 }<a name="l00127"></a>00127 <a name="l00128"></a><a class="code" href="class_invertible_rabin_function.html#e7296b7b97da157a6cb0dc193174aa2d">00128</a> <span class="keywordtype">void</span> <a class="code" href="class_invertible_rabin_function.html#e7296b7b97da157a6cb0dc193174aa2d">InvertibleRabinFunction::DEREncode</a>(<a class="code" href="class_buffered_transformation.html" title="interface for buffered transformations">BufferedTransformation</a> &amp;bt)<span class="keyword"> const</span><a name="l00129"></a>00129 <span class="keyword"></span>{<a name="l00130"></a>00130         <a class="code" href="class_d_e_r_sequence_encoder.html" title="DER Sequence Encoder.">DERSequenceEncoder</a> seq(bt);<a name="l00131"></a>00131         <a class="code" href="class_rabin_function.html#f361185b5962701c78c410945f6280bc">m_n</a>.<a class="code" href="class_integer.html#6ab51a05bee88cfa690179611e8a084e" title="encode using Distinguished Encoding Rules, put result into a BufferedTransformation...">DEREncode</a>(seq);<a name="l00132"></a>00132         <a class="code" href="class_rabin_function.html#c98a931385ae02de294a7a7da66fddfd">m_r</a>.<a class="code" href="class_integer.html#6ab51a05bee88cfa690179611e8a084e" title="encode using Distinguished Encoding Rules, put result into a BufferedTransformation...">DEREncode</a>(seq);<a name="l00133"></a>00133         <a class="code" href="class_rabin_function.html#601264d1c703941468aaaa7c1918dec2">m_s</a>.<a class="code" href="class_integer.html#6ab51a05bee88cfa690179611e8a084e" title="encode using Distinguished Encoding Rules, put result into a BufferedTransformation...">DEREncode</a>(seq);<a name="l00134"></a>00134         <a class="code" href="class_invertible_rabin_function.html#7265f1577c7c60962100bf048c532b65">m_p</a>.<a class="code" href="class_integer.html#6ab51a05bee88cfa690179611e8a084e" title="encode using Distinguished Encoding Rules, put result into a BufferedTransformation...">DEREncode</a>(seq);<a name="l00135"></a>00135         <a class="code" href="class_invertible_rabin_function.html#6f7f68cf7b9299b85da9df3241aa8096">m_q</a>.<a class="code" href="class_integer.html#6ab51a05bee88cfa690179611e8a084e" title="encode using Distinguished Encoding Rules, put result into a BufferedTransformation...">DEREncode</a>(seq);<a name="l00136"></a>00136         <a class="code" href="class_invertible_rabin_function.html#39d937e65da2073ea867b8a93f8b25e7">m_u</a>.<a class="code" href="class_integer.html#6ab51a05bee88cfa690179611e8a084e" title="encode using Distinguished Encoding Rules, put result into a BufferedTransformation...">DEREncode</a>(seq);<a name="l00137"></a>00137         seq.<a class="code" href="class_d_e_r_general_encoder.html#cb5f1a94ba97027c603b019bd5228510">MessageEnd</a>();<a name="l00138"></a>00138 }<a name="l00139"></a>00139 <a name="l00140"></a><a class="code" href="class_invertible_rabin_function.html#15788f2fb0225297f665e30044661dfd">00140</a> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> <a class="code" href="class_invertible_rabin_function.html#15788f2fb0225297f665e30044661dfd">InvertibleRabinFunction::CalculateInverse</a>(<a class="code" href="class_random_number_generator.html" title="interface for random number generators">RandomNumberGenerator</a> &amp;rng, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &amp;in)<span class="keyword"> const</span><a name="l00141"></a>00141 <span class="keyword"></span>{<a name="l00142"></a>00142         <a class="code" href="class_crypto_material.html#a1da44802c5cf00946043922cb9e7e6e">DoQuickSanityCheck</a>();<a name="l00143"></a>00143 <a name="l00144"></a>00144         <a class="code" href="class_modular_arithmetic.html" title="ring of congruence classes modulo n">ModularArithmetic</a> modn(<a class="code" href="class_rabin_function.html#f361185b5962701c78c410945f6280bc">m_n</a>);<a name="l00145"></a>00145         <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> r(rng, <a class="code" href="class_integer.html#8c070592581bf6c2f928c72bfa1c1638" title="avoid calling constructors for these frequently used integers">Integer::One</a>(), <a class="code" href="class_rabin_function.html#f361185b5962701c78c410945f6280bc">m_n</a> - <a class="code" href="class_integer.html#8c070592581bf6c2f928c72bfa1c1638" title="avoid calling constructors for these frequently used integers">Integer::One</a>());

⌨️ 快捷键说明

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