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

📄 rsa_8cpp-source.html

📁 著名的密码库Crypto++的文档 C++语言的杰作。程序员必备。
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<a name="l00063"></a>00063 <a name="l00064"></a><a class="code" href="class_r_s_a_function.html#8ff27b06cc624d5c41a1314b189cfab7">00064</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.html#8ff27b06cc624d5c41a1314b189cfab7">RSAFunction::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="l00065"></a>00065 <span class="keyword"></span>{<a name="l00066"></a>00066         <a class="code" href="class_crypto_material.html#a1da44802c5cf00946043922cb9e7e6e">DoQuickSanityCheck</a>();<a name="l00067"></a>00067         <span class="keywordflow">return</span> a_exp_b_mod_c(x, <a class="code" href="class_r_s_a_function.html#f6d15cdabeb084e9cdc7efb1bbdea783">m_e</a>, <a class="code" href="class_r_s_a_function.html#b2e89822414861cb312d52d8c7ac3017">m_n</a>);<a name="l00068"></a>00068 }<a name="l00069"></a>00069 <a name="l00070"></a><a class="code" href="class_r_s_a_function.html#2c43e097f0525db01c15bb53d80c2bea">00070</a> <span class="keywordtype">bool</span> <a class="code" href="class_r_s_a_function.html#2c43e097f0525db01c15bb53d80c2bea" title="check this object for errors">RSAFunction::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="l00071"></a>00071 <span class="keyword"></span>{<a name="l00072"></a>00072         <span class="keywordtype">bool</span> pass = <span class="keyword">true</span>;<a name="l00073"></a>00073         pass = pass &amp;&amp; <a class="code" href="class_r_s_a_function.html#b2e89822414861cb312d52d8c7ac3017">m_n</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_r_s_a_function.html#b2e89822414861cb312d52d8c7ac3017">m_n</a>.<a class="code" href="class_integer.html#ed4bb7208a18b986ef3e1a7d92e06d1d">IsOdd</a>();<a name="l00074"></a>00074         pass = pass &amp;&amp; <a class="code" href="class_r_s_a_function.html#f6d15cdabeb084e9cdc7efb1bbdea783">m_e</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_r_s_a_function.html#f6d15cdabeb084e9cdc7efb1bbdea783">m_e</a>.<a class="code" href="class_integer.html#ed4bb7208a18b986ef3e1a7d92e06d1d">IsOdd</a>() &amp;&amp; <a class="code" href="class_r_s_a_function.html#f6d15cdabeb084e9cdc7efb1bbdea783">m_e</a> &lt; <a class="code" href="class_r_s_a_function.html#b2e89822414861cb312d52d8c7ac3017">m_n</a>;<a name="l00075"></a>00075         <span class="keywordflow">return</span> pass;<a name="l00076"></a>00076 }<a name="l00077"></a>00077 <a name="l00078"></a><a class="code" href="class_r_s_a_function.html#8178257a012f725ceb2cb6eead2121d6">00078</a> <span class="keywordtype">bool</span> <a class="code" href="class_r_s_a_function.html#8178257a012f725ceb2cb6eead2121d6" title="to be implemented by derived classes, users should use one of the above functions...">RSAFunction::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="l00079"></a>00079 <span class="keyword"></span>{<a name="l00080"></a>00080         <span class="keywordflow">return</span> GetValueHelper(<span class="keyword">this</span>, name, valueType, pValue).Assignable()<a name="l00081"></a>00081                 CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)<a name="l00082"></a>00082                 CRYPTOPP_GET_FUNCTION_ENTRY(PublicExponent)<a name="l00083"></a>00083                 ;<a name="l00084"></a>00084 }<a name="l00085"></a>00085 <a name="l00086"></a><a class="code" href="class_r_s_a_function.html#d2db62b20297a8d9df55171e73fe8de7">00086</a> <span class="keywordtype">void</span> <a class="code" href="class_r_s_a_function.html#d2db62b20297a8d9df55171e73fe8de7" title="assign values from source to this object">RSAFunction::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="l00087"></a>00087 {<a name="l00088"></a>00088         AssignFromHelper(<span class="keyword">this</span>, source)<a name="l00089"></a>00089                 CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)<a name="l00090"></a>00090                 CRYPTOPP_SET_FUNCTION_ENTRY(PublicExponent)<a name="l00091"></a>00091                 ;<a name="l00092"></a>00092 }<a name="l00093"></a>00093 <a name="l00094"></a>00094 <span class="comment">// *****************************************************************************</span><a name="l00095"></a>00095 <a name="l00096"></a><a class="code" href="class_r_s_a_prime_selector.html">00096</a> <span class="keyword">class </span><a class="code" href="class_r_s_a_prime_selector.html">RSAPrimeSelector</a> : <span class="keyword">public</span> <a class="code" href="class_prime_selector.html">PrimeSelector</a><a name="l00097"></a>00097 {<a name="l00098"></a>00098 <span class="keyword">public</span>:<a name="l00099"></a><a class="code" href="class_r_s_a_prime_selector.html#1e2909a8eacf291aa5f89a90d0fe1623">00099</a>         <a class="code" href="class_r_s_a_prime_selector.html">RSAPrimeSelector</a>(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &amp;e) : <a class="code" href="class_r_s_a_function.html#f6d15cdabeb084e9cdc7efb1bbdea783">m_e</a>(e) {}<a name="l00100"></a><a class="code" href="class_r_s_a_prime_selector.html#0ad0eb88574ade431c00514e132a0a35">00100</a>         <span class="keywordtype">bool</span> IsAcceptable(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &amp;candidate)<span class="keyword"> const </span>{<span class="keywordflow">return</span> RelativelyPrime(<a class="code" href="class_r_s_a_function.html#f6d15cdabeb084e9cdc7efb1bbdea783">m_e</a>, candidate-<a class="code" href="class_integer.html#8c070592581bf6c2f928c72bfa1c1638" title="avoid calling constructors for these frequently used integers">Integer::One</a>());}<a name="l00101"></a><a class="code" href="class_r_s_a_prime_selector.html#748d4eca8b06793bb25ea6f01cbebe01">00101</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.html#f6d15cdabeb084e9cdc7efb1bbdea783">m_e</a>;<a name="l00102"></a>00102 };<a name="l00103"></a>00103 <a name="l00104"></a><a class="code" href="class_invertible_r_s_a_function.html#369c37b8f01c8b64f22baa28811ff72f">00104</a> <span class="keywordtype">void</span> <a class="code" href="class_invertible_r_s_a_function.html#369c37b8f01c8b64f22baa28811ff72f">InvertibleRSAFunction::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="l00105"></a>00105 {<a name="l00106"></a>00106         <span class="keywordtype">int</span> modulusSize = 2048;<a name="l00107"></a>00107         alg.<a class="code" href="class_name_value_pairs.html#39b6daefcabcdd07f5ae482a075e1728" title="get a named value with type int">GetIntValue</a>(<a class="code" href="namespace_name.html#d4bbcf9f235a4aca97cfd03e7733d54a" title="int, in bits">Name::ModulusSize</a>(), modulusSize) || alg.<a class="code" href="class_name_value_pairs.html#39b6daefcabcdd07f5ae482a075e1728" title="get a named value with type int">GetIntValue</a>(<a class="code" href="namespace_name.html#7872612dee54298fcdbca6ac7c297d0a" title="int, in bits">Name::KeySize</a>(), modulusSize);<a name="l00108"></a>00108 <a name="l00109"></a>00109         <span class="keywordflow">if</span> (modulusSize &lt; 16)<a name="l00110"></a>00110                 <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">"InvertibleRSAFunction: specified modulus size is too small"</span>);<a name="l00111"></a>00111 <a name="l00112"></a>00112         <a class="code" href="class_r_s_a_function.html#f6d15cdabeb084e9cdc7efb1bbdea783">m_e</a> = alg.<a class="code" href="class_name_value_pairs.html#943b2009297783f1c35bae46efc3b5f7" title="get a named value, returns the default if the name doesn't exist">GetValueWithDefault</a>(<a class="code" href="namespace_name.html#cc52915312dfc2b01ec47df5066cf1ad" title="Integer.">Name::PublicExponent</a>(), <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a>(17));<a name="l00113"></a>00113 <a name="l00114"></a>00114         <span class="keywordflow">if</span> (<a class="code" href="class_r_s_a_function.html#f6d15cdabeb084e9cdc7efb1bbdea783">m_e</a> &lt; 3 || <a class="code" href="class_r_s_a_function.html#f6d15cdabeb084e9cdc7efb1bbdea783">m_e</a>.<a class="code" href="class_integer.html#fedf9af097a3417d8bd3742ec53f9593">IsEven</a>())<a name="l00115"></a>00115                 <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">"InvertibleRSAFunction: invalid public exponent"</span>);<a name="l00116"></a>00116 <a name="l00117"></a>00117         <a class="code" href="class_r_s_a_prime_selector.html">RSAPrimeSelector</a> selector(<a class="code" href="class_r_s_a_function.html#f6d15cdabeb084e9cdc7efb1bbdea783">m_e</a>);<a name="l00118"></a>00118         <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="l00119"></a>00119                 (<a class="code" href="namespace_name.html#2c58f621e53639c1e9aebac364898756" title="const PrimeSelector *">Name::PointerToPrimeSelector</a>(), selector.<a class="code" href="class_prime_selector.html#0fb5aafa214283370841f019a7b36a4f">GetSelectorPointer</a>());<a name="l00120"></a>00120         <a class="code" href="class_invertible_r_s_a_function.html#0061793214f56717f67956444c935cf9">m_p</a>.<a class="code" href="class_integer.html#0d9a427fe64a454b11b24fe61c10dbeb">GenerateRandom</a>(rng, primeParam);<a name="l00121"></a>00121         <a class="code" href="class_invertible_r_s_a_function.html#48b1e3c5cf1ba5ba83952d46708afaee">m_q</a>.<a class="code" href="class_integer.html#0d9a427fe64a454b11b24fe61c10dbeb">GenerateRandom</a>(rng, primeParam);<a name="l00122"></a>00122 <a name="l00123"></a>00123         <a class="code" href="class_invertible_r_s_a_function.html#e6ebef334123434c80fd0d87dddea8c7">m_d</a> = EuclideanMultiplicativeInverse(<a class="code" href="class_r_s_a_function.html#f6d15cdabeb084e9cdc7efb1bbdea783">m_e</a>, LCM(<a class="code" href="class_invertible_r_s_a_function.html#0061793214f56717f67956444c935cf9">m_p</a>-1, <a class="code" href="class_invertible_r_s_a_function.html#48b1e3c5cf1ba5ba83952d46708afaee">m_q</a>-1));<a name="l00124"></a>00124         assert(<a class="code" href="class_invertible_r_s_a_function.html#e6ebef334123434c80fd0d87dddea8c7">m_d</a>.<a class="code" href="class_integer.html#13ddbfd8e9729932c2a99b0dff530978">IsPositive</a>());<a name="l00125"></a>00125 <a name="l00126"></a>00126         <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> % (<a class="code" href="class_invertible_r_s_a_function.html#0061793214f56717f67956444c935cf9">m_p</a>-1);<a name="l00127"></a>00127         <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> % (<a class="code" href="class_invertible_r_s_a_function.html#48b1e3c5cf1ba5ba83952d46708afaee">m_q</a>-1);<a name="l00128"></a>00128         <a class="code" href="class_r_s_a_function.html#b2e89822414861cb312d52d8c7ac3017">m_n</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#48b1e3c5cf1ba5ba83952d46708afaee">m_q</a>;<a name="l00129"></a>00129         <a class="code" href="class_invertible_r_s_a_function.html#6b7b378c8be194a47faf824844ed6c7b">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_r_s_a_function.html#0061793214f56717f67956444c935cf9">m_p</a>);<a name="l00130"></a>00130 <a name="l00131"></a>00131         <span class="keywordflow">if</span> (FIPS_140_2_ComplianceEnabled())<a name="l00132"></a>00132         {<a name="l00133"></a>00133                 <a class="code" href="struct_r_s_a_s_s.html" title="RSA signature scheme with appendix">RSASS&lt;PKCS1v15, SHA&gt;::Signer</a> signer(*<span class="keyword">this</span>);<a name="l00134"></a>00134                 <a class="code" href="struct_r_s_a_s_s.html" title="RSA signature scheme with appendix">RSASS&lt;PKCS1v15, SHA&gt;::Verifier</a> verifier(signer);<a name="l00135"></a>00135                 SignaturePairwiseConsistencyTest_FIPS_140_Only(signer, verifier);<a name="l00136"></a>00136 <a name="l00137"></a>00137                 <a class="code" href="struct_r_s_a_e_s.html" title="RSA cryptosystem">RSAES&lt;OAEP&lt;SHA&gt;</a> &gt;::Decryptor decryptor(*<span class="keyword">this</span>);<a name="l00138"></a>00138                 <a class="code" href="struct_r_s_a_e_s.html" title="RSA cryptosystem">RSAES&lt;OAEP&lt;SHA&gt;</a> &gt;::Encryptor encryptor(decryptor);<a name="l00139"></a>00139                 EncryptionPairwiseConsistencyTest_FIPS_140_Only(encryptor, decryptor);<a name="l00140"></a>00140         }<a name="l00141"></a>00141 }<a name="l00142"></a>00142 <a name="l00143"></a><a class="code" href="class_invertible_r_s_a_function.html#1d400269a0d1305e93623299d8184e29">00143</a> <span class="keywordtype">void</span> <a class="code" href="class_invertible_r_s_a_function.html#1d400269a0d1305e93623299d8184e29">InvertibleRSAFunction::Initialize</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> keybits, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &amp;e)<a name="l00144"></a>00144 {<a name="l00145"></a>00145         <a class="code" href="class_invertible_r_s_a_function.html#369c37b8f01c8b64f22baa28811ff72f">GenerateRandom</a>(rng, MakeParameters(<a class="code" href="namespace_name.html#d4bbcf9f235a4aca97cfd03e7733d54a" title="int, in bits">Name::ModulusSize</a>(), (<span class="keywordtype">int</span>)keybits)(<a class="code" href="namespace_name.html#cc52915312dfc2b01ec47df5066cf1ad" title="Integer.">Name::PublicExponent</a>(), e+e.<a class="code" href="class_integer.html#fedf9af097a3417d8bd3742ec53f9593">IsEven</a>()));

⌨️ 快捷键说明

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