<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rahul Kumar Patro on Medium]]></title>
        <description><![CDATA[Stories by Rahul Kumar Patro on Medium]]></description>
        <link>https://medium.com/@rahul1582?source=rss-a3b20b314f91------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*WEpODTYPR_AHFi_8ZmipcA.jpeg</url>
            <title>Stories by Rahul Kumar Patro on Medium</title>
            <link>https://medium.com/@rahul1582?source=rss-a3b20b314f91------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 11:52:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rahul1582/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Steganography]]></title>
            <link>https://rahul1582.medium.com/steganography-6c9af950cbb3?source=rss-a3b20b314f91------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c9af950cbb3</guid>
            <category><![CDATA[encoding]]></category>
            <category><![CDATA[decoding]]></category>
            <category><![CDATA[least-significant-bit]]></category>
            <category><![CDATA[opencv]]></category>
            <category><![CDATA[text-steganography]]></category>
            <dc:creator><![CDATA[Rahul Kumar Patro]]></dc:creator>
            <pubDate>Mon, 18 Jan 2021 12:13:37 GMT</pubDate>
            <atom:updated>2021-01-18T12:15:35.408Z</atom:updated>
            <content:encoded><![CDATA[<p>Learn how to hide Information within Images.</p><p>Steganography is a process of <strong>hiding</strong> a <strong>secret message</strong> within a larger one in such a way that someone can not know the presence or contents of the hidden message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*CuRJH_-TdcMdfudYT1QOHA.jpeg" /></figure><p>The purpose of Steganography is to maintain <strong>secret communication</strong> between two parties. Unlike cryptography, which conceals the contents of a secret message, steganography conceals the very fact that a message is communicated.</p><p>Although steganography differs from cryptography, there are many analogies between the two, and some authors classify steganography as a form of cryptography since hidden communication is a type of secret message.</p><p>The secret can be in the form of a text or in the form of an image(i.e Hiding texts inside an Image or hiding an Image inside an Image).</p><p>In this <strong>blog</strong>, we will be focussing on both <strong><em>text-based steganography</em></strong>.</p><p>Prerequisites you should know about:-</p><ol><li>An image is made up of pixels. The pixels are the building blocks of an image.</li><li>For a color image, 3 channels are there per pixel (i.e Red, Green, and Blue).</li><li>And every RGB value lies between 0–255. That is in binary an 8-bit number (as 2 to the power of 8 is 256).</li></ol><h3>Text Steganography</h3><blockquote>Hiding Text Inside an Image.</blockquote><p>As our goal is to hide the texts inside the image, one way we can do this is by converting the texts to binary and then store the converted binary bits using the Least Significant Bit Steganography.</p><h3>Least Significant Bit Steganography</h3><p>We can describe a <strong>digital image</strong> as a finite set of digital values, called pixels. Pixels are the smallest individual element of an image, holding values that represent the brightness of a given color at any specific point. So we can think of an image as a matrix (or a two-dimensional array) of pixels that contains a fixed number of rows and columns.</p><p>The Least Significant Bit (LSB) is a technique in which the last bits of each pixel are modified and replaced with the secret message’s data bit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*47mgIQeCcuxVW97xd_6r0w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/826/1*JdKyDLgQnyx2EUF32VDN1Q.png" /></figure><p>So from this, we can infer that change in bits is minimal when we change the least significant bits of an 8-bit binary number. Otherwise, if we change MSB it will have a larger impact on the final value and the original image’s content will be somehow lost. So we use the Least Significant Bit Steganography.</p><p>So let’s proceed to the code part: —</p><h3><strong>Step-1:</strong></h3><p>Function for the conversion of the message you want to encode to Binary.</p><pre><strong>def</strong> message2binary(message):<br>  <strong>if</strong> type(message) == str:<br>    result= &#39;&#39;.join([ format(ord(i), &quot;08b&quot;) <strong>for</strong> i <strong>in</strong> message ])<br>    <br>  <strong>elif</strong> type(message) == bytes <strong>or</strong> type(message) == np.ndarray:<br>    result= [ format(i, &quot;08b&quot;) <strong>for</strong> i <strong>in</strong> message ]<br>    <br>  <strong>elif</strong> type(message) == int <strong>or</strong> type(message) == np.uint8:<br>    result=format(message, &quot;08b&quot;)<br><br>  <strong>else</strong>:<br>    <strong>raise</strong> <strong>TypeError</strong>(&quot;Input type is not supported&quot;)<br>    <br>  <strong>return</strong> result</pre><blockquote>Here we are using 08b as we require the 8-bit representation of binary digits.</blockquote><blockquote>If we use only b then it will not add 0 to convert it into 8 bits and returns the binary converted value..</blockquote><h3>Step-2:</h3><p>Import the image inside which you want to hide your message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*R7hNHxcdr934y-DF0yBi9w.jpeg" /></figure><h3><strong>Step-3:</strong></h3><p>Following is the Encoder Function that helps to encode your message inside the image:</p><pre><strong>def</strong> encode_data(img):<br>    data=input(&quot;Enter the data to be Encoded:&quot;)    <br>    <strong>if</strong> (len(data) == 0): <br>      <strong>raise</strong> <strong>ValueError</strong>(&#39;Data is empty&#39;)<br>  <br>    filename = input(&quot;Enter the name of the New Image after Encoding(with extension): &quot;)<br>    <br>    no_bytes=(img.shape[0] * img.shape[1] * 3) // 8<br>    <br>    print(&quot;Maximum bytes to encode:&quot;, no_bytes)<br>    <br>    <strong>if</strong>(len(data)&gt;no_bytes):<br>        <strong>raise</strong> <strong>ValueError</strong>(&quot;Error encountered Insufficient bytes, Need Bigger Image or give Less Data !!&quot;)<br>    <br>    <em># Using the below as delimeter</em><br>    data +=&#39;*****&#39;    <br>    <br>    data_binary=message2binary(data)<br>    print(data_binary)<br>    data_len=len(data_binary)<br>    <br>    print(&quot;The Length of Binary data&quot;,data_len)<br>    <br>    data_index = 0<br>    <br>    <strong>for</strong> i <strong>in</strong> img:<br>        <strong>for</strong> pixel <strong>in</strong> i:<br>            <br>          r, g, b = message2binary(pixel)<br>         <em># print(r)</em><br>         <em># print(g)</em><br>         <em># print(b)</em><br>          <em># print(pixel)</em><br>          <strong>if</strong> data_index &lt; data_len:<br>              <strong><em># hiding the data into LSB(Least Significant Bit) of Red Pixel</em></strong><br><em>#               print(&quot;Original Binary&quot;,r)</em><br>              <em># print(&quot;The old pixel&quot;,pixel[0])</em><br>              pixel[0] = int(r[:-1] + data_binary[data_index], 2) <em>#changing to binary after overwrriting the LSB bit of Red Pixel</em><br><em>#               print(&quot;Changed binary&quot;,r[:-1] + data_binary[data_index])</em><br>              <br>              data_index += 1<br>              list1.append(pixel[0])<br><br>          <strong>if</strong> data_index &lt; data_len:<br>             <em># hiding the data into LSB of Green Pixel</em><br>              pixel[1] = int(g[:-1] + data_binary[data_index], 2) <em>#changing to binary after overwrriting the LSB bit of Green Pixel</em><br>              data_index += 1<br>              list1.append(pixel[1])<br><br>          <strong>if</strong> data_index &lt; data_len:<br>              <em># hiding the data into LSB of  Blue Pixel</em><br>              pixel[2] = int(b[:-1] + data_binary[data_index], 2) <em>#changing to binary after overwrriting the LSB bit of Blue pixel</em><br>              data_index += 1<br>              list1.append(pixel[2])<br><br>              <em># if data is encoded, just breaking out of the Loop</em><br>          <strong>if</strong> data_index &gt;= data_len:<br>              <strong>break</strong><br><br>         <br>   <br>    cv2.imwrite(filename,img)<br>    <br>    print(&quot;Encoded the data successfully and the image is successfully saved as &quot;,filename)</pre><p>First, we are checking whether the total bits that are to be encoded is not exceeding the total number of bits in the image. We will add a delimiter to identify the end of the message and then convert it to binary.</p><p>After that, we will iterate through pixels of the image and get r,g,b separate binary values. Once we are done with this we can replace the pixel values like this:</p><pre>pixel[0] = int(r[:-1] + data_binary[data_index], 2)</pre><p>We will do that for the total length of encoded binary bits.</p><p>After passing all the information through this function you will get an output like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aThDdceNhHp49ox1ApJN-Q.jpeg" /></figure><h3>Step-4:</h3><p>As we have encoded the data to the image, now its time for the retrieval of the message from the image, So the following is the Decoder Function:-</p><pre><strong>def</strong> decode_data(img):<br><br>  binary_data = &quot;&quot;<br>  <strong>for</strong> i <strong>in</strong> img:<br>      <strong>for</strong> pixel <strong>in</strong> i:<br>        <br>        <em>#   print(pixel)</em><br>          r, g, b = message2binary(pixel) <br>          binary_data += r[-1]  <em>#Extracting Encoded data from the LSB bit of Red Pixel as we have stored in LSB bit of every pixel.</em><br>          binary_data += g[-1]  <em>#Extracting Encoded data from the LSB bit of Green Pixel</em><br>          binary_data += b[-1]  <em>#Extracting Encoded data from LSB bit of Blue Pixel</em><br><br>  <em># splitting by 8-bits</em><br>  all_bytes = [ binary_data[i: i+8] <strong>for</strong> i <strong>in</strong> range(0, len(binary_data), 8) ]<br><br>  <em># Converting the bits to Characters</em><br>  decoded_data = &quot;&quot;<br>  <strong>for</strong> byte <strong>in</strong> all_bytes:<br>      decoded_data += chr(int(byte, 2))<br>      <strong>if</strong> decoded_data[-5:] == &quot;*****&quot;: <em>#Checking if we have reached the delimeter which is &quot;*****&quot;</em><br>          <strong>break</strong><br><br>  <br>  print(&quot;The Encoded data was :--&quot;,decoded_data[:-5])</pre><p>Now the reverse process will go on. As we had replaced the binary bits of the encoded message with the last bits of the 8-bit binary number we will decode the encoded message by looping over the pixel bits by adding the last bit i.e the Least Significant Bit.</p><pre>binary_data += r[-1]  <em>#Extracting Encoded data from the LSB bit of Red Pixel as we have stored in LSB bit of every pixel.</em></pre><p>We will continue to do that till we find the delimiter we will continue to go through the loop and convert the binary string to the character string.</p><p>After decoding the data you will get an output like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6lxdaFOo9A6N_DQSLCnNvQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*Cy-BaeW651PyhqXWbr1KVA.jpeg" /></figure><p>So on the left side, we can see the images before and after encoding the data.</p><blockquote><em>Can you identify any difference between the two images???</em></blockquote><p>Yahoo, we are done with Text Steganography.</p><p>Hope you liked it.</p><blockquote>If you are interested in the code, you can find my notebook on <a href="https://github.com/Rahul1582/Steganography">Github</a>.</blockquote><p>To visit the next part of this series i.e Image Steganography please visit this <a href="https://rahul1582.medium.com/steganography-part-2-ad7452a3ff1">link</a>.</p><h3>Hope <strong>you</strong> enjoyed the article!!</h3><h3>References:</h3><ul><li><a href="https://medium.com/hackernoon/simple-image-steganography-in-python-18c7b534854f">Simple Image Steganography in Python</a></li><li><a href="https://towardsdatascience.com/hiding-data-in-an-image-image-steganography-using-python-e491b68b1372">Hiding data in an image : Image Steganography using Python</a></li><li><a href="https://www.edureka.co/blog/steganography-tutorial">Steganography Tutorial | A Complete Guide For Beginners | Edureka</a></li></ul><p><a href="https://www.thepythoncode.com/article/hide-secret-data-in-images-using-steganography-python">https://www.thepythoncode.com/article/hide-secret-data-in-images-using-steganography-python</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c9af950cbb3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Steganography Part-2]]></title>
            <link>https://rahul1582.medium.com/steganography-part-2-ad7452a3ff1?source=rss-a3b20b314f91------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad7452a3ff1</guid>
            <category><![CDATA[decoding]]></category>
            <category><![CDATA[least-significant-bit]]></category>
            <category><![CDATA[encoding]]></category>
            <category><![CDATA[image-steganography]]></category>
            <category><![CDATA[opencv]]></category>
            <dc:creator><![CDATA[Rahul Kumar Patro]]></dc:creator>
            <pubDate>Mon, 18 Jan 2021 12:11:24 GMT</pubDate>
            <atom:updated>2021-01-18T12:17:36.190Z</atom:updated>
            <content:encoded><![CDATA[<p>Learn how to hide Images Inside Images</p><p>We saw how to hide information inside Images, now it&#39;s time to see another form of Steganography.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/1*tEkbeSQtPinZMtZlpCYWkg.jpeg" /></figure><h3><strong>Image Steganography</strong></h3><blockquote>Hiding an Image Inside an Image.</blockquote><p>As our goal is to hide an image inside another image, one way we can do this is by converting the images to binary and then store the converted binary bits using the Least Significant Bit Steganography.</p><p>For more details about the Least Significant Bit Steganography, you can refer to my previous post.</p><p>Now let’s proceed to the code part: —</p><h3><strong>Step-1:</strong></h3><p>Loading the two images and ensuring the image to which another image will be merged is bigger in size than the other. If not then resize it accordingly.</p><p>The Image which is to be merged:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*rAaXrb8U6BQg1cGw6Clz3Q.png" /></figure><p>The image on which the above image will be merged:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*wCI-Nvdl1gHcvGsy0rSBzg.png" /></figure><h3><strong>Step-2:</strong></h3><p>Below is the code for the integer to binary conversion where the input will be the pixel value that itself is a tuple of r,g,b.</p><pre><strong>def</strong> int2bin(rgb):<br>    <br>    <em>#Will convert RGB pixel values from integer to binary</em><br>    <em>#INPUT: An integer tuple (e.g. (220, 110, 96))</em><br>    <em>#OUTPUT: A string tuple (e.g. (&quot;00101010&quot;, &quot;11101011&quot;, &quot;00010110&quot;))</em><br>    <br>    r, g, b = rgb<br>    <strong>return</strong> (&#39;<strong>{0:08b}</strong>&#39;.format(r),<br>            &#39;<strong>{0:08b}</strong>&#39;.format(g),<br>            &#39;<strong>{0:08b}</strong>&#39;.format(b))<br>    <em>#Return converted r,g,b binary values separately..</em></pre><p>And for binary to integer conversion where the input will be a binary number and results to an integer.</p><pre><strong>def</strong> bin2int(rgb):<br>    <br>    <em>#Will convert RGB pixel values from binary to integer.</em><br>    <em>#Reverse of the first part.</em><br>  <br>    r, g, b = rgb<br>    <strong>return</strong> (int(r, 2),<br>            int(g, 2),<br>            int(b, 2))<br>    <em>#return converted r,g,b integer values separately</em></pre><h3><strong>Step-3:</strong></h3><p>For merging the two images we can replace the last two bits of each r,g,b value with that of the new image. You can choose this yourself ensuring you are not losing any content from the original image.</p><pre><strong>def</strong> merge2rgb2(rgb1, rgb2):<br>    <br>    r1, g1, b1 = rgb1<br>    r2, g2, b2 = rgb2<br>    rgb = (r1[:6] + r2[:2],<br>           g1[:6] + g2[:2],<br>           b1[:6] + b2[:2])<br>    <strong>return</strong> rgb</pre><h3><strong>Step-4:</strong></h3><p>Merge Function for merging the two Images:</p><pre><strong>def</strong> merge2img2(img1, img2):<br><br>    image1=img1<br>    image2=img2<br>    <em>#print(&#39;rahul&#39;)</em><br>  <br>    <em># Condition for merging</em><br>    <strong>if</strong>(image1.size[0]&gt;image2.size[0] <strong>or</strong> image1.size[1]&gt;image2.size[1]):<br>       print(&quot;Cannot merge as the size of 1st Image is greater than size of 2nd Image&quot;)<br>       <strong>return</strong><br>  <br>  <em># Getting the pixel map of the two images</em><br>    pixel_tuple1 = image1.load()<br>    pixel_tuple2 = image2.load()<br><br>    <em>#print(pixel_tuple1)</em><br>    <em>#print(pixel_tuple2)</em><br><br>    <em># The new image that will be created.</em><br>    new_image = Image.new(image2.mode, image2.size) <em># Setting the size of Image 2 as Image 1 will be merged to Image 2.</em><br>    pixels_new = new_image.load()<br><br>    <strong>for</strong> row <strong>in</strong> range(image2.size[0]):<br>      <strong>for</strong> col <strong>in</strong> range(image2.size[1]):<br><br>        rgb1 = int2bin(pixel_tuple2[row, col])<br><br>        <em># Using a black pixel as default</em><br>        rgb2 = int2bin((0, 0, 0))<br><br>        <em># Converting the pixels of image 1 if condition is satisfied</em><br><br>        <strong>if</strong>(row &lt;image1.size[0] <strong>and</strong> col&lt; image1.size[1]):<br>          rgb2= int2bin(pixel_tuple1[row,col])<br><br><br>        merge_rgb= merge2rgb2(rgb1,rgb2)<br><br>        pixels_new[row,col] = bin2int(merge_rgb) <br><br>    <em>#print(&#39;rahul&#39;)</em><br>    new_image.convert(&#39;RGB&#39;).save(&#39;merged2.jpg&#39;)<br>    <br>    <strong>return</strong> new_image</pre><p>First checking with the size compatibility then iterating over the image on which another image will be merged and converting the integer pixel values to binary and replacing it will the binary converted values of the image to be merged. Finally converting the changed binary number to an integer.</p><p>After merging the image the merged image was looking like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*AhLn3kXVQLI9LZ_y0g5eOw.png" /></figure><p><strong><em>Can you see any difference between the original and the merged image??</em></strong></p><h3><strong>Step-5:</strong></h3><p>Function for unmerging the image:</p><pre><strong>def</strong> unmerge2(img):<br>    <br>    pixel_map = img.load()<br><br>    new_image = Image.new(img.mode, img.size)<br>    pixels_new = new_image.load()<br><br>    <br>    original_size = img.size<br><br>    <strong>for</strong> row <strong>in</strong> range(img.size[0]):<br>        <strong>for</strong> col <strong>in</strong> range(img.size[1]):<br>            r, g, b = int2bin(pixel_map[row, col])<br><br>            <em># Extracting the last 6 bits (corresponding to the hidden image) and adding zeroes to increase the brightness.</em><br>            <br>            rgb = (r[6:] + &quot;000000&quot;,<br>                   g[6:] + &quot;000000&quot;,<br>                   b[6:] + &quot;000000&quot;)<br><br>            <em># Convert it to an integer tuple</em><br>            pixels_new[row, col] = bin2int(rgb)<br><br>           <em>#If this is a &#39;valid&#39; position, store it as a last valid option</em><br>            <strong>if</strong> pixels_new[row, col] != (0, 0, 0):<br>                original_size = (row + 1, col + 1)<br><br>    <em># Crop the image based on the &#39;valid&#39; pixels</em><br>    new_image = new_image.crop((0, 0, original_size[0], original_size[1]))<br><br>    <strong>return</strong> new_image</pre><p>Here the reverse process will be followed i.e iterating over the merged image and restoring the image that was merged by taking the last 2 bits of every r,g,b value and then adding zeroes to it for good brightness.</p><p>We will continue to do that until we find an invalid position.</p><p>After unmerging the unmerged image was looking like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*0Wbi_wjWRXSaLh3h0VYuzQ.png" /></figure><blockquote><strong>Can you see any difference between the original and the unmerged image??</strong></blockquote><h3><strong><em>Results:</em></strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-YA6_FmbxWOGFjK4nd1O2g.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*knXiK-1-ZO8xKUi1VejciQ.jpeg" /></figure><p>We can see the final unmerged image is slightly less bright than the original but mostly the same as that of the previous original image.</p><p>Previously I tried merging 4 bits out of 8 bits but that was not successful as the image after unmerging the result was not quite good.</p><h3>So we have concluded that the 2 MSBs of Image 1 were more important than 4 MSBs of image 1, they were adding noise to the image. So merging 6 MSBs of Image2 and 2 MSBs of Image 1 was helpful.</h3><blockquote>If you are interested in the code, you can find my notebook on <a href="https://github.com/Rahul1582/Steganography">Github</a>.</blockquote><p>To visit the first part of this series i.e Text Steganography please visit this <a href="https://rahul1582.medium.com/steganography-6c9af950cbb3">link</a>.</p><h3>Hope <strong>you</strong> enjoyed the article!!</h3><h3><strong>References:</strong></h3><ul><li><a href="https://www.researchgate.net/publication/314116270_Image_Steganography">(PDF) Image Steganography</a></li><li><a href="https://www.mygreatlearning.com/blog/image-steganography-explained/">Image Steganography Explained | What is Image Steganography?</a></li><li><a href="https://towardsdatascience.com/hiding-data-in-an-image-image-steganography-using-python-e491b68b1372">Hiding data in an image : Image Steganography using Python</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad7452a3ff1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>