Files
C-Plus-Plus/d7/db9/hill__cipher_8cpp.html
2025-08-14 04:29:32 +00:00

392 lines
31 KiB
HTML

<!-- HTML header for doxygen 1.12.0-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.14.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>TheAlgorithms/C++: ciphers/hill_cipher.cpp File Reference</title>
<link rel="icon" href="../../favicon.svg" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@xpack-3rd-party/doxygen-awesome-css@2.2.0-1/doxygen-awesome-darkmode-toggle.js"></script>
<script type="text/javascript">
DoxygenAwesomeDarkModeToggle.init()
</script>
<script type="text/javascript" src="../../clipboard.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<script type="text/javascript" src="../../cookie.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/javascript">
window.MathJax = {
options: {
ignoreHtmlClass: 'tex2jax_ignore',
processHtmlClass: 'tex2jax_process'
},
loader: {
load: ['[tex]/ams']
},
tex: {
macros: {},
packages: ['base','configmacros','ams']
}
};
</script>
<script type="text/javascript" id="MathJax-script" async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../doxygen-awesome.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectlogo"><img alt="Logo" src="../../project_logo.png"/></td>
<td id="projectalign">
<div id="projectname">TheAlgorithms/C++<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">All the algorithms implemented in C++</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.14.0 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
</script>
<script type="text/javascript">
$(function() { codefold.init(); });
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
initMenu('../../',true,false,'search.php','Search',true);
$(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(function(){initNavTree('d7/db9/hill__cipher_8cpp.html','../../',''); });
</script>
<div id="container">
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="headertitle"><div class="title">hill_cipher.cpp File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>Implementation of <a href="https://en.wikipedia.org/wiki/Hill_cipher" target="_blank">Hill cipher</a> algorithm.
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;cassert&gt;</code><br />
<code>#include &lt;cmath&gt;</code><br />
<code>#include &lt;cstdint&gt;</code><br />
<code>#include &lt;cstring&gt;</code><br />
<code>#include &lt;ctime&gt;</code><br />
<code>#include &lt;fstream&gt;</code><br />
<code>#include &lt;iomanip&gt;</code><br />
<code>#include &lt;iostream&gt;</code><br />
<code>#include &lt;string&gt;</code><br />
<code>#include &quot;<a class="el" href="../../d1/dbe/lu__decomposition_8h_source.html">../numerical_methods/lu_decomposition.h</a>&quot;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for hill_cipher.cpp:</div>
<div class="dyncontent">
<div class="center"><div class="zoom"><iframe scrolling="no" loading="lazy" frameborder="0" src="../../d3/d1a/hill__cipher_8cpp__incl.svg" width="100%" height="449"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div></div>
</div>
</div>
<p><a href="../../d7/db9/hill__cipher_8cpp_source.html">Go to the source code of this file.</a></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 id="header-nested-classes" class="groupheader"><a id="nested-classes" name="nested-classes"></a>
Classes</h2></td></tr>
<tr class="memitem:ciphers_3A_3AHillCipher" id="r_ciphers_3A_3AHillCipher"><td class="memItemLeft" align="right" valign="top">class &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d6/d26/classciphers_1_1_hill_cipher.html">ciphers::HillCipher</a></td></tr>
<tr class="memdesc:"><td class="mdescLeft">&#160;</td><td class="mdescRight">Implementation of <a href="https://en.wikipedia.org/wiki/Hill_cipher" target="_blank">Hill Cipher</a> algorithm. <a href="../../d6/d26/classciphers_1_1_hill_cipher.html#details">More...</a><br /></td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 id="header-namespaces" class="groupheader"><a id="namespaces" name="namespaces"></a>
Namespaces</h2></td></tr>
<tr class="memitem:ciphers" id="r_ciphers"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d6/d4e/namespaceciphers.html">ciphers</a></td></tr>
<tr class="memdesc:d6/d4e/namespaceciphers"><td class="mdescLeft">&#160;</td><td class="mdescRight">Algorithms for encryption and decryption. <br /></td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 id="header-func-members" class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a34bfcd756610834acac501f9eea1e2eb" id="r_a34bfcd756610834acac501f9eea1e2eb"><td class="memTemplParams" colspan="2">template&lt;typename T&gt; </td></tr>
<tr class="memitem:a34bfcd756610834acac501f9eea1e2eb template"><td class="memItemLeft" align="right" valign="top">static std::ostream &amp;&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a34bfcd756610834acac501f9eea1e2eb">operator&lt;&lt;</a> (std::ostream &amp;out, <a class="el" href="../../d1/dbe/lu__decomposition_8h.html#ac029b636b353cefbb18b2fcc71e427e4">matrix</a>&lt; T &gt; const &amp;v)</td></tr>
<tr class="memitem:a3147ad576f8a94a2a6b66948672b452b" id="r_a3147ad576f8a94a2a6b66948672b452b"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a3147ad576f8a94a2a6b66948672b452b">test1</a> (const std::string &amp;text)</td></tr>
<tr class="memdesc:a3147ad576f8a94a2a6b66948672b452b"><td class="mdescLeft">&#160;</td><td class="mdescRight">Self test 1 - using 3x3 randomly generated key. <br /></td></tr>
<tr class="memitem:a04391124480d2a49f2dec900237b0712" id="r_a04391124480d2a49f2dec900237b0712"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a04391124480d2a49f2dec900237b0712">test2</a> (const std::string &amp;text)</td></tr>
<tr class="memdesc:a04391124480d2a49f2dec900237b0712"><td class="mdescLeft">&#160;</td><td class="mdescRight">Self test 2 - using 8x8 randomly generated key. <br /></td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4" id="r_ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 id="header-var-members" class="groupheader"><a id="var-members" name="var-members"></a>
Variables</h2></td></tr>
<tr class="memitem:ab9aec0ccf4b6809f652bb540be87c216" id="r_ab9aec0ccf4b6809f652bb540be87c216"><td class="memItemLeft" align="right" valign="top">static const char *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d6/d4e/namespaceciphers.html#ab9aec0ccf4b6809f652bb540be87c216">ciphers::STRKEY</a></td></tr>
</table>
<a name="details" id="details"></a><h2 id="header-details" class="groupheader">Detailed Description</h2>
<div class="textblock"><p>Implementation of <a href="https://en.wikipedia.org/wiki/Hill_cipher" target="_blank">Hill cipher</a> algorithm. </p>
<p>Program to generate the encryption-decryption key and perform encryption and decryption of ASCII text using the famous block cipher algorithm. This is a powerful encryption algorithm that is relatively easy to implement with a given key. The strength of the algorithm depends on the size of the block encryption matrix key; the bigger the matrix, the stronger the encryption and more difficult to break it. However, the important requirement for the matrix is that:</p><ol type="1">
<li>matrix should be invertible - all inversion conditions should be satisfied and</li>
<li>its determinant must not have any common factors with the length of character set Due to this restriction, most implementations only implement with small 3x3 encryption keys and a small subset of ASCII alphabets.</li>
</ol>
<p>In the current implementation, I present to you an implementation for generating larger encryption keys (I have attempted upto 10x10) and an ASCII character set of 97 printable characters. Hence, a typical ASCII text file could be easily encrypted with the module. The larger character set increases the modulo of cipher and hence the matrix determinants can get very large very quickly rendering them ill-defined.</p>
<dl class="section note"><dt>Note</dt><dd>This program uses determinant computation using LU decomposition from the file <a class="el" href="../../d1/dbe/lu__decomposition_8h.html" title="Functions associated with LU Decomposition of a square matrix.">lu_decomposition.h</a> </dd>
<dd>
The matrix generation algorithm is very rudimentary and does not guarantee an invertible modulus matrix. </dd></dl>
<dl class="todo"><dt><b><a class="el" href="../../dd/da0/todo.html#_todo000001">Todo</a></b></dt><dd>Better matrix generation algorithm.</dd></dl>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala" target="_blank">Krishna Vedala</a> </dd></dl>
<p class="definition">Definition in file <a class="el" href="../../d7/db9/hill__cipher_8cpp_source.html">hill_cipher.cpp</a>.</p>
</div><a name="doc-func-members" id="doc-func-members"></a><h2 id="header-doc-func-members" class="groupheader">Function Documentation</h2>
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">&#9670;&#160;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">void</td> <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function </p>
<p class="definition">Definition at line <a class="el" href="../../d7/db9/hill__cipher_8cpp_source.html#l00532">532</a> of file <a class="el" href="../../d7/db9/hill__cipher_8cpp_source.html">hill_cipher.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 532</span> {</div>
<div class="line"><span class="lineno"> 533</span> std::srand(std::time(<span class="keyword">nullptr</span>));</div>
<div class="line"><span class="lineno"> 534</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Key dictionary: (&quot;</span> &lt;&lt; std::strlen(<a class="code hl_variable" href="../../d6/d4e/namespaceciphers.html#ab9aec0ccf4b6809f652bb540be87c216">ciphers::STRKEY</a>) &lt;&lt; <span class="stringliteral">&quot;)\n\t&quot;</span></div>
<div class="line"><span class="lineno"> 535</span> &lt;&lt; <a class="code hl_variable" href="../../d6/d4e/namespaceciphers.html#ab9aec0ccf4b6809f652bb540be87c216">ciphers::STRKEY</a> &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="line"><span class="lineno"> 536</span> </div>
<div class="line"><span class="lineno"> 537</span> std::string text = <span class="stringliteral">&quot;This is a simple text with numb3r5 and exclamat!0n.&quot;</span>;</div>
<div class="line"><span class="lineno"> 538</span> </div>
<div class="line"><span class="lineno"> 539</span> <a class="code hl_function" href="../../d3/dae/dsu__path__compression_8cpp.html#ae7880ce913f3058a35ff106d5be9e243">test1</a>(text);</div>
<div class="line"><span class="lineno"> 540</span> <a class="code hl_function" href="../../d3/dae/dsu__path__compression_8cpp.html#a45d94ead4cf4e1ff9f87c38bc99f59ae">test2</a>(text);</div>
<div class="line"><span class="lineno"> 541</span> </div>
<div class="line"><span class="lineno"> 542</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 543</span>}</div>
<div class="ttc" id="adsu__path__compression_8cpp_html_a45d94ead4cf4e1ff9f87c38bc99f59ae"><div class="ttname"><a href="../../d3/dae/dsu__path__compression_8cpp.html#a45d94ead4cf4e1ff9f87c38bc99f59ae">test2</a></div><div class="ttdeci">static void test2()</div><div class="ttdoc">Self-implementations, 2nd test.</div><div class="ttdef"><b>Definition</b> <a href="../../d3/dae/dsu__path__compression_8cpp_source.html#l00187">dsu_path_compression.cpp:187</a></div></div>
<div class="ttc" id="adsu__path__compression_8cpp_html_ae7880ce913f3058a35ff106d5be9e243"><div class="ttname"><a href="../../d3/dae/dsu__path__compression_8cpp.html#ae7880ce913f3058a35ff106d5be9e243">test1</a></div><div class="ttdeci">static void test1()</div><div class="ttdoc">Self-test implementations, 1st test.</div><div class="ttdef"><b>Definition</b> <a href="../../d3/dae/dsu__path__compression_8cpp_source.html#l00170">dsu_path_compression.cpp:170</a></div></div>
<div class="ttc" id="anamespaceciphers_html_ab9aec0ccf4b6809f652bb540be87c216"><div class="ttname"><a href="../../d6/d4e/namespaceciphers.html#ab9aec0ccf4b6809f652bb540be87c216">ciphers::STRKEY</a></div><div class="ttdeci">static const char * STRKEY</div><div class="ttdef"><b>Definition</b> <a href="../../d7/db9/hill__cipher_8cpp_source.html#l00074">hill_cipher.cpp:74</a></div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a34bfcd756610834acac501f9eea1e2eb" name="a34bfcd756610834acac501f9eea1e2eb"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a34bfcd756610834acac501f9eea1e2eb">&#9670;&#160;</a></span>operator&lt;&lt;()</h2>
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;typename T&gt; </div>
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname">std::ostream &amp; operator&lt;&lt; </td>
<td>(</td>
<td class="paramtype">std::ostream &amp;</td> <td class="paramname"><span class="paramname"><em>out</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype"><a class="el" href="../../d1/dbe/lu__decomposition_8h.html#ac029b636b353cefbb18b2fcc71e427e4">matrix</a>&lt; T &gt; const &amp;</td> <td class="paramname"><span class="paramname"><em>v</em></span>&#160;)</td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel static">static</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>operator to print a matrix </p>
<p class="definition">Definition at line <a class="el" href="../../d7/db9/hill__cipher_8cpp_source.html#l00055">55</a> of file <a class="el" href="../../d7/db9/hill__cipher_8cpp_source.html">hill_cipher.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 55</span> {</div>
<div class="line"><span class="lineno"> 56</span> <span class="keyword">const</span> <span class="keywordtype">int</span> width = 15;</div>
<div class="line"><span class="lineno"> 57</span> <span class="keyword">const</span> <span class="keywordtype">char</span> separator = <span class="charliteral">&#39; &#39;</span>;</div>
<div class="line"><span class="lineno"> 58</span> </div>
<div class="line"><span class="lineno"> 59</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> row = 0; row &lt; v.size(); row++) {</div>
<div class="line"><span class="lineno"> 60</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> col = 0; col &lt; v[row].size(); col++)</div>
<div class="line"><span class="lineno"> 61</span> out &lt;&lt; std::left &lt;&lt; std::setw(width) &lt;&lt; std::setfill(separator)</div>
<div class="line"><span class="lineno"> 62</span> &lt;&lt; v[row][col];</div>
<div class="line"><span class="lineno"> 63</span> out &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 64</span> }</div>
<div class="line"><span class="lineno"> 65</span> </div>
<div class="line"><span class="lineno"> 66</span> <span class="keywordflow">return</span> out;</div>
<div class="line"><span class="lineno"> 67</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a3147ad576f8a94a2a6b66948672b452b" name="a3147ad576f8a94a2a6b66948672b452b"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a3147ad576f8a94a2a6b66948672b452b">&#9670;&#160;</a></span>test1()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void test1 </td>
<td>(</td>
<td class="paramtype">const std::string &amp;</td> <td class="paramname"><span class="paramname"><em>text</em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Self test 1 - using 3x3 randomly generated key. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">text</td><td>string to encrypt and decrypt </td></tr>
</table>
</dd>
</dl>
<p class="definition">Definition at line <a class="el" href="../../d7/db9/hill__cipher_8cpp_source.html#l00470">470</a> of file <a class="el" href="../../d7/db9/hill__cipher_8cpp_source.html">hill_cipher.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 470</span> {</div>
<div class="line"><span class="lineno"> 471</span> <span class="comment">// std::string text = &quot;Hello world!&quot;;</span></div>
<div class="line"><span class="lineno"> 472</span> std::cout &lt;&lt; <span class="stringliteral">&quot;======Test 1 (3x3 key) ======\nOriginal text:\n\t&quot;</span> &lt;&lt; text</div>
<div class="line"><span class="lineno"> 473</span> &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 474</span> </div>
<div class="line"><span class="lineno"> 475</span> std::pair&lt;matrix&lt;int&gt;, <a class="code hl_typedef" href="../../d1/dbe/lu__decomposition_8h.html#ac029b636b353cefbb18b2fcc71e427e4">matrix&lt;int&gt;</a>&gt; p =</div>
<div class="line"><span class="lineno"> 476</span> <a class="code hl_function" href="../../d6/d26/classciphers_1_1_hill_cipher.html#a405b0a28d66a61239d3565d5256f9cb5">ciphers::HillCipher::generate_keys</a>(3, 0, 100);</div>
<div class="line"><span class="lineno"> 477</span> <a class="code hl_typedef" href="../../d1/dbe/lu__decomposition_8h.html#ac029b636b353cefbb18b2fcc71e427e4">matrix&lt;int&gt;</a> ekey = p.first;</div>
<div class="line"><span class="lineno"> 478</span> <a class="code hl_typedef" href="../../d1/dbe/lu__decomposition_8h.html#ac029b636b353cefbb18b2fcc71e427e4">matrix&lt;int&gt;</a> dkey = p.second;</div>
<div class="line"><span class="lineno"> 479</span> </div>
<div class="line"><span class="lineno"> 480</span> <span class="comment">// matrix&lt;int&gt; ekey = {{22, 28, 25}, {5, 26, 15}, {14, 18, 9}};</span></div>
<div class="line"><span class="lineno"> 481</span> <span class="comment">// std::cout &lt;&lt; &quot;Encryption key: \n&quot; &lt;&lt; ekey;</span></div>
<div class="line"><span class="lineno"> 482</span> std::string gibberish = <a class="code hl_function" href="../../d6/d26/classciphers_1_1_hill_cipher.html#aa8bbb6e4a5749f6008b06602d5103917">ciphers::HillCipher::encrypt_text</a>(text, ekey);</div>
<div class="line"><span class="lineno"> 483</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Encrypted text:\n\t&quot;</span> &lt;&lt; gibberish &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 484</span> </div>
<div class="line"><span class="lineno"> 485</span> <span class="comment">// matrix&lt;int&gt; dkey = ciphers::HillCipher::generate_decryption_key(ekey);</span></div>
<div class="line"><span class="lineno"> 486</span> <span class="comment">// std::cout &lt;&lt; &quot;Decryption key: \n&quot; &lt;&lt; dkey;</span></div>
<div class="line"><span class="lineno"> 487</span> std::string txt_back = <a class="code hl_function" href="../../d6/d26/classciphers_1_1_hill_cipher.html#a427acfac1dbff3f48a2b071d449d965b">ciphers::HillCipher::decrypt_text</a>(gibberish, dkey);</div>
<div class="line"><span class="lineno"> 488</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Reconstruct text:\n\t&quot;</span> &lt;&lt; txt_back &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 489</span> </div>
<div class="line"><span class="lineno"> 490</span> std::ofstream out_file(<span class="stringliteral">&quot;hill_cipher_test1.txt&quot;</span>);</div>
<div class="line"><span class="lineno"> 491</span> out_file &lt;&lt; <span class="stringliteral">&quot;Block size: &quot;</span> &lt;&lt; ekey.size() &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="line"><span class="lineno"> 492</span> out_file &lt;&lt; <span class="stringliteral">&quot;Encryption Key:\n&quot;</span> &lt;&lt; ekey;</div>
<div class="line"><span class="lineno"> 493</span> out_file &lt;&lt; <span class="stringliteral">&quot;\nDecryption Key:\n&quot;</span> &lt;&lt; dkey;</div>
<div class="line"><span class="lineno"> 494</span> out_file.close();</div>
<div class="line"><span class="lineno"> 495</span> </div>
<div class="line"><span class="lineno"> 496</span> assert(txt_back == text);</div>
<div class="line"><span class="lineno"> 497</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Passed :)\n&quot;</span>;</div>
<div class="line"><span class="lineno"> 498</span>}</div>
<div class="ttc" id="aclassciphers_1_1_hill_cipher_html_a405b0a28d66a61239d3565d5256f9cb5"><div class="ttname"><a href="../../d6/d26/classciphers_1_1_hill_cipher.html#a405b0a28d66a61239d3565d5256f9cb5">ciphers::HillCipher::generate_keys</a></div><div class="ttdeci">static std::pair&lt; matrix&lt; int &gt;, matrix&lt; int &gt; &gt; generate_keys(size_t size, int limit1=0, int limit2=10)</div><div class="ttdoc">Generate encryption and decryption key pair.</div><div class="ttdef"><b>Definition</b> <a href="../../d7/db9/hill__cipher_8cpp_source.html#l00424">hill_cipher.cpp:424</a></div></div>
<div class="ttc" id="aclassciphers_1_1_hill_cipher_html_a427acfac1dbff3f48a2b071d449d965b"><div class="ttname"><a href="../../d6/d26/classciphers_1_1_hill_cipher.html#a427acfac1dbff3f48a2b071d449d965b">ciphers::HillCipher::decrypt_text</a></div><div class="ttdeci">static const std::string decrypt_text(const std::string &amp;text, const matrix&lt; int &gt; &amp;decrypt_key)</div><div class="ttdoc">Decrypt a given text using a given key.</div><div class="ttdef"><b>Definition</b> <a href="../../d7/db9/hill__cipher_8cpp_source.html#l00457">hill_cipher.cpp:457</a></div></div>
<div class="ttc" id="aclassciphers_1_1_hill_cipher_html_aa8bbb6e4a5749f6008b06602d5103917"><div class="ttname"><a href="../../d6/d26/classciphers_1_1_hill_cipher.html#aa8bbb6e4a5749f6008b06602d5103917">ciphers::HillCipher::encrypt_text</a></div><div class="ttdeci">static const std::string encrypt_text(const std::string &amp;text, const matrix&lt; int &gt; &amp;encrypt_key)</div><div class="ttdoc">Encrypt a given text using a given key.</div><div class="ttdef"><b>Definition</b> <a href="../../d7/db9/hill__cipher_8cpp_source.html#l00445">hill_cipher.cpp:445</a></div></div>
<div class="ttc" id="alu__decomposition_8h_html_ac029b636b353cefbb18b2fcc71e427e4"><div class="ttname"><a href="../../d1/dbe/lu__decomposition_8h.html#ac029b636b353cefbb18b2fcc71e427e4">matrix</a></div><div class="ttdeci">std::vector&lt; std::valarray&lt; T &gt; &gt; matrix</div><div class="ttdef"><b>Definition</b> <a href="../../d1/dbe/lu__decomposition_8h_source.html#l00019">lu_decomposition.h:19</a></div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a04391124480d2a49f2dec900237b0712" name="a04391124480d2a49f2dec900237b0712"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a04391124480d2a49f2dec900237b0712">&#9670;&#160;</a></span>test2()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void test2 </td>
<td>(</td>
<td class="paramtype">const std::string &amp;</td> <td class="paramname"><span class="paramname"><em>text</em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Self test 2 - using 8x8 randomly generated key. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">text</td><td>string to encrypt and decrypt </td></tr>
</table>
</dd>
</dl>
<p class="definition">Definition at line <a class="el" href="../../d7/db9/hill__cipher_8cpp_source.html#l00505">505</a> of file <a class="el" href="../../d7/db9/hill__cipher_8cpp_source.html">hill_cipher.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 505</span> {</div>
<div class="line"><span class="lineno"> 506</span> <span class="comment">// std::string text = &quot;Hello world!&quot;;</span></div>
<div class="line"><span class="lineno"> 507</span> std::cout &lt;&lt; <span class="stringliteral">&quot;======Test 2 (8x8 key) ======\nOriginal text:\n\t&quot;</span> &lt;&lt; text</div>
<div class="line"><span class="lineno"> 508</span> &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 509</span> </div>
<div class="line"><span class="lineno"> 510</span> std::pair&lt;matrix&lt;int&gt;, <a class="code hl_typedef" href="../../d1/dbe/lu__decomposition_8h.html#ac029b636b353cefbb18b2fcc71e427e4">matrix&lt;int&gt;</a>&gt; p =</div>
<div class="line"><span class="lineno"> 511</span> <a class="code hl_function" href="../../d6/d26/classciphers_1_1_hill_cipher.html#a405b0a28d66a61239d3565d5256f9cb5">ciphers::HillCipher::generate_keys</a>(8, 0, 3);</div>
<div class="line"><span class="lineno"> 512</span> <a class="code hl_typedef" href="../../d1/dbe/lu__decomposition_8h.html#ac029b636b353cefbb18b2fcc71e427e4">matrix&lt;int&gt;</a> ekey = p.first;</div>
<div class="line"><span class="lineno"> 513</span> <a class="code hl_typedef" href="../../d1/dbe/lu__decomposition_8h.html#ac029b636b353cefbb18b2fcc71e427e4">matrix&lt;int&gt;</a> dkey = p.second;</div>
<div class="line"><span class="lineno"> 514</span> </div>
<div class="line"><span class="lineno"> 515</span> std::string gibberish = <a class="code hl_function" href="../../d6/d26/classciphers_1_1_hill_cipher.html#aa8bbb6e4a5749f6008b06602d5103917">ciphers::HillCipher::encrypt_text</a>(text, ekey);</div>
<div class="line"><span class="lineno"> 516</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Encrypted text:\n\t&quot;</span> &lt;&lt; gibberish &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 517</span> </div>
<div class="line"><span class="lineno"> 518</span> std::string txt_back = <a class="code hl_function" href="../../d6/d26/classciphers_1_1_hill_cipher.html#a427acfac1dbff3f48a2b071d449d965b">ciphers::HillCipher::decrypt_text</a>(gibberish, dkey);</div>
<div class="line"><span class="lineno"> 519</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Reconstruct text:\n\t&quot;</span> &lt;&lt; txt_back &lt;&lt; std::endl;</div>
<div class="line"><span class="lineno"> 520</span> </div>
<div class="line"><span class="lineno"> 521</span> std::ofstream out_file(<span class="stringliteral">&quot;hill_cipher_test2.txt&quot;</span>);</div>
<div class="line"><span class="lineno"> 522</span> out_file &lt;&lt; <span class="stringliteral">&quot;Block size: &quot;</span> &lt;&lt; ekey.size() &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="line"><span class="lineno"> 523</span> out_file &lt;&lt; <span class="stringliteral">&quot;Encryption Key:\n&quot;</span> &lt;&lt; ekey;</div>
<div class="line"><span class="lineno"> 524</span> out_file &lt;&lt; <span class="stringliteral">&quot;\nDecryption Key:\n&quot;</span> &lt;&lt; dkey;</div>
<div class="line"><span class="lineno"> 525</span> out_file.close();</div>
<div class="line"><span class="lineno"> 526</span> </div>
<div class="line"><span class="lineno"> 527</span> assert(txt_back.compare(0, text.size(), text) == 0);</div>
<div class="line"><span class="lineno"> 528</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Passed :)\n&quot;</span>;</div>
<div class="line"><span class="lineno"> 529</span>}</div>
</div><!-- fragment -->
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<div id="page-nav" class="page-nav-panel">
<div id="page-nav-resize-handle"></div>
<div id="page-nav-tree">
<div id="page-nav-contents">
</div><!-- page-nav-contents -->
</div><!-- page-nav-tree -->
</div><!-- page-nav -->
</div><!-- container -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a href="../../dir_4d6e05837bf820fb089a8a8cdf2f42b7.html">ciphers</a></li><li class="navelem"><a href="../../d7/db9/hill__cipher_8cpp.html">hill_cipher.cpp</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.14.0 </li>
</ul>
</div>
</body>
</html>