This commit is contained in:
Crystal 2024-03-17 21:46:44 +01:00
parent 8347b8d1f7
commit e8c190c8b2
4 changed files with 137 additions and 57 deletions

View File

@ -3,7 +3,7 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2024-03-07 Thu 20:55 -->
<!-- 2024-03-17 Sun 21:28 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>x86 Assembly from my understanding</title>
@ -23,9 +23,9 @@
<p>
Soooo this article (or maybe even a series of articles, who knows ?) will be about x86 assembly, or rather, what I understood from it and my road from the bottom-up hopefully reaching a good level of understanding
</p>
<div id="outline-container-org924bef5" class="outline-2">
<h2 id="org924bef5">Memory :</h2>
<div class="outline-text-2" id="text-org924bef5">
<div id="outline-container-orgd547ad6" class="outline-2">
<h2 id="orgd547ad6">Memory :</h2>
<div class="outline-text-2" id="text-orgd547ad6">
<p>
Memory is a sequence of octets (Aka 8bits) that each have a unique integer assigned to them called <b>The Effective Address (EA)</b>, in this particular CPU Architecture (the i8086), the octet is designated by a couple (A segment number, and the offset in the segment)
</p>
@ -40,9 +40,9 @@ Memory is a sequence of octets (Aka 8bits) that each have a unique integer assig
The offset and segment are encoded in 16bits, so they take a value between 0 and 65535
</p>
</div>
<div id="outline-container-orgdfa99d1" class="outline-4">
<h4 id="orgdfa99d1">Important :</h4>
<div class="outline-text-4" id="text-orgdfa99d1">
<div id="outline-container-org024e482" class="outline-4">
<h4 id="org024e482">Important :</h4>
<div class="outline-text-4" id="text-org024e482">
<p>
The relation between the Effective Address and the Segment &amp; Offset is as follow :
</p>
@ -52,10 +52,10 @@ The relation between the Effective Address and the Segment &amp; Offset is as fo
</p>
</div>
<ul class="org-ul">
<li><a id="org8845f88"></a>Example :<br />
<div class="outline-text-5" id="text-org8845f88">
<li><a id="org6cfa3c7"></a>Example :<br />
<div class="outline-text-5" id="text-org6cfa3c7">
<p>
Let the Physical address (Or Effective Address, these two terms are enterchangeable) <b>12345h</b> (the h refers to Hexadecimal, which can also be written like this <b>0x12345</b>), the register <b>DS = 1230h</b> and the register <b>SI = 0045h</b>, the CPU calculates the physical address by multiplying the content of the segment register <b>DS</b> by 10h (or 16) and adding the content of the register <b>SI</b>. so we get : <b>1230h x 10h + 45h = 12345h</b>
Let the Physical address (Or Effective Address, these two terms are interchangeable) <b>12345h</b> (the h refers to Hexadecimal, which can also be written like this <b>0x12345</b>), the register <b>DS = 1230h</b> and the register <b>SI = 0045h</b>, the CPU calculates the physical address by multiplying the content of the segment register <b>DS</b> by 10h (or 16) and adding the content of the register <b>SI</b>. so we get : <b>1230h x 10h + 45h = 12345h</b>
</p>
@ -66,16 +66,16 @@ Now if you are a clever one ( I know you are, since you are reading this &lt;3 )
</li>
</ul>
</div>
<div id="outline-container-org16ce372" class="outline-3">
<h3 id="org16ce372">Registers</h3>
<div class="outline-text-3" id="text-org16ce372">
<div id="outline-container-org6b34cdf" class="outline-3">
<h3 id="org6b34cdf">Registers</h3>
<div class="outline-text-3" id="text-org6b34cdf">
<p>
The 8086 CPU has 14 registers of 16bits of size. From the POV of the user, the 8086 has 3 groups of 4 registers of 16bits. One state register of 9bits and a counting program of 16bits inaccessible to the user (whatever this means).
</p>
</div>
<div id="outline-container-org8d1541d" class="outline-4">
<h4 id="org8d1541d">General Registers</h4>
<div class="outline-text-4" id="text-org8d1541d">
<div id="outline-container-org67926ce" class="outline-4">
<h4 id="org67926ce">General Registers</h4>
<div class="outline-text-4" id="text-org67926ce">
<p>
General registers contribute to arithmetic&rsquo;s and logic and addressing too.
</p>
@ -126,28 +126,28 @@ Now here are the Registers we can find in this section:
</div>
</div>
</div>
<div id="outline-container-orgd61521c" class="outline-3">
<h3 id="orgd61521c">Addressing and registers&#x2026;again</h3>
<div class="outline-text-3" id="text-orgd61521c">
<div id="outline-container-org824a260" class="outline-3">
<h3 id="org824a260">Addressing and registers&#x2026;again</h3>
<div class="outline-text-3" id="text-org824a260">
</div>
<div id="outline-container-orgc573bac" class="outline-4">
<h4 id="orgc573bac">I realized what I wrote here before was almost gibberish, sooo here we go again I guess ?</h4>
<div class="outline-text-4" id="text-orgc573bac">
<div id="outline-container-orgaa8f029" class="outline-4">
<h4 id="orgaa8f029">I realized what I wrote here before was almost gibberish, sooo here we go again I guess ?</h4>
<div class="outline-text-4" id="text-orgaa8f029">
<p>
Well lets take a step back to the notion of effective addresses VS relative ones.
</p>
</div>
</div>
<div id="outline-container-org83b616a" class="outline-4">
<h4 id="org83b616a">Effective = 10h x Segment + Offset . Part1</h4>
<div class="outline-text-4" id="text-org83b616a">
<div id="outline-container-org85a2533" class="outline-4">
<h4 id="org85a2533">Effective = 10h x Segment + Offset . Part1</h4>
<div class="outline-text-4" id="text-org85a2533">
<p>
When trying to access a specific memory space, we use this annotation <b>[Segment:Offset]</b>, so for example, and assuming <b>DS = 0100h</b>. We want to write the value <b>0x0005</b> to the memory space defined by the physical address <b>1234h</b>, what do we do ?
</p>
</div>
<ul class="org-ul">
<li><a id="orga0fd499"></a>Answer :<br />
<div class="outline-text-5" id="text-orga0fd499">
<li><a id="orgae0f70c"></a>Answer :<br />
<div class="outline-text-5" id="text-orgae0f70c">
<div class="org-src-container">
<pre class="src src-asm"><span style="color: #89b4fa;">MOV</span> [DS:0234h], 0x0005
</pre>
@ -159,7 +159,7 @@ Why ? Let&rsquo;s break it down :
<div id="orge5d4a33" class="figure">
<div id="orgd01a20f" class="figure">
<p><img src="../../src/gifs/lain-dance.gif" alt="lain-dance.gif" />
</p>
</div>
@ -177,9 +177,9 @@ Simple, right ?, now for another example
</li>
</ul>
</div>
<div id="outline-container-org93b46d0" class="outline-4">
<h4 id="org93b46d0">Another example :</h4>
<div class="outline-text-4" id="text-org93b46d0">
<div id="outline-container-org811633c" class="outline-4">
<h4 id="org811633c">Another example :</h4>
<div class="outline-text-4" id="text-org811633c">
<p>
What if we now have this instruction ?
</p>
@ -192,9 +192,9 @@ What does it do ? You might or might not be surprised that it does the exact sam
</p>
</div>
</div>
<div id="outline-container-orgcba10b0" class="outline-4">
<h4 id="orgcba10b0">Segment + Register &lt;3</h4>
<div class="outline-text-4" id="text-orgcba10b0">
<div id="outline-container-orge6219c5" class="outline-4">
<h4 id="orge6219c5">Segment + Register &lt;3</h4>
<div class="outline-text-4" id="text-orge6219c5">
<p>
Consider <b>DS = 0100h</b> and <b>BX = BP = 0234h</b> and this code snippet:
</p>
@ -230,8 +230,8 @@ The General rule of thumb is as follows :
</ul>
</div>
<ul class="org-ul">
<li><a id="orgf6c5f3b"></a>Note<br />
<div class="outline-text-5" id="text-orgf6c5f3b">
<li><a id="orga9c3a1b"></a>Note<br />
<div class="outline-text-5" id="text-orga9c3a1b">
<p>
The values of the registers CS DS and SS are automatically initialized by the OS when launching the program. So these segments are implicit. AKA : If we want to access a specific data in memory, we just need to specify its offset. Also you can&rsquo;t write directly into the DS or CS segment registers, so something like
</p>
@ -249,7 +249,7 @@ The values of the registers CS DS and SS are automatically initialized by the OS
</div>
<div id="postamble" class="status">
<p class="author">Author: Crystal</p>
<p class="date">Created: 2024-03-07 Thu 20:55</p>
<p class="date">Created: 2024-03-17 Sun 21:28</p>
</div>
</body>
</html>

