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

📄 elgamal_8h-source.html

📁 Crypto++是一个非常强大的密码学库,主要是功能全
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!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++: elgamal.h 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&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Compound&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Compound&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a></div><h1>elgamal.h</h1><div class="fragment"><pre>00001 <span class="preprocessor">#ifndef CRYPTOPP_ELGAMAL_H</span>00002 <span class="preprocessor"></span><span class="preprocessor">#define CRYPTOPP_ELGAMAL_H</span>00003 <span class="preprocessor"></span>00004 <span class="preprocessor">#include "modexppc.h"</span>00005 <span class="preprocessor">#include "<a class="code" href="dsa_8h.html">dsa.h</a>"</span>00006 00007 NAMESPACE_BEGIN(CryptoPP)00008 00009 <span class="keyword">class </span>ElGamalBase : <span class="keyword">public</span> <a class="code" href="class_d_l___key_agreement_algorithm___d_h.html">DL_KeyAgreementAlgorithm_DH</a>&lt;Integer, NoCofactorMultiplication&gt;, 00010                                         <span class="keyword">public</span> <a class="code" href="class_d_l___key_derivation_algorithm.html">DL_KeyDerivationAlgorithm</a>&lt;Integer&gt;, 00011                                         <span class="keyword">public</span> <a class="code" href="class_d_l___symmetric_encryption_algorithm.html">DL_SymmetricEncryptionAlgorithm</a>00012 {00013 <span class="keyword">public</span>:00014         <span class="keywordtype">void</span> Derive(<span class="keyword">const</span> <a class="code" href="class_d_l___group_parameters.html">DL_GroupParameters&lt;Integer&gt;</a> &amp;params, byte *derivedKey, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> derivedLength, <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;agreedElement, <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;ephemeralPublicKey)<span class="keyword"> const</span>00015 <span class="keyword">        </span>{00016                 agreedElement.<a class="code" href="class_integer.html#_integerz39_1">Encode</a>(derivedKey, derivedLength);00017         }00018 00019         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> GetSymmetricKeyLength(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> plainTextLength)<span class="keyword"> const</span>00020 <span class="keyword">        </span>{00021                 <span class="keywordflow">return</span> GetGroupParameters().GetModulus().ByteCount();00022         }00023 00024         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> GetSymmetricCiphertextLength(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> plainTextLength)<span class="keyword"> const</span>00025 <span class="keyword">        </span>{00026                 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len = GetGroupParameters().GetModulus().ByteCount();00027                 <span class="keywordflow">if</span> (plainTextLength &lt;= GetMaxSymmetricPlaintextLength(len))00028                         <span class="keywordflow">return</span> len;00029                 <span class="keywordflow">else</span>00030                         <span class="keywordflow">return</span> 0;00031         }00032 00033         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> GetMaxSymmetricPlaintextLength(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> cipherTextLength)<span class="keyword"> const</span>00034 <span class="keyword">        </span>{00035                 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len = GetGroupParameters().GetModulus().ByteCount();00036                 <span class="keywordflow">if</span> (cipherTextLength == len)00037                         <span class="keywordflow">return</span> STDMIN(255U, len-3);00038                 <span class="keywordflow">else</span>00039                         <span class="keywordflow">return</span> 0;00040         }00041 00042         <span class="keywordtype">void</span> SymmetricEncrypt(<a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a> &amp;rng, <span class="keyword">const</span> byte *key, <span class="keyword">const</span> byte *plainText, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> plainTextLength, byte *cipherText)<span class="keyword"> const</span>00043 <span class="keyword">        </span>{00044                 <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;p = GetGroupParameters().GetModulus();00045                 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> modulusLen = p.<a class="code" href="class_integer.html#_integerz41_3">ByteCount</a>();00046 00047                 <a class="code" href="class_sec_block.html">SecByteBlock</a> block(modulusLen-1);00048                 rng.<a class="code" href="class_random_number_generator.html#_x917_r_n_ga4">GenerateBlock</a>(block, modulusLen-2-plainTextLength);00049                 memcpy(block+modulusLen-2-plainTextLength, plainText, plainTextLength);00050                 block[modulusLen-2] = plainTextLength;00051 00052                 a_times_b_mod_c(<a class="code" href="class_integer.html">Integer</a>(key, modulusLen), <a class="code" href="class_integer.html">Integer</a>(block, modulusLen-1), p).<a class="code" href="class_integer.html#_integerz39_1">Encode</a>(cipherText, modulusLen);00053         }00054 00055         <a class="code" href="struct_decoding_result.html">DecodingResult</a> SymmetricDecrypt(<span class="keyword">const</span> byte *key, <span class="keyword">const</span> byte *cipherText, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> cipherTextLength, byte *plainText)<span class="keyword"> const</span>00056 <span class="keyword">        </span>{00057                 <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;p = GetGroupParameters().GetModulus();00058                 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> modulusLen = p.<a class="code" href="class_integer.html#_integerz41_3">ByteCount</a>();00059 00060                 <span class="keywordflow">if</span> (cipherTextLength != modulusLen)00061                         <span class="keywordflow">return</span> <a class="code" href="struct_decoding_result.html">DecodingResult</a>();00062 00063                 <a class="code" href="class_integer.html">Integer</a> m = a_times_b_mod_c(<a class="code" href="class_integer.html">Integer</a>(cipherText, modulusLen), <a class="code" href="class_integer.html">Integer</a>(key, modulusLen).InverseMod(p), p);00064 00065                 m.<a class="code" href="class_integer.html#_integerz39_1">Encode</a>(plainText, 1);00066                 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> plainTextLength = plainText[0];00067                 <span class="keywordflow">if</span> (plainTextLength &gt; GetMaxSymmetricPlaintextLength(modulusLen))00068                         <span class="keywordflow">return</span> <a class="code" href="struct_decoding_result.html">DecodingResult</a>();00069                 m &gt;&gt;= 8;

⌨️ 快捷键说明

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