<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	 xmlns:media="http://search.yahoo.com/mrss/" >

<channel>
	<title>Steganography &#8211; AnonyViet &#8211; English Version</title>
	<atom:link href="https://en.anonyviet.com/tag/steganography/feed/" rel="self" type="application/rss+xml" />
	<link>https://en.anonyviet.com</link>
	<description>The most popular website for sharing information technology, computer networks, and security knowledge. Stay up to date with the hottest news and tips</description>
	<lastBuildDate>Wed, 11 Jan 2023 17:26:57 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.1</generator>

<image>
	<url>https://en.anonyviet.com/wp-content/uploads/2023/01/cropped-ico-logo-75x75-1.png</url>
	<title>Steganography &#8211; AnonyViet &#8211; English Version</title>
	<link>https://en.anonyviet.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Adding &#8220;secret messages&#8221; to images with Python: Steganography</title>
		<link>https://en.anonyviet.com/adding-secret-messages-to-images-with-python-steganography/</link>
					<comments>https://en.anonyviet.com/adding-secret-messages-to-images-with-python-steganography/#respond</comments>
		
		<dc:creator><![CDATA[AnonyViet]]></dc:creator>
		<pubDate>Wed, 11 Jan 2023 17:26:57 +0000</pubDate>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Adding]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[messages]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[secret]]></category>
		<category><![CDATA[Steganography]]></category>
		<guid isPermaLink="false">https://en.anonyviet.com/?p=1065</guid>

					<description><![CDATA[Before implementing Steganography in python, we need to understand exactly what Steganography is. Join the channel Telegram of the AnonyViet 👉 Link 👈 Steganography is a method of hiding information in plain data. The word steganography comes from the Greek words: steganos meaning &#8220;protected&#8221; or &#8220;secret&#8221; and graphein, meaning &#8220;to write&#8221;. “Digital” steganography can involve [&#8230;]]]></description>
										<content:encoded><![CDATA[<p></p>
<div id="ftwp-postcontent">
<p>Before implementing Steganography in python, we need to understand exactly what Steganography is.</p>
<div class="code-block code-block-16" style="margin: 8px 0; clear: both;">
<div align="center">
<table class=" aligncenter" style="background-color: #c0c0c0; border-collapse: collapse; width: 59.9985%;">
<tbody>
<tr>
<td style="width: 100%; text-align: center;"><span style="font-size: 12pt;"><strong>Join the channel <span style="color: #0000ff;">Telegram</span> of the <span style="color: #008080;">AnonyViet </span> 👉 <span style="text-decoration: underline;"><a target="_blank" href="https://en.anonyviet.com/next-link?url=https%3A%2F%2Ft.me%2Fanonyvietchat" class="local-link" rel="noopener">Link</a></span>  👈</strong></span></td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Steganography is a method of hiding information in plain data.  The word steganography comes from the Greek words: steganos meaning &#8220;protected&#8221; or &#8220;secret&#8221; and graphein, meaning &#8220;to write&#8221;.</p>
<p>“Digital” steganography can involve hiding information in images, audio files, and even videos.  The advantage of “digital” Steganography over traditional methods is that the message can be hidden in a file that is already visible to the eye.  For example, someone could hide the text of a message in an image posted on a website.  The recipient can only see the message if it knows how to decode it.</p>
<p>To increase the level of security, the sender can encrypt the message before using some Steganography techniques.</p>
<p>Steganography can be used for a variety of purposes, including sending confidential messages, sharing sensitive information, and copyrighting digital data.</p>
<h2 id="ftoc-tong-quan-ve-ky-thuat-steganography" class="ftwp-heading"><strong>Overview of Steganography technique</strong></h2>
<p>Looking at the hexadecimal (hex) of the image, we can see that the files <strong>png</strong> and <strong>jpg</strong> always end with the same sequence of bytes.</p>
<p>If using Linux, so we will check with the command <code>xxd</code> or or use software <a target="_blank" href="https://en.anonyviet.com/next-link/?url=https%3A%2F%2Fmh-nexus.de%2Fdownloads%2FHxDSetup.zip" rel="noopener external nofollow" class="ext-link" onclick="this.target='_blank';"><strong>HxD . hex editing</strong></a>  on Windows.</p>
<p>When using the Linux command to see the hex system of the file <strong>jpg</strong>: <code>xxd -i image.jpg</code></p>
<p>The result will look like this:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">..... &#13;
0xef, 0x47, 0xf7, 0x94, 0x70, 0x51, 0xec, 0xa2, 0x9f, 0x4a, 0x7d, 0xe6, 0xa8, 0x73, 0xff, 0x00, 0xc7, 0x3f, 0xff, 0xd9</pre>
<p>When using HxD to open images on Windows.</p>
<p><img fetchpriority="high" decoding="async" class="aligncenter size-full wp-image-44098" src="https://anonyviet.com/wp-content/uploads/2022/11/giau-tin-nhan-vao-Steganography.jpg" alt="Sending messages to Steganography" width="597" height="323" srcset="https://anonyviet.com/wp-content/uploads/2022/11/giau-tin-nhan-vao-Steganography.jpg 597w, https://anonyviet.com/wp-content/uploads/2022/11/giau-tin-nhan-vao-Steganography-300x162.jpg 300w" sizes="(max-width: 597px) 100vw, 597px" title="More &quot;secret message&quot; into images in Python: Steganography 7"/></p>
<p>Either way you can see it <strong>FF</strong> and <strong>D9</strong> at the end, for the image, the following content <strong>FF D9</strong> will not be read.  Therefore, it is easy to add hidden messages after these locations.</p>
<p>The same process can be done with <strong>png</strong> and we will see results similar to this</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">... &#13;
0xfc, 0xab, 0x38, 0x9d, 0x9d, 0xa2, 0xee, 0x3d, 0xc0, 0x77, 0xfe, 0xd1, 0xef, 0xbf, 0xe8, 0x1f, 0x77, 0xc3, 0x86, 0x0d, 0x1b, 0x36, 0x6c, 0xb8, 0x3c, 0xfc, 0x6f, 0x44, 0x3a, 0x0e, 0x7e, 0xe2, 0x52, 0x24, 0x49, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82</pre>
<p>For files <strong>png</strong>the terminating string consisting of the last 12 bytes is: <code>0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82</code></p>
<h2 id="ftoc-trien-khai-steganography-trong-python" class="ftwp-heading">Implement Steganography in Python</h2>
<p>To script for steganography in python, we only need Python 3 and no extra libraries are needed.</p>
<p>The next step is to create a dictionary containing the terminating strings to distinguish between .png and .jpg files.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">file_end = {&#13;
    ".png": b'\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82',&#13;
    ".jpg": b'\xff\xd9'&#13;
}</pre>
<h3 id="ftoc-phuong-thuc" class="ftwp-heading"><strong>Method:</strong></h3>
<p>The code below opens a file in “append binary” mode and writes secret content to it.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">def append_secret(filename, file_extension, secret):&#13;
    with open(f"{filename}{file_extension}", "ab") as f:&#13;
        f.write(bytes(secret, encoding="utf-8"))</pre>
<p>This function will check if the file extension (png, jpg) of the image is in the dictionary “file_end” or not.</p>
<p>If true, this function will open the file and read it as a byte array.</p>
<p>It will then find the location of the file ending in the byte array.</p>
<p>After it finds the position, it decodes the byte array from that location on (after that location the secret message) and returns it as a string.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">def retrieve_secret(filename, file_extension):&#13;
    if not file_extension in file_end:&#13;
        print("Format not supported!")&#13;
    else:&#13;
        with open(f"{filename}{file_extension}", 'rb') as f:&#13;
            buff = bytes(f.read())&#13;
            index = buff.index(file_end[file_extension])&#13;
&#13;
            return buff[index+len(file_end[file_extension]):].decode('utf-8')</pre>
<p>This function will read the content of the image file, find the end position of the image file, delete all content at the end position of the image file (delete the secret code)</p>
<p>Now we just need to use them for the main thing.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">def clear_secret(filename, file_extension):&#13;
&#13;
    if not file_extension in file_end:&#13;
        print("Format not supported!")&#13;
    else:&#13;
        buff=""&#13;
        with open(f"{filename}{file_extension}", 'rb+') as f:&#13;
            buff = bytes(f.read())&#13;
            index = buff.index(file_end[file_extension])&#13;
&#13;
            f.truncate(index+len(file_end[file_extension]))</pre>
<h3 id="ftoc-ham-main" class="ftwp-heading">Main function</h3>
<p>Basically, we have the following 3 features:</p>
<ul>
<li>Append: will add a secret message to the image</li>
<li>Retrieve: will show secret message</li>
<li>Clear: will delete secret messages</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">if __name__=="__main__":&#13;
    request = sys.argv[1]&#13;
    filename, file_extension = os.path.splitext(sys.argv[2])&#13;
&#13;
    if request == "r":&#13;
        secret = retrieve_secret(filename, file_extension)&#13;
        print(secret)&#13;
    &#13;
    elif request == "a":&#13;
        append_secret(filename, file_extension, sys.argv[3])&#13;
    &#13;
    elif request == "c":&#13;
        clear_secret(filename, file_extension)&#13;
    &#13;
    else:&#13;
        print("[!] Wrong request, please use 'r' or 'a'")</pre>
<p>Putting the functions together gives us a complete Python image-message program:</p>
<p>Create files <code>main.py</code> with content:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">import sys&#13;
import os&#13;
&#13;
file_end = {&#13;
    ".png": b'\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82',&#13;
    ".jpg": b'\xff\xd9'&#13;
}&#13;
&#13;
def retrieve_secret(filename, file_extension):&#13;
    if not file_extension in file_end:&#13;
        print("Format not supported!")&#13;
    else:&#13;
        with open(f"{filename}{file_extension}", 'rb') as f:&#13;
            buff = bytes(f.read())&#13;
            index = buff.index(file_end[file_extension])&#13;
&#13;
            return buff[index+len(file_end[file_extension]):].decode('utf-8')&#13;
&#13;
def append_secret(filename, file_extension, secret):&#13;
    with open(f"{filename}{file_extension}", "ab") as f:&#13;
        f.write(bytes(secret, encoding="utf-8"))&#13;
&#13;
def clear_secret(filename, file_extension):&#13;
&#13;
    if not file_extension in file_end:&#13;
        print("Format not supported!")&#13;
    else:&#13;
        buff=""&#13;
        with open(f"{filename}{file_extension}", 'rb+') as f:&#13;
            buff = bytes(f.read())&#13;
            index = buff.index(file_end[file_extension])&#13;
&#13;
            f.truncate(index+len(file_end[file_extension]))&#13;
&#13;
if __name__=="__main__":&#13;
    request = sys.argv[1]&#13;
    filename, file_extension = os.path.splitext(sys.argv[2])&#13;
&#13;
    if request == "r":&#13;
        secret = retrieve_secret(filename, file_extension)&#13;
        print(secret)&#13;
    &#13;
    elif request == "a":&#13;
        append_secret(filename, file_extension, sys.argv[3])&#13;
    &#13;
    elif request == "c":&#13;
        clear_secret(filename, file_extension)&#13;
    &#13;
    else:&#13;
        print("[!] Wrong request, please use 'r' or 'a'")</pre>
<h2 id="ftoc-cach-su-dung-python-them-doc-xoa-tin-nhan-trong-file-anh" class="ftwp-heading">How to use Python to add/read/delete messages in image files</h2>
<p>Prepare a photo and name it <code>anonyviet.jpg</code> or <code>anonyviet.png</code></p>
<p>Add a secret message to the image:</p>
<p>Linux:<code>python main.py a anonyviet.jpg "tin nhắn bí mật"</code></p>
<p>Read secret messages in image files:</p>
<p><code>python main.py r anonyviet.jpg</code></p>
<p>Delete added messages in photos:</p>
<p><code>python main.py c anonyviet.jpg</code></p>
<p>This is a Steganography technique that helps to hide the text of the message into the image in Python, you can invent the encryption of the code so that other people can&#8217;t decode it when reading the content unless you have a Private Key.  This is an advanced exercise, you can try to apply it yourself.</p>
<h2 id="ftoc-giai-thuong" class="ftwp-heading">Award:</h2>
<p>05 you found “Secret Content” and in the picture below and <span style="text-decoration: underline;">write the solution</span> Then comment below this article and you will be given a 10K phone card.</p>
</p>
<p><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-44100" src="https://anonyviet.com/wp-content/uploads/2022/11/anonyviet.jpg" alt="anonyviet" width="320" height="320" srcset="https://anonyviet.com/wp-content/uploads/2022/11/anonyviet.jpg 320w, https://anonyviet.com/wp-content/uploads/2022/11/anonyviet-300x300.jpg 300w, https://anonyviet.com/wp-content/uploads/2022/11/anonyviet-150x150.jpg 150w, https://anonyviet.com/wp-content/uploads/2022/11/anonyviet-75x75.jpg 75w" sizes="auto, (max-width: 320px) 100vw, 320px" title="More &quot;secret message&quot; into images in Python: Steganography 8"/></p>
<div class="kk-star-ratings kksr-auto kksr-align-right kksr-valign-bottom" data-payload="{&quot;align&quot;:&quot;right&quot;,&quot;id&quot;:&quot;44090&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;60&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;4.9&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;\u0110\u00e1nh gi\u00e1 b\u00e0i vi\u1ebft post&quot;,&quot;legend&quot;:&quot;B\u00e0i vi\u1ebft \u0111\u1ea1t: 4.9\/5 - (60 b\u00ecnh ch\u1ecdn)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;139.6&quot;,&quot;_legend&quot;:&quot;B\u00e0i vi\u1ebft \u0111\u1ea1t: {score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}">
<p>            The article achieved: 4.9/5 &#8211; (60 votes)    </p>
</p></div>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://en.anonyviet.com/adding-secret-messages-to-images-with-python-steganography/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<media:content url="https://anonyviet.com/wp-content/uploads/2022/11/giau-noi-dung-vao-file-anh-Steganography-1.jpg" medium="image"></media:content>
            	</item>
	</channel>
</rss>
