Files
C-Plus-Plus/d7/db9/hill__cipher_8cpp.html
2023-06-16 21:23:15 +00:00

415 lines
38 KiB
HTML

<!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.9.6"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C++: ciphers/hill_cipher.cpp File Reference</title>
<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>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.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/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.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="projectalign">
<div id="projectname">Algorithms_in_C++<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C++.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.6 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</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">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('d7/db9/hill__cipher_8cpp.html','../../'); initResizable(); });
/* @license-end */
</script>
<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="summary">
<a href="#nested-classes">Classes</a> &#124;
<a href="#namespaces">Namespaces</a> &#124;
<a href="#func-members">Functions</a> &#124;
<a href="#var-members">Variables</a> </div>
<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;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" 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><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="nested-classes" name="nested-classes"></a>
Classes</h2></td></tr>
<tr class="memitem:"><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>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="namespaces" name="namespaces"></a>
Namespaces</h2></td></tr>
<tr class="memitem:d6/d4e/namespaceciphers"><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>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a34bfcd756610834acac501f9eea1e2eb"><td class="memTemplParams" colspan="2">template&lt;typename T &gt; </td></tr>
<tr class="memitem:a34bfcd756610834acac501f9eea1e2eb"><td class="memTemplItemLeft" align="right" valign="top">static <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::ostream</a> &amp;&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="../../d7/db9/hill__cipher_8cpp.html#a34bfcd756610834acac501f9eea1e2eb">operator&lt;&lt;</a> (<a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::ostream</a> &amp;out, <a class="el" href="../../d1/dbe/lu__decomposition_8h.html#aed8766713ee9b561a4acdcdff5f90ea5">matrix</a>&lt; T &gt; const &amp;v)</td></tr>
<tr class="separator:a34bfcd756610834acac501f9eea1e2eb"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a3147ad576f8a94a2a6b66948672b452b"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/db9/hill__cipher_8cpp.html#a3147ad576f8a94a2a6b66948672b452b">test1</a> (const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> &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="separator:a3147ad576f8a94a2a6b66948672b452b"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a04391124480d2a49f2dec900237b0712"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/db9/hill__cipher_8cpp.html#a04391124480d2a49f2dec900237b0712">test2</a> (const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> &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="separator:a04391124480d2a49f2dec900237b0712"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d7/db9/hill__cipher_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="var-members" name="var-members"></a>
Variables</h2></td></tr>
<tr class="memitem: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>
<tr class="separator:ab9aec0ccf4b6809f652bb540be87c216"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 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>
</div><h2 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&#160;</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function </p>
<div class="fragment"><div class="line"><span class="lineno"> 532</span> {</div>
<div class="line"><span class="lineno"> 533</span> <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/random/srand.html">std::srand</a>(<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/chrono/c/time.html">std::time</a>(<span class="keyword">nullptr</span>));</div>
<div class="line"><span class="lineno"> 534</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Key dictionary: (&quot;</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/byte/strlen.html">std::strlen</a>(<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> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> 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="abasic_ostream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div></div>
<div class="ttc" id="abasic_string_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a></div></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> dsu_path_compression.cpp:186</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> dsu_path_compression.cpp:169</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> hill_cipher.cpp:73</div></div>
<div class="ttc" id="asrand_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/numeric/random/srand.html">std::srand</a></div><div class="ttdeci">T srand(T... args)</div></div>
<div class="ttc" id="astrlen_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/string/byte/strlen.html">std::strlen</a></div><div class="ttdeci">T strlen(T... args)</div></div>
<div class="ttc" id="atime_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/chrono/c/time.html">std::time</a></div><div class="ttdeci">T time(T... args)</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d7/db9/hill__cipher_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="628" height="410"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</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">static <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::ostream</a> &amp; operator&lt;&lt; </td>
<td>(</td>
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::ostream</a> &amp;&#160;</td>
<td class="paramname"><em>out</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype"><a class="el" href="../../d1/dbe/lu__decomposition_8h.html#aed8766713ee9b561a4acdcdff5f90ea5">matrix</a>&lt; T &gt; const &amp;&#160;</td>
<td class="paramname"><em>v</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel">static</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>operator to print a matrix </p>
<div class="fragment"><div class="line"><span class="lineno"> 54</span> {</div>
<div class="line"><span class="lineno"> 55</span> <span class="keyword">const</span> <span class="keywordtype">int</span> width = 15;</div>
<div class="line"><span class="lineno"> 56</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"> 57</span> </div>
<div class="line"><span class="lineno"> 58</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> row = 0; row &lt; v.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector/size.html">size</a>(); row++) {</div>
<div class="line"><span class="lineno"> 59</span> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> col = 0; col &lt; v[row].<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector/size.html">size</a>(); col++)</div>
<div class="line"><span class="lineno"> 60</span> out &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/left.html">std::left</a> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/setw.html">std::setw</a>(width) &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/setfill.html">std::setfill</a>(separator)</div>
<div class="line"><span class="lineno"> 61</span> &lt;&lt; v[row][col];</div>
<div class="line"><span class="lineno"> 62</span> out &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 63</span> }</div>
<div class="line"><span class="lineno"> 64</span> </div>
<div class="line"><span class="lineno"> 65</span> <span class="keywordflow">return</span> out;</div>
<div class="line"><span class="lineno"> 66</span>}</div>
<div class="ttc" id="aendl_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a></div><div class="ttdeci">T endl(T... args)</div></div>
<div class="ttc" id="aleft_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/left.html">std::left</a></div><div class="ttdeci">T left(T... args)</div></div>
<div class="ttc" id="asetfill_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/setfill.html">std::setfill</a></div><div class="ttdeci">T setfill(T... args)</div></div>
<div class="ttc" id="asetw_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/setw.html">std::setw</a></div><div class="ttdeci">T setw(T... args)</div></div>
<div class="ttc" id="asize_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/vector/size.html">std::vector::size</a></div><div class="ttdeci">T size(T... args)</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d7/db9/hill__cipher_8cpp_a34bfcd756610834acac501f9eea1e2eb_cgraph.svg" width="259" height="234"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</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 <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> &amp;&#160;</td>
<td class="paramname"><em>text</em></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>
<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> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &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; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 474</span> </div>
<div class="line"><span class="lineno"> 475</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/utility/pair.html">std::pair&lt;matrix&lt;int&gt;</a>, <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">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_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">matrix&lt;int&gt;</a> ekey = p.first;</div>
<div class="line"><span class="lineno"> 478</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">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> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> 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> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Encrypted text:\n\t&quot;</span> &lt;&lt; gibberish &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</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> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> 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> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Reconstruct text:\n\t&quot;</span> &lt;&lt; txt_back &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 489</span> </div>
<div class="line"><span class="lineno"> 490</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ofstream.html">std::ofstream</a> 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.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector/size.html">size</a>() &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> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Passed :)\n&quot;</span>;</div>
<div class="line"><span class="lineno"> 498</span>}</div>
<div class="ttc" id="abasic_ofstream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_ofstream.html">std::ofstream</a></div></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> hill_cipher.cpp:424</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> hill_cipher.cpp:457</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> hill_cipher.cpp:445</div></div>
<div class="ttc" id="apair_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/utility/pair.html">std::pair</a></div></div>
<div class="ttc" id="avector_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a></div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><div class="zoom"><iframe scrolling="no" frameborder="0" src="../../d7/db9/hill__cipher_8cpp_a3147ad576f8a94a2a6b66948672b452b_cgraph.svg" width="100%" height="600"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</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 <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> &amp;&#160;</td>
<td class="paramname"><em>text</em></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>
<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> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &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; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 509</span> </div>
<div class="line"><span class="lineno"> 510</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/utility/pair.html">std::pair&lt;matrix&lt;int&gt;</a>, <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">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_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">matrix&lt;int&gt;</a> ekey = p.first;</div>
<div class="line"><span class="lineno"> 513</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">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> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> 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> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Encrypted text:\n\t&quot;</span> &lt;&lt; gibberish &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 517</span> </div>
<div class="line"><span class="lineno"> 518</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> 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> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Reconstruct text:\n\t&quot;</span> &lt;&lt; txt_back &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 520</span> </div>
<div class="line"><span class="lineno"> 521</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ofstream.html">std::ofstream</a> 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.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector/size.html">size</a>() &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.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/compare.html">compare</a>(0, text.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">size</a>(), text) == 0);</div>
<div class="line"><span class="lineno"> 528</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Passed :)\n&quot;</span>;</div>
<div class="line"><span class="lineno"> 529</span>}</div>
<div class="ttc" id="acompare_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/string/basic_string/compare.html">std::string::compare</a></div><div class="ttdeci">T compare(T... args)</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><div class="zoom"><iframe scrolling="no" frameborder="0" src="../../d7/db9/hill__cipher_8cpp_a04391124480d2a49f2dec900237b0712_cgraph.svg" width="100%" height="600"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_4d6e05837bf820fb089a8a8cdf2f42b7.html">ciphers</a></li><li class="navelem"><a class="el" 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.9.6 </li>
</ul>
</div>
</body>
</html>