rabin_8cpp-source.html
来自「Crypto++是一个非常强大的密码学库,主要是功能全」· HTML 代码 · 共 226 行 · 第 1/2 页
HTML
226 行
<!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++: rabin.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>rabin.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// rabin.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="rabin_8h.html">rabin.h</a>"</span>00005 <span class="preprocessor">#include "nbtheory.h"</span>00006 <span class="preprocessor">#include "asn.h"</span>00007 <span class="preprocessor">#include "sha.h"</span>00008 00009 NAMESPACE_BEGIN(CryptoPP)00010 00011 <span class="keywordtype">void</span> RabinFunction::BERDecode(<a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> &bt)00012 {00013 <a class="code" href="class_b_e_r_sequence_decoder.html">BERSequenceDecoder</a> seq(bt);00014 m_n.<a class="code" href="class_integer.html#_integerz39_9">BERDecode</a>(seq);00015 m_r.<a class="code" href="class_integer.html#_integerz39_9">BERDecode</a>(seq);00016 m_s.<a class="code" href="class_integer.html#_integerz39_9">BERDecode</a>(seq);00017 seq.<a class="code" href="class_b_e_r_general_decoder.html#_b_e_r_set_decodera9">MessageEnd</a>();00018 }00019 00020 <span class="keywordtype">void</span> RabinFunction::DEREncode(<a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> &bt)<span class="keyword"> const</span>00021 <span class="keyword"></span>{00022 <a class="code" href="class_d_e_r_sequence_encoder.html">DERSequenceEncoder</a> seq(bt);00023 m_n.<a class="code" href="class_integer.html#_integerz39_3">DEREncode</a>(seq);00024 m_r.<a class="code" href="class_integer.html#_integerz39_3">DEREncode</a>(seq);00025 m_s.<a class="code" href="class_integer.html#_integerz39_3">DEREncode</a>(seq);00026 seq.<a class="code" href="class_d_e_r_general_encoder.html#_d_e_r_set_encodera2">MessageEnd</a>();00027 }00028 00029 <a class="code" href="class_integer.html">Integer</a> RabinFunction::ApplyFunction(<span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &in)<span class="keyword"> const</span>00030 <span class="keyword"></span>{00031 DoQuickSanityCheck();00032 00033 <a class="code" href="class_integer.html">Integer</a> out = in.<a class="code" href="class_integer.html#_integerz49_2">Squared</a>()%m_n;00034 <span class="keywordflow">if</span> (in.<a class="code" href="class_integer.html#_integerz41_15">IsOdd</a>())00035 out = out*m_r%m_n;00036 <span class="keywordflow">if</span> (Jacobi(in, m_n)==-1)00037 out = out*m_s%m_n;00038 <span class="keywordflow">return</span> out;00039 }00040 <a name="l00041"></a><a class="code" href="class_rabin_function.html#_rabin_functiona6">00041</a> <span class="keywordtype">bool</span> <a class="code" href="class_rabin_function.html#_rabin_functiona6">RabinFunction::Validate</a>(<a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a> &rng, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> level)<span class="keyword"> const</span>00042 <span class="keyword"></span>{00043 <span class="keywordtype">bool</span> pass = <span class="keyword">true</span>;00044 pass = pass && m_n > <a class="code" href="class_integer.html#_integerz37_11">Integer::One</a>() && m_n%4 == 1;00045 pass = pass && m_r > <a class="code" href="class_integer.html#_integerz37_11">Integer::One</a>() && m_r < m_n;00046 pass = pass && m_s > <a class="code" href="class_integer.html#_integerz37_11">Integer::One</a>() && m_s < m_n;00047 <span class="keywordflow">if</span> (level >= 1)00048 pass = pass && Jacobi(m_r, m_n) == -1 && Jacobi(m_s, m_n) == -1;00049 <span class="keywordflow">return</span> pass;00050 }00051 <a name="l00052"></a><a class="code" href="class_rabin_function.html#_rabin_functiona7">00052</a> <span class="keywordtype">bool</span> <a class="code" href="class_rabin_function.html#_rabin_functiona7">RabinFunction::GetVoidValue</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *name, <span class="keyword">const</span> std::type_info &valueType, <span class="keywordtype">void</span> *pValue)<span class="keyword"> const</span>00053 <span class="keyword"></span>{00054 <span class="keywordflow">return</span> GetValueHelper(<span class="keyword">this</span>, name, valueType, pValue).Assignable()00055 CRYPTOPP_GET_FUNCTION_ENTRY(<a class="code" href="namespace_name.html#a15">Modulus</a>)00056 CRYPTOPP_GET_FUNCTION_ENTRY(<a class="code" href="namespace_name.html#a29">QuadraticResidueModPrime1</a>)00057 CRYPTOPP_GET_FUNCTION_ENTRY(<a class="code" href="namespace_name.html#a30">QuadraticResidueModPrime2</a>)00058 ;00059 }00060 <a name="l00061"></a><a class="code" href="class_rabin_function.html#_rabin_functiona8">00061</a> <span class="keywordtype">void</span> <a class="code" href="class_rabin_function.html#_rabin_functiona8">RabinFunction::AssignFrom</a>(<span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html">NameValuePairs</a> &source)00062 {00063 AssignFromHelper(<span class="keyword">this</span>, source)00064 CRYPTOPP_SET_FUNCTION_ENTRY(<a class="code" href="namespace_name.html#a15">Modulus</a>)00065 CRYPTOPP_SET_FUNCTION_ENTRY(<a class="code" href="namespace_name.html#a29">QuadraticResidueModPrime1</a>)00066 CRYPTOPP_SET_FUNCTION_ENTRY(<a class="code" href="namespace_name.html#a30">QuadraticResidueModPrime2</a>)00067 ;00068 }00069 00070 <span class="comment">// *****************************************************************************</span>00071 <span class="comment">// private key operations:</span>00072 00073 <span class="comment">// generate a random private key</span><a name="l00074"></a><a class="code" href="class_invertible_rabin_function.html#_invertible_rabin_functiona8">00074</a> <span class="keywordtype">void</span> <a class="code" href="class_invertible_rabin_function.html#_invertible_rabin_functiona8">InvertibleRabinFunction::GenerateRandom</a>(<a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a> &rng, <span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html">NameValuePairs</a> &alg)00075 {00076 <span class="keywordtype">int</span> modulusSize = 2048;00077 alg.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha42">GetIntValue</a>(<span class="stringliteral">"ModulusSize"</span>, modulusSize) || alg.<a class="code" href="class_name_value_pairs.html#_x_t_r___d_ha42">GetIntValue</a>(<span class="stringliteral">"KeySize"</span>, modulusSize);00078 00079 <span class="keywordflow">if</span> (modulusSize < 16)00080 <span class="keywordflow">throw</span> <a class="code" href="class_invalid_argument.html">InvalidArgument</a>(<span class="stringliteral">"InvertibleRabinFunction: specified modulus size is too small"</span>);00081 00082 <span class="comment">// VC70 workaround: putting these after primeParam causes overlapped stack allocation</span>00083 <span class="keywordtype">bool</span> rFound=<span class="keyword">false</span>, sFound=<span class="keyword">false</span>;00084 <a class="code" href="class_integer.html">Integer</a> t=2;00085 00086 <span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html">NameValuePairs</a> &primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize)00087 (<span class="stringliteral">"EquivalentTo"</span>, 3)(<span class="stringliteral">"Mod"</span>, 4);00088 m_p.<a class="code" href="class_integer.html#_integerz43_14">GenerateRandom</a>(rng, primeParam);00089 m_q.<a class="code" href="class_integer.html#_integerz43_14">GenerateRandom</a>(rng, primeParam);00090 00091 <span class="keywordflow">while</span> (!(rFound && sFound))00092 {00093 <span class="keywordtype">int</span> jp = Jacobi(t, m_p);00094 <span class="keywordtype">int</span> jq = Jacobi(t, m_q);00095 00096 <span class="keywordflow">if</span> (!rFound && jp==1 && jq==-1)00097 {00098 m_r = t;00099 rFound = <span class="keyword">true</span>;00100 }00101 00102 <span class="keywordflow">if</span> (!sFound && jp==-1 && jq==1)00103 {00104 m_s = t;00105 sFound = <span class="keyword">true</span>;00106 }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?