View File

@ -3,7 +3,7 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2024-03-16 Sat 21:42 -->
<!-- 2024-03-17 Sun 21:46 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Reviving Caesar with a Cherry-flavored Crystal</title>
@ -20,17 +20,17 @@
<a accesskey="H" href="https://crystal.tilde.institute/"> HOME </a>
</div><div id="content" class="content">
<h1 class="title">Reviving Caesar with a Cherry-flavored Crystal</h1>
<div id="outline-container-org394c654" class="outline-2">
<h2 id="org394c654">What ?&#x2026;</h2>
<div class="outline-text-2" id="text-org394c654">
<div id="outline-container-org0bff230" class="outline-2">
<h2 id="org0bff230">What ?&#x2026;</h2>
<div class="outline-text-2" id="text-org0bff230">
<p>
That is probably your reaction reading this title, and no, this isn&rsquo;t a randomly generated sentence, but rather a simple encryption algorithm I recently made (Actually the first encryption algorithm i make at all!!). Meet <b>Cherry-Crystal Encryption</b>.
</p>
</div>
</div>
<div id="outline-container-org5a46ff7" class="outline-2">
<h2 id="org5a46ff7">Okay so, what is this all about ?</h2>
<div class="outline-text-2" id="text-org5a46ff7">
<div id="outline-container-org4770304" class="outline-2">
<h2 id="org4770304">Okay so, what is this all about ?</h2>
<div class="outline-text-2" id="text-org4770304">
<p>
This encryption Algorithm that we will call <b>CCE</b> for short, takes inspiration from the Caesar cipher which needn&rsquo;t an introduction (you can find great explanations online). But what about mine you might ask ?
</p>
@ -44,15 +44,15 @@ This encryption Algorithm that we will call <b>CCE</b> for short, takes inspirat
</ul>
</div>
</div>
<div id="outline-container-org9e305ea" class="outline-2">
<h2 id="org9e305ea">The Code :</h2>
<div class="outline-text-2" id="text-org9e305ea">
<div id="outline-container-org4496ca5" class="outline-2">
<h2 id="org4496ca5">The Code :</h2>
<div class="outline-text-2" id="text-org4496ca5">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #f9e2af;">#include</span> <span style="color: #f38ba8;">&lt;</span><span style="color: #a6e3a1;">stdio.h</span><span style="color: #f38ba8;">&gt;</span>
<span style="color: #f9e2af;">#include</span> <span style="color: #f38ba8;">&lt;</span><span style="color: #a6e3a1;">stdlib.h</span><span style="color: #f38ba8;">&gt;</span>
<span style="color: #f9e2af;">#include</span> <span style="color: #f38ba8;">&lt;</span><span style="color: #a6e3a1;">string.h</span><span style="color: #f38ba8;">&gt;</span>
<span style="color: #f9e2af;">void</span> <span style="color: #89b4fa;">encrypt</span><span style="color: #f38ba8;">(</span><span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">cherry</span><span style="color: #fab387;">[]</span>, <span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">crystal</span><span style="color: #fab387;">[]</span>, <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">mask</span><span style="color: #fab387;">[]</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span>
<span style="color: #f9e2af;">void</span> <span style="color: #89b4fa;">sloth</span><span style="color: #f38ba8;">(</span><span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">cherry</span><span style="color: #fab387;">[]</span>, <span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">crystal</span><span style="color: #fab387;">[]</span>, <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">mask</span><span style="color: #fab387;">[]</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span>
<span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">i</span>;
<span style="color: #cba6f7;">for</span> <span style="color: #fab387;">(</span>i = <span style="color: #fab387;">0</span>; i &lt; strlen<span style="color: #f9e2af;">(</span>cherry<span style="color: #f9e2af;">)</span> - <span style="color: #fab387;">1</span>; i++<span style="color: #fab387;">)</span> <span style="color: #fab387;">{</span>
mask<span style="color: #f9e2af;">[</span>i<span style="color: #f9e2af;">]</span> = cherry<span style="color: #f9e2af;">[</span>i<span style="color: #f9e2af;">]</span> - crystal<span style="color: #f9e2af;">[</span>i<span style="color: #f9e2af;">]</span>;
@ -61,7 +61,7 @@ This encryption Algorithm that we will call <b>CCE</b> for short, takes inspirat
mask<span style="color: #f9e2af;">[</span>i<span style="color: #f9e2af;">]</span> = crystal<span style="color: #f9e2af;">[</span>i<span style="color: #f9e2af;">]</span>;
<span style="color: #fab387;">}</span>
<span style="color: #f38ba8;">}</span>
<span style="color: #f9e2af;">void</span> <span style="color: #89b4fa;">decrypt</span><span style="color: #f38ba8;">(</span><span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">cherry</span><span style="color: #fab387;">[]</span>, <span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">crystal</span><span style="color: #fab387;">[]</span>, <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">mask</span><span style="color: #fab387;">[]</span>, <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">length</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span>
<span style="color: #f9e2af;">void</span> <span style="color: #89b4fa;">moon</span><span style="color: #f38ba8;">(</span><span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">cherry</span><span style="color: #fab387;">[]</span>, <span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">crystal</span><span style="color: #fab387;">[]</span>, <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">mask</span><span style="color: #fab387;">[]</span>, <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">length</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span>
<span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">i</span>, <span style="color: #cdd6f4;">end</span> = <span style="color: #fab387;">1</span>;
<span style="color: #cba6f7;">for</span> <span style="color: #fab387;">(</span>i = <span style="color: #fab387;">0</span>; i &lt; length; i++<span style="color: #fab387;">)</span> <span style="color: #fab387;">{</span>
<span style="color: #cba6f7;">if</span> <span style="color: #f9e2af;">(</span>i == strlen<span style="color: #a6e3a1;">(</span>cherry<span style="color: #a6e3a1;">)</span> - <span style="color: #fab387;">1</span> || end == <span style="color: #fab387;">0</span><span style="color: #f9e2af;">)</span> <span style="color: #f9e2af;">{</span>
@ -81,7 +81,7 @@ This encryption Algorithm that we will call <b>CCE</b> for short, takes inspirat
fgets<span style="color: #fab387;">(</span>cherry, size, stdin<span style="color: #fab387;">)</span>;
puts<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"Enter the Crystal: "</span><span style="color: #fab387;">)</span>;
fgets<span style="color: #fab387;">(</span>crystal, size, stdin<span style="color: #fab387;">)</span>;
encrypt<span style="color: #fab387;">(</span>cherry, crystal, mask<span style="color: #fab387;">)</span>;
sloth<span style="color: #fab387;">(</span>cherry, crystal, mask<span style="color: #fab387;">)</span>;
<span style="color: #cba6f7;">for</span> <span style="color: #fab387;">(</span>i = <span style="color: #fab387;">0</span>; i &lt; strlen<span style="color: #f9e2af;">(</span>crystal<span style="color: #f9e2af;">)</span> - <span style="color: #fab387;">1</span>; i++<span style="color: #fab387;">)</span> <span style="color: #fab387;">{</span>
printf<span style="color: #f9e2af;">(</span><span style="color: #a6e3a1;">"%d "</span>, mask<span style="color: #a6e3a1;">[</span>i<span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span>;
length++;
@ -97,7 +97,7 @@ This encryption Algorithm that we will call <b>CCE</b> for short, takes inspirat
scanf<span style="color: #f9e2af;">(</span><span style="color: #a6e3a1;">"%d"</span>, &amp;mask2<span style="color: #a6e3a1;">[</span>i<span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span>;
<span style="color: #fab387;">}</span>
puts<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"The Crystal is: "</span><span style="color: #fab387;">)</span>;
decrypt<span style="color: #fab387;">(</span>cherry2, crystal2, mask2, length<span style="color: #fab387;">)</span>;
moon<span style="color: #fab387;">(</span>cherry2, crystal2, mask2, length<span style="color: #fab387;">)</span>;
puts<span style="color: #fab387;">(</span>crystal2<span style="color: #fab387;">)</span>;
<span style="color: #cba6f7;">return</span> <span style="color: #fab387;">0</span>;
<span style="color: #f38ba8;">}</span>
@ -110,10 +110,62 @@ The program has been tested both on Alpine OS with Musl libc (thanks <a href="ht
</p>
</div>
</div>
<div id="outline-container-org6c3305a" class="outline-2">
<h2 id="org6c3305a">How does it work ?</h2>
<div class="outline-text-2" id="text-org6c3305a">
</div>
<div id="outline-container-orgf462a7c" class="outline-3">
<h3 id="orgf462a7c">Slothing (Encrypting) 🦥:</h3>
<div class="outline-text-3" id="text-orgf462a7c">
<p>
<del>What is it with these names I pick ?</del> Anyways, the <b>sloth(char *cherry, char *crystal, int *mask)</b> void function takes as parameters three variables:
</p>
<ul class="org-ul">
<li>A pointer to a <b>char array</b> or simply said <b>a string</b>, It&rsquo;s the <b>Cherry</b>.</li>
<li>Another pointer to the <b>Crystal</b>.</li>
<li>And Finally, a pointer to an array of integers <b>The Mask</b> which will be output-ed by the function.</li>
</ul>
<p>
The general idea of it is like this : (we will use a quick example)
</p>
<ul class="org-ul">
<li><b>Cherry</b>: H e l l o \0.</li>
<li><b>Crystal</b>: W o r l d \0.</li>
<li>Cherry[0] here is <b>H</b>, or in ASCII <b>72</b>. And Crystal[0] is <b>W</b> or <b>87</b>.</li>
<li>Mask[0] in this case is : Cherry[0] - Crystal[0]. which is <b>-15</b>. We then repeat the same steps for each letter on the <b>Crystal</b>.</li>
</ul>
<p>
Why the emphasis on <b>Crystal</b> ? Because we might end up with a case of a Crystal larger than a Cherry. we set the offset to the ASCII value of <b>Crystal[i]</b>, okay which to be fair is not the safest option out there, but I&rsquo;m planning on fixing it sooner or later. In the case of a large Cherry but a small Crystal&#x2026;it works but now looking at the code, i have no idea why it works the intended way&#x2026;.
</p>
</div>
</div>
<div id="outline-container-org7cb73ea" class="outline-3">
<h3 id="org7cb73ea">Mooning (Decrypting) 🌕:</h3>
<div class="outline-text-3" id="text-org7cb73ea">
<p>
The function <b>moon(char *cherry, char *crystal, int *mask, int length)</b> works the same way as the sloth function, but in reverse and a small change.
</p>
<ul class="org-ul">
<li><b>The for loop goes through all the elements of the Mask and reconstructing the Crystal using the reverse equation of the encryption</b>. But when it arrives at the end of the <b>Cherry</b> (here we enter the case of a Cherry smaller than a Crystal). Then we will just assume that <b>Mask[i]</b> is the ASCII code of <b>Crystal[i]</b>, and we continue this assumption until the end of the loop.</li>
</ul>
<p>
And voila that&rsquo;s it. Of course there might be some things I will change, but the overall concept is here!
</p>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="author">Author: Crystal &amp; Sloth</p>
<p class="date">Created: 2024-03-16 Sat 21:42</p>
<p class="date">Created: 2024-03-17 Sun 21:46</p>
</div>
</body>
</html>

View File

@ -29,7 +29,7 @@ The relation between the Effective Address and the Segment & Offset is as follow
**Effective address = 16 x segment + offset** keep in mind that this equation is encoded in decimal, which will change soon as we use Hexadecimal for convention reasons.
**** Example :
Let the Physical address (Or Effective Address, these two terms are enterchangeable) *12345h* (the h refers to Hexadecimal, which can also be written like this *0x12345*), the register *DS = 1230h* and the register *SI = 0045h*, the CPU calculates the physical address by multiplying the content of the segment register *DS* by 10h (or 16) and adding the content of the register *SI*. so we get : *1230h x 10h + 45h = 12345h*
Let the Physical address (Or Effective Address, these two terms are interchangeable) *12345h* (the h refers to Hexadecimal, which can also be written like this *0x12345*), the register *DS = 1230h* and the register *SI = 0045h*, the CPU calculates the physical address by multiplying the content of the segment register *DS* by 10h (or 16) and adding the content of the register *SI*. so we get : *1230h x 10h + 45h = 12345h*
Now if you are a clever one ( I know you are, since you are reading this <3 ) you may say that the physical address *12345h* can be written in more than one way....and you are right, more precisely : *2^{12} = 4096* different ways !!!

View File

@ -30,7 +30,7 @@ This encryption Algorithm that we will call *CCE* for short, takes inspiration f
#include <stdlib.h>
#include <string.h>
void encrypt(char cherry[], char crystal[], int mask[]) {
void sloth(char cherry[], char crystal[], int mask[]) {
int i;
for (i = 0; i < strlen(cherry) - 1; i++) {
mask[i] = cherry[i] - crystal[i];
@ -39,7 +39,7 @@ void encrypt(char cherry[], char crystal[], int mask[]) {
mask[i] = crystal[i];
}
}
void decrypt(char cherry[], char crystal[], int mask[], int length) {
void moon(char cherry[], char crystal[], int mask[], int length) {
int i, end = 1;
for (i = 0; i < length; i++) {
if (i == strlen(cherry) - 1 || end == 0) {
@ -59,7 +59,7 @@ int main(int argc, char *argv[]) {
fgets(cherry, size, stdin);
puts("Enter the Crystal: ");
fgets(crystal, size, stdin);
encrypt(cherry, crystal, mask);
sloth(cherry, crystal, mask);
for (i = 0; i < strlen(crystal) - 1; i++) {
printf("%d ", mask[i]);
length++;
@ -75,7 +75,7 @@ int main(int argc, char *argv[]) {
scanf("%d", &mask2[i]);
}
puts("The Crystal is: ");
decrypt(cherry2, crystal2, mask2, length);
moon(cherry2, crystal2, mask2, length);
puts(crystal2);
return 0;
}
@ -83,3 +83,31 @@ int main(int argc, char *argv[]) {
#+END_SRC
The program has been tested both on Alpine OS with Musl libc (thanks [[https://kaa.neocities.org/][Kin]]) and on OpenBSD 7.5-current. In the close future I will make a git repo as i'm planning to upgrade it and just make it better overall, who knows, maybe i will make a library out of it!!
* How does it work ?
** Slothing (Encrypting) 🦥:
+What is it with these names I pick ?+ Anyways, the *sloth(char *cherry, char *crystal, int *mask)* void function takes as parameters three variables:
- A pointer to a *char array* or simply said *a string*, It's the *Cherry*.
- Another pointer to the *Crystal*.
- And Finally, a pointer to an array of integers *The Mask* which will be output-ed by the function.
The general idea of it is like this : (we will use a quick example)
- *Cherry*: H e l l o \0.
- *Crystal*: W o r l d \0.
- Cherry[0] here is *H*, or in ASCII *72*. And Crystal[0] is *W* or *87*.
- Mask[0] in this case is : Cherry[0] - Crystal[0]. which is *-15*. We then repeat the same steps for each letter on the *Crystal*.
Why the emphasis on *Crystal* ? Because we might end up with a case of a Crystal larger than a Cherry. we set the offset to the ASCII value of *Crystal[i]*, okay which to be fair is not the safest option out there, but I'm planning on fixing it sooner or later. In the case of a large Cherry but a small Crystal...it works but now looking at the code, i have no idea why it works the intended way....
** Mooning (Decrypting) 🌕:
The function *moon(char *cherry, char *crystal, int *mask, int length)* works the same way as the sloth function, but in reverse and a small change.
- *The for loop goes through all the elements of the Mask and reconstructing the Crystal using the reverse equation of the encryption*. But when it arrives at the end of the *Cherry* (here we enter the case of a Cherry smaller than a Crystal). Then we will just assume that *Mask[i]* is the ASCII code of *Crystal[i]*, and we continue this assumption until the end of the loop.
And voila that's it. Of course there might be some things I will change, but the overall concept is here!