Subscribe Us

Steganography in Python

 

Steganography: -

Steganography is the process of concealing information inside seemingly unrelated media. It has been used to securely transfer information for millennia and has grown with technology to become a sophisticated way of concealing data in digital formats.

History

The use of invisible ink to create messages that could only be seen when the paper was held over heat or a specific chemical was applied was one of the early kinds of steganography. Steganography has been used to conceal information in digital photos, audio files, and even network traffic in the digital era.

Digital Steganography

One prominent approach of digital steganography is to hide text within an image file using lossless image compression techniques. The message can be encoded within the image without affecting its look by modifying the least important bits of the image data. By decoding the picture data using the necessary software, the message may be obtained.

 

Steganography in Python

Source code

import cv2

def hide_message(image_path, message):
    # Loading the image
    image = cv2.imread(image_path)

    # Changing the image to one-dimensional array of bytes
    image_bytes = image.flatten()

    # change the message to one-dimensional array of bytes
    message_bytes = bytearray(message, "utf-8")

    # Add the message length to the beginning of the message
    message_length=len(message_bytes)
    message_bytes=message_length.to_bytes(4,byteorder="big")+ message_bytes

    # Hide the message within the image bytes
    for i, image_byte in enumerate(image_bytes):
        if i<len(message_bytes):
            image_bytes[i]=(image_bytes[i] & 0xfc) (message_bytes[i] & 0x03)

            # Change the image bytes back to 2D array
            image = image_bytes.reshape(image.shape)

            # Save the image with the hidden message
            cv2.imwrite("hidden_image.png", image)

        def reveal_message(image_path):
            # Loading the image
            image=cv2.imread(image_path)

            # Change the image to a one-dimensional array of bytes
            image_bytes=image.flatten()

            # Extract the message length from the beginning of the message
            message_length=int.from_bytes(image_bytes[:4], byteorder="big")

            # Extract the message from the image bytes
            message_bytes=bytearray()
            for i in range(4,4 + message_length):
                message_bytes.append(image_byte[i] & 0x03)
                # Decode the emssage bytes to a string
                message = message_bytes.decode("utf-8")

                return message

 

Explanation: -

The Python code below implements steganography using the OpenCV library. Two methods are implemented in the code: hide message and reveal message.

The hide message function takes as argument an image file location and a message string and returns an image with the message concealed inside it. The message is hidden by replacing part of the image's pixels' least significant bits (LSBs) with message bits. The reveal message function accepts an image file location as argument and returns the hidden message within it. The message is disclosed by decoding the LSBs of the image's pixels back into the original message.

We begin the hide message method by loading the picture using the cv2.imread function. We then use the flatten technique to convert the picture to a one-dimensional array of bytes, converting the 2D array to a 1D array.

The message string is then converted to a one-dimensional array of bytes using the bytearray function and the utf-8 encoding. We then add the message's length to the beginning of the message bytes to determine how many bits to remove before disclosing the message. This length is kept as a 4-byte big-endian integer.

____________________________________________________________________________


Another approach is to conceal messages among the background noise of an audio recording. The message can be placed within the file without being noticed by the human ear by modifying the least important bits of the audio data. This method may be used to conceal crucial information within seemingly harmless audio recordings, such as music or sound effects.

Steganography could also be utilized to conceal data in network transmission. Malicious actors sometimes utilize this to mask their operations, such as exfiltrating data from a network or operating a botnet. Attackers can avoid detection and operate unnoticed for lengthy periods of time by concealing their traffic inside seemingly normal network traffic.

 

Steganography could be used to authenticate the legitimacy of a communication in addition to obscuring content. A digital signature, for example, can be buried within an image or audio file to indicate that the material has not been tampered with. This method is especially suitable for sensitive data, such as financial transactions or secret correspondence.

Despite its lengthy history and numerous applications, steganography is frequently misunderstood and occasionally mistaken with encryption. Cryptography is the process of encoding communications to prevent unwanted access, whereas steganography is the activity of concealing a message's very existence. While the two strategies are frequently employed in tandem, they serve distinct functions and should not be regarded interchangeable.

Steganography has also been increasingly popular in recent years as the internet has become more accessible to individuals all over the world. Because the use of encryption to safeguard sensitive information is tightly controlled in many countries, steganography is an appealing option. This is especially true for individuals and organizations that need to discuss sensitive material without drawing the notice of government spy agencies or other players who may intercept their communications.

Another advantage of steganography is that it allows information to be sent across public networks without being discovered. Messages, for example, might be disguised in photographs or audio files published to social networking sites or delivered through email. This allows users to communicate securely even in nations where the internet is severely regulated.

 

While steganography has numerous advantages, it also has certain drawbacks. Detecting secret signals is one of the most difficult difficulties. Although software techniques exist to detect steganographically encoded messages, they are not always successful. In many circumstances, only by carefully studying the data and looking for patterns associated with steganographic encoding can the messages be identified.

Another difficulty with steganography is that it relies on a common secret to encode and decode messages. This implies that in order to communicate securely, both the sender and the recipient must have access to the same program or application. The capacity to communicate securely is lost if the shared secret is found or lost.

 

Steganography may also be time-consuming, especially for bigger communications. This is due to the fact that the data must be adjusted in order to conceal the message, which might result in bigger file sizes and longer transmission times.

Despite these difficulties, steganography remains a common way of securely conveying information, particularly in situations where typical encryption methods are either impractical or undesirable. Its application is anticipated to increase further as technology improves and the necessity for secure communication grows.

 

Steganography has an interesting use in the realm of art. Steganography has been utilized by artists to hide messages or secret pictures inside their works, producing a hidden layer of significance that only those who know where to look may see. This has introduced a new level to art appreciation and allowed artists to express themselves in new and inventive ways.

Steganography has also been employed in the medical profession. It has been used, for example, to conceal medical pictures within other images such as x-rays, CT scans, and MRI scans. This enables medical providers to securely communicate medical pictures without fear of the images being intercepted or seen by unauthorized parties.

 

Lastly, steganography is a strong technique for hiding information within other media that has been used for ages to securely convey data. Steganography has gotten increasingly complex with the development of digital technology and is now utilized in a range of applications, including concealing information within digital photos, audio files, and network traffic, as well as confirming the validity of a message. While steganography is frequently misunderstood and occasionally confused with encryption, the two serve distinct objectives and are employed in different contexts.

Post a Comment

0 Comments