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

📄 rsa_8cpp-source.html

📁 著名的密码库Crypto++的文档 C++语言的杰作。程序员必备。
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<a name="l00229"></a>00229                 r.<a class="code" href="class_integer.html#0f0574b9cae3cddf62c155da93085f0d">Randomize</a>(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_r_s_a_function.html#b2e89822414861cb312d52d8c7ac3017">m_n</a> - <a class="code" href="class_integer.html#8c070592581bf6c2f928c72bfa1c1638" title="avoid calling constructors for these frequently used integers">Integer::One</a>());<a name="l00230"></a>00230                 rInv = modn.<a class="code" href="class_modular_arithmetic.html#e2246ad602514e19f1291cd46b351ed9">MultiplicativeInverse</a>(r);<a name="l00231"></a>00231         } <span class="keywordflow">while</span> (rInv.<a class="code" href="class_integer.html#cca1f2de6c0bfa91c49950daf681b678">IsZero</a>());<a name="l00232"></a>00232         <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> re = modn.<a class="code" href="class_abstract_ring.html#0f011366054a4edb80be92f968083538">Exponentiate</a>(r, <a class="code" href="class_r_s_a_function.html#f6d15cdabeb084e9cdc7efb1bbdea783">m_e</a>);<a name="l00233"></a>00233         re = modn.<a class="code" href="class_modular_arithmetic.html#3b88a85b11eb1a826d26d01bdaafbf0a">Multiply</a>(re, x);                      <span class="comment">// blind</span><a name="l00234"></a>00234         <span class="comment">// here we follow the notation of PKCS #1 and let u=q inverse mod p</span><a name="l00235"></a>00235         <span class="comment">// but in ModRoot, u=p inverse mod q, so we reverse the order of p and q</span><a name="l00236"></a>00236         <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> y = ModularRoot(re, <a class="code" href="class_invertible_r_s_a_function.html#3702d3ea62a2edce11be04493e18a5a9">m_dq</a>, <a class="code" href="class_invertible_r_s_a_function.html#665927266b9a38963099529f720246ac">m_dp</a>, <a class="code" href="class_invertible_r_s_a_function.html#48b1e3c5cf1ba5ba83952d46708afaee">m_q</a>, <a class="code" href="class_invertible_r_s_a_function.html#0061793214f56717f67956444c935cf9">m_p</a>, <a class="code" href="class_invertible_r_s_a_function.html#6b7b378c8be194a47faf824844ed6c7b">m_u</a>);<a name="l00237"></a>00237         y = modn.<a class="code" href="class_modular_arithmetic.html#3b88a85b11eb1a826d26d01bdaafbf0a">Multiply</a>(y, rInv);                             <span class="comment">// unblind</span><a name="l00238"></a>00238         <span class="keywordflow">if</span> (modn.<a class="code" href="class_abstract_ring.html#0f011366054a4edb80be92f968083538">Exponentiate</a>(y, <a class="code" href="class_r_s_a_function.html#f6d15cdabeb084e9cdc7efb1bbdea783">m_e</a>) != x)             <span class="comment">// check</span><a name="l00239"></a>00239                 <span class="keywordflow">throw</span> <a class="code" href="class_exception.html" title="base class for all exceptions thrown by Crypto++">Exception</a>(<a class="code" href="class_exception.html#8e53909d979479380e0274807c074c0a27d9ee7704100a88e4ae4adcc4072015" title="some error not belong to any of the above categories">Exception::OTHER_ERROR</a>, <span class="stringliteral">"InvertibleRSAFunction: computational error during private key operation"</span>);<a name="l00240"></a>00240         <span class="keywordflow">return</span> y;<a name="l00241"></a>00241 }<a name="l00242"></a>00242 <a name="l00243"></a><a class="code" href="class_invertible_r_s_a_function.html#1416fe1274d2be8b297153ff02fe14dc">00243</a> <span class="keywordtype">bool</span> <a class="code" href="class_invertible_r_s_a_function.html#1416fe1274d2be8b297153ff02fe14dc" title="check this object for errors">InvertibleRSAFunction::Validate</a>(<a class="code" href="class_random_number_generator.html" title="interface for random number generators">RandomNumberGenerator</a> &amp;rng, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> level)<span class="keyword"> const</span><a name="l00244"></a>00244 <span class="keyword"></span>{<a name="l00245"></a>00245         <span class="keywordtype">bool</span> pass = <a class="code" href="class_r_s_a_function.html#2c43e097f0525db01c15bb53d80c2bea" title="check this object for errors">RSAFunction::Validate</a>(rng, level);<a name="l00246"></a>00246         pass = pass &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#0061793214f56717f67956444c935cf9">m_p</a> &gt; <a class="code" href="class_integer.html#8c070592581bf6c2f928c72bfa1c1638" title="avoid calling constructors for these frequently used integers">Integer::One</a>() &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#0061793214f56717f67956444c935cf9">m_p</a>.<a class="code" href="class_integer.html#ed4bb7208a18b986ef3e1a7d92e06d1d">IsOdd</a>() &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#0061793214f56717f67956444c935cf9">m_p</a> &lt; <a class="code" href="class_r_s_a_function.html#b2e89822414861cb312d52d8c7ac3017">m_n</a>;<a name="l00247"></a>00247         pass = pass &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#48b1e3c5cf1ba5ba83952d46708afaee">m_q</a> &gt; <a class="code" href="class_integer.html#8c070592581bf6c2f928c72bfa1c1638" title="avoid calling constructors for these frequently used integers">Integer::One</a>() &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#48b1e3c5cf1ba5ba83952d46708afaee">m_q</a>.<a class="code" href="class_integer.html#ed4bb7208a18b986ef3e1a7d92e06d1d">IsOdd</a>() &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#48b1e3c5cf1ba5ba83952d46708afaee">m_q</a> &lt; m_n;<a name="l00248"></a>00248         pass = pass &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#e6ebef334123434c80fd0d87dddea8c7">m_d</a> &gt; <a class="code" href="class_integer.html#8c070592581bf6c2f928c72bfa1c1638" title="avoid calling constructors for these frequently used integers">Integer::One</a>() &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#e6ebef334123434c80fd0d87dddea8c7">m_d</a>.<a class="code" href="class_integer.html#ed4bb7208a18b986ef3e1a7d92e06d1d">IsOdd</a>() &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#e6ebef334123434c80fd0d87dddea8c7">m_d</a> &lt; m_n;<a name="l00249"></a>00249         pass = pass &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#665927266b9a38963099529f720246ac">m_dp</a> &gt; <a class="code" href="class_integer.html#8c070592581bf6c2f928c72bfa1c1638" title="avoid calling constructors for these frequently used integers">Integer::One</a>() &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#665927266b9a38963099529f720246ac">m_dp</a>.<a class="code" href="class_integer.html#ed4bb7208a18b986ef3e1a7d92e06d1d">IsOdd</a>() &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#665927266b9a38963099529f720246ac">m_dp</a> &lt; <a class="code" href="class_invertible_r_s_a_function.html#0061793214f56717f67956444c935cf9">m_p</a>;<a name="l00250"></a>00250         pass = pass &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#3702d3ea62a2edce11be04493e18a5a9">m_dq</a> &gt; <a class="code" href="class_integer.html#8c070592581bf6c2f928c72bfa1c1638" title="avoid calling constructors for these frequently used integers">Integer::One</a>() &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#3702d3ea62a2edce11be04493e18a5a9">m_dq</a>.<a class="code" href="class_integer.html#ed4bb7208a18b986ef3e1a7d92e06d1d">IsOdd</a>() &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#3702d3ea62a2edce11be04493e18a5a9">m_dq</a> &lt; <a class="code" href="class_invertible_r_s_a_function.html#48b1e3c5cf1ba5ba83952d46708afaee">m_q</a>;<a name="l00251"></a>00251         pass = pass &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#6b7b378c8be194a47faf824844ed6c7b">m_u</a>.<a class="code" href="class_integer.html#13ddbfd8e9729932c2a99b0dff530978">IsPositive</a>() &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#6b7b378c8be194a47faf824844ed6c7b">m_u</a> &lt; m_p;<a name="l00252"></a>00252         <span class="keywordflow">if</span> (level &gt;= 1)<a name="l00253"></a>00253         {<a name="l00254"></a>00254                 pass = pass &amp;&amp; m_p * m_q == m_n;<a name="l00255"></a>00255                 pass = pass &amp;&amp; <a class="code" href="class_r_s_a_function.html#f6d15cdabeb084e9cdc7efb1bbdea783">m_e</a>*<a class="code" href="class_invertible_r_s_a_function.html#e6ebef334123434c80fd0d87dddea8c7">m_d</a> % LCM(m_p-1, m_q-1) == 1;<a name="l00256"></a>00256                 pass = pass &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#665927266b9a38963099529f720246ac">m_dp</a> == <a class="code" href="class_invertible_r_s_a_function.html#e6ebef334123434c80fd0d87dddea8c7">m_d</a>%(m_p-1) &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#3702d3ea62a2edce11be04493e18a5a9">m_dq</a> == <a class="code" href="class_invertible_r_s_a_function.html#e6ebef334123434c80fd0d87dddea8c7">m_d</a>%(m_q-1);<a name="l00257"></a>00257                 pass = pass &amp;&amp; <a class="code" href="class_invertible_r_s_a_function.html#6b7b378c8be194a47faf824844ed6c7b">m_u</a> * m_q % m_p == 1;<a name="l00258"></a>00258         }<a name="l00259"></a>00259         <span class="keywordflow">if</span> (level &gt;= 2)<a name="l00260"></a>00260                 pass = pass &amp;&amp; VerifyPrime(rng, m_p, level-2) &amp;&amp; VerifyPrime(rng, m_q, level-2);<a name="l00261"></a>00261         <span class="keywordflow">return</span> pass;<a name="l00262"></a>00262 }<a name="l00263"></a>00263 <a name="l00264"></a><a class="code" href="class_invertible_r_s_a_function.html#5d11463cbb6dac0edea661e6da7ed7ad">00264</a> <span class="keywordtype">bool</span> <a class="code" href="class_invertible_r_s_a_function.html#5d11463cbb6dac0edea661e6da7ed7ad" title="to be implemented by derived classes, users should use one of the above functions...">InvertibleRSAFunction::GetVoidValue</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *name, <span class="keyword">const</span> std::type_info &amp;valueType, <span class="keywordtype">void</span> *pValue)<span class="keyword"> const</span><a name="l00265"></a>00265 <span class="keyword"></span>{<a name="l00266"></a>00266         <span class="keywordflow">return</span> GetValueHelper&lt;RSAFunction&gt;(<span class="keyword">this</span>, name, valueType, pValue).Assignable()<a name="l00267"></a>00267                 CRYPTOPP_GET_FUNCTION_ENTRY(Prime1)<a name="l00268"></a>00268                 CRYPTOPP_GET_FUNCTION_ENTRY(Prime2)<a name="l00269"></a>00269                 CRYPTOPP_GET_FUNCTION_ENTRY(PrivateExponent)<a name="l00270"></a>00270                 CRYPTOPP_GET_FUNCTION_ENTRY(ModPrime1PrivateExponent)<a name="l00271"></a>00271                 CRYPTOPP_GET_FUNCTION_ENTRY(ModPrime2PrivateExponent)<a name="l00272"></a>00272                 CRYPTOPP_GET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)<a name="l00273"></a>00273                 ;<a name="l00274"></a>00274 }<a name="l00275"></a>00275 <a name="l00276"></a><a class="code" href="class_invertible_r_s_a_function.html#871fc366191e9c5fae114c06a200c928">00276</a> <span class="keywordtype">void</span> <a class="code" href="class_invertible_r_s_a_function.html#871fc366191e9c5fae114c06a200c928" title="assign values from source to this object">InvertibleRSAFunction::AssignFrom</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;source)<a name="l00277"></a>00277 {<a name="l00278"></a>00278         AssignFromHelper&lt;RSAFunction&gt;(<span class="keyword">this</span>, source)<a name="l00279"></a>00279                 CRYPTOPP_SET_FUNCTION_ENTRY(Prime1)<a name="l00280"></a>00280                 CRYPTOPP_SET_FUNCTION_ENTRY(Prime2)<a name="l00281"></a>00281                 CRYPTOPP_SET_FUNCTION_ENTRY(PrivateExponent)<a name="l00282"></a>00282                 CRYPTOPP_SET_FUNCTION_ENTRY(ModPrime1PrivateExponent)<a name="l00283"></a>00283                 CRYPTOPP_SET_FUNCTION_ENTRY(ModPrime2PrivateExponent)<a name="l00284"></a>00284                 CRYPTOPP_SET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)<a name="l00285"></a>00285                 ;<a name="l00286"></a>00286 }<a name="l00287"></a>00287 <a name="l00288"></a>00288 <span class="comment">// *****************************************************************************</span><a name="l00289"></a>00289 <a name="l00290"></a><a class="code" href="class_r_s_a_function___i_s_o.html#d7212813e9850ab6f1858cc2af263800">00290</a> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> <a class="code" href="class_r_s_a_function___i_s_o.html#d7212813e9850ab6f1858cc2af263800">RSAFunction_ISO::ApplyFunction</a>(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &amp;x)<span class="keyword"> const</span><a name="l00291"></a>00291 <span class="keyword"></span>{<a name="l00292"></a>00292         <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> t = <a class="code" href="class_r_s_a_function.html#8ff27b06cc624d5c41a1314b189cfab7">RSAFunction::ApplyFunction</a>(x);<a name="l00293"></a>00293         <span class="keywordflow">return</span> t % 16 == 12 ? t : <a class="code" href="class_r_s_a_function.html#b2e89822414861cb312d52d8c7ac3017">m_n</a> - t;<a name="l00294"></a>00294 }<a name="l00295"></a>00295 <a name="l00296"></a><a class="code" href="class_invertible_r_s_a_function___i_s_o.html#c5e8e542add1e671faa8619923f14312">00296</a> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> <a class="code" href="class_invertible_r_s_a_function___i_s_o.html#c5e8e542add1e671faa8619923f14312">InvertibleRSAFunction_ISO::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;x)<span class="keyword"> const </span><a name="l00297"></a>00297 <span class="keyword"></span>{<a name="l00298"></a>00298         <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> t = <a class="code" href="class_invertible_r_s_a_function.html#929a48a3b7942c57223c0ca8baa194d3">InvertibleRSAFunction::CalculateInverse</a>(rng, x);<a name="l00299"></a>00299         <span class="keywordflow">return</span> STDMIN(t, <a class="code" href="class_r_s_a_function.html#b2e89822414861cb312d52d8c7ac3017">m_n</a>-t);<a name="l00300"></a>00300 }<a name="l00301"></a>00301 <a name="l00302"></a>00302 NAMESPACE_END<a name="l00303"></a>00303 <a name="l00304"></a>00304 <span class="preprocessor">#endif</span></pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Fri Jun 1 11:11:24 2007 for Crypto++ by&nbsp;<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.2 </small></address></body></html>

⌨️ 快捷键说明

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