File size: 4,293 Bytes
319dbb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import streamlit as st
from PIL import Image
import io
def steganography_function():
    # Steganography functions
    def encode_image(input_image, secret_message):
        """Encodes a secret message into an image."""
        img = Image.open(input_image)
        encoded = img.copy()
        width, height = img.size
        pixels = encoded.load()

        # Convert the message into binary and add a delimiter
        binary_message = ''.join(format(ord(char), '08b') for char in secret_message) + '1111111111111110'
        data_index = 0

        for y in range(height):
            for x in range(width):
                if data_index < len(binary_message):
                    pixel = list(pixels[x, y])
                    # Modify the LSB of each RGB value
                    for i in range(3):  # Process R, G, B channels
                        if data_index < len(binary_message):
                            pixel[i] = (pixel[i] & ~1) | int(binary_message[data_index])
                            data_index += 1
                    pixels[x, y] = tuple(pixel)

        output = io.BytesIO()
        encoded.save(output, format='PNG')
        output.seek(0)
        return output

    def decode_image(encoded_image):
        """Decodes a secret message from an image."""
        img = Image.open(encoded_image)
        width, height = img.size
        pixels = img.load()

        binary_message = ""
        for y in range(height):
            for x in range(width):
                pixel = pixels[x, y]
                for i in range(3):  # Process R, G, B channels
                    binary_message += str(pixel[i] & 1)

        # Split binary string into 8-bit chunks
        message = ""
        for i in range(0, len(binary_message), 8):
            byte = binary_message[i:i+8]
            if byte == '11111110':  # Stop at the delimiter
                break
            message += chr(int(byte, 2))

        return message

    # Streamlit App
    st.title("Steganography Tool")
    st.subheader("Hide and retrieve secret messages in images")

    # Tabs for Encode and Decode
    tab1, tab2 = st.tabs(["๐Ÿ”’ Encode Message", "๐Ÿ”“ Decode Message"])

    with tab1:
        st.header("Encode a Secret Message")
        uploaded_image = st.file_uploader("Upload an image to encode", type=["png", "jpg", "jpeg"])
        if uploaded_image:
            st.image(uploaded_image, caption="Selected Image for Encoding", use_column_width=True)
        secret_message = st.text_area("Enter the secret message to hide")
        encode_button = st.button("Apply Encoding")

        if encode_button:
            if uploaded_image and secret_message:
                try:
                    encoded_image = encode_image(uploaded_image, secret_message)
                    st.success("Message encoded successfully!")
                    st.image(encoded_image, caption="Encoded Image", use_column_width=True)
                    st.download_button(
                        label="Download Encoded Image",
                        data=encoded_image,
                        file_name="encoded_image.png",
                        mime="image/png"
                    )
                except Exception as e:
                    st.error(f"An error occurred: {e}")
            else:
                st.error("Please upload an image and enter a secret message.")

    with tab2:
        st.header("Decode a Secret Message")
        encoded_image = st.file_uploader("Upload an image to decode", type=["png", "jpg", "jpeg"], key="decode")
        if encoded_image:
            st.image(encoded_image, caption="Selected Image for Decoding", use_column_width=True)
        decode_button = st.button("Apply Decoding")

        if decode_button:
            if encoded_image:
                try:
                    decoded_message = decode_image(encoded_image)
                    st.success("Message decoded successfully!")
                    st.text_area("Decoded Message", decoded_message, height=100)
                except Exception as e:
                    st.error(f"An error occurred: {e}")
            else:
                st.error("Please upload an image to decode.")