Skip to content

Commit d0111e3

Browse files
committed
added argument parsing for steganography tutorial
1 parent 2d01f82 commit d0111e3

File tree

3 files changed

+58
-17
lines changed

3 files changed

+58
-17
lines changed

ethical-hacking/steganography/README.md

+30-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
11
# [How to use Steganography to Hide Secret Data in Images in Python](https://www.thepythoncode.com/article/hide-secret-data-in-images-using-steganography-python)
22
To run this:
33
- `pip3 install -r requimements.txt`
4-
- To encode some data to the imag `image.PNG` and decode it right away:
4+
-
55
```
6-
python steganography image.PNG "This is some secret data."
6+
python steganography.py --help
77
```
8-
This will write another image with data encoded in it and **outputs:**
8+
**Output**:
9+
```
10+
usage: steganography.py [-h] [-t TEXT] [-e ENCODE] [-d DECODE]
11+
12+
Steganography encoder/decoder, this Python scripts encode data within images.
13+
14+
optional arguments:
15+
-h, --help show this help message and exit
16+
-t TEXT, --text TEXT The text data to encode into the image, this only
17+
should be specified for encoding
18+
-e ENCODE, --encode ENCODE
19+
Encode the following image
20+
-d DECODE, --decode DECODE
21+
Decode the following image
22+
```
23+
- To encode some data to the image `image.PNG`:
24+
```
25+
python steganography.py -e image.PNG -t "This is some secret data."
26+
```
27+
This will write another image `imaged_encoded.PNG` with data encoded in it and **outputs:**
928
```
1029
[*] Maximum bytes to encode: 125028
1130
[*] Encoding data...
31+
[+] Saved encoded image.
32+
```
33+
- To decode the data containing the image `encoded_image.PNG`, use:
34+
```
35+
python steganography.py -d encoded_image.PNG
36+
```
37+
**Outputs:**
38+
```
1239
[+] Decoding...
1340
[+] Decoded data: This is some secret data.
1441
```
15-
- You can isolate encoding and decoding processes in two different Python files, which makes more sense.
-32 Bytes
Loading

ethical-hacking/steganography/steganography.py

+28-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import cv2
22
import numpy as np
3-
import sys
4-
3+
import os
54

65
def to_bin(data):
76
"""Convert `data` to binary format as string"""
@@ -79,14 +78,30 @@ def decode(image_name):
7978

8079

8180
if __name__ == "__main__":
82-
input_image = sys.argv[1]
83-
output_image = f"encoded_{input_image}"
84-
secret_data = sys.argv[2]
85-
86-
# encode the data into the image
87-
encoded_image = encode(image_name=input_image, secret_data=secret_data)
88-
# save the output image (encoded image)
89-
cv2.imwrite(output_image, encoded_image)
90-
# decode the secret data from the image
91-
decoded_data = decode(output_image)
92-
print("[+] Decoded data:", decoded_data)
81+
import argparse
82+
parser = argparse.ArgumentParser(description="Steganography encoder/decoder, this Python scripts encode data within images.")
83+
parser.add_argument("-t", "--text", help="The text data to encode into the image, this only should be specified for encoding")
84+
parser.add_argument("-e", "--encode", help="Encode the following image")
85+
parser.add_argument("-d", "--decode", help="Decode the following image")
86+
87+
args = parser.parse_args()
88+
secret_data = args.text
89+
if args.encode:
90+
# if the encode argument is specified
91+
input_image = args.encode
92+
print("input_image:", input_image)
93+
# split the absolute path and the file
94+
path, file = os.path.split(input_image)
95+
# split the filename and the image extension
96+
filename, ext = file.split(".")
97+
output_image = os.path.join(path, f"{filename}_encoded.{ext}")
98+
# encode the data into the image
99+
encoded_image = encode(image_name=input_image, secret_data=secret_data)
100+
# save the output image (encoded image)
101+
cv2.imwrite(output_image, encoded_image)
102+
print("[+] Saved encoded image.")
103+
if args.decode:
104+
input_image = args.decode
105+
# decode the secret data from the image
106+
decoded_data = decode(input_image)
107+
print("[+] Decoded data:", decoded_data)

0 commit comments

Comments
 (0)