diff --git a/README.md b/README.md index 85d382c..cd8cad5 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,7 @@ So far, the following projects have been integrated to this repo: |[Plagiarism_detector](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Plagiarism_detector)|[Akshita Singhal](https://github.com/akshitasinghal4444)| |[csv_to_json](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/csv_to_json)|[MoiZ](https://github.com/TechBoyy6)| |[Battery_notification](https://github.com/hastagABAwesome-Python-Scripts/Battery_notification/)|[Krishna Sharma](https://github.com/krishnasharma1386)| +|[Steg_Tool](https://github.com/hastagABAwesome-Python-Scripts/Steg_Tool/)|[Shankar JP](https://github.com/shankarjp)| ## How to use : diff --git a/Steg_Tool/README.md b/Steg_Tool/README.md new file mode 100644 index 0000000..2d52cb5 --- /dev/null +++ b/Steg_Tool/README.md @@ -0,0 +1,21 @@ +# Steganography Tool + +* Advanced Image Steganography Tool used to hide files inside Images. +* Uses LSB (Least Significant Bit) Algorithm to store file data inside the image +* Works with only .png format + +Commands +``` +python3 steg.py +``` +Input +``` +encode + +decode +``` +Example +``` +encode image.png image_new.png secret.txt +decode image_new.png secret_new.txt +``` \ No newline at end of file diff --git a/Steg_Tool/image.png b/Steg_Tool/image.png new file mode 100644 index 0000000..df25ec4 Binary files /dev/null and b/Steg_Tool/image.png differ diff --git a/Steg_Tool/secret.txt b/Steg_Tool/secret.txt new file mode 100644 index 0000000..536aca3 --- /dev/null +++ b/Steg_Tool/secret.txt @@ -0,0 +1 @@ +secret \ No newline at end of file diff --git a/Steg_Tool/steg.py b/Steg_Tool/steg.py new file mode 100644 index 0000000..f733f64 --- /dev/null +++ b/Steg_Tool/steg.py @@ -0,0 +1,174 @@ +#Image Stego using LSB +import cv2 + +def encode(input_image_name, output_image_name, file_name): + input_image = cv2.imread(input_image_name) + + height, width, nbchannels = input_image.shape + size = width*height + current_width = 0 + current_height = 0 + current_channel = 0 + + maskonevalues = [1, 2, 4, 8, 16, 32, 64, 128] + maskone = maskonevalues.pop(0) + maskzerovalues = [254, 253, 251, 247, 239, 223, 191, 127] + maskzero = maskzerovalues.pop(0) + + data = open(file_name, "rb").read() + length = len(data) + if(width*height*nbchannels < length + 64): + raise Exception("Not enough space to hold all steganographic data") + binary_value = bin(length)[2:] + if(len(binary_value) > 64): + raise Exception("Binary Value larger than expected") + else: + while(len(binary_value) < 64): + binary_value = "0" + binary_value + for c in binary_value: + value = list(input_image[current_height, current_width]) + if(int(c) == 1): + value[current_channel] = int(value[current_channel]) | maskone + else: + value[current_channel] = int(value[current_channel]) & maskzero + input_image[current_height, current_width] = tuple(value) + if(current_channel == nbchannels-1): + current_channel = 0 + if(current_width == width-1): + current_width = 0 + if(current_height == height-1): + current_height = 0 + if maskone == 128: + raise Exception("No more space available in image") + else: + maskone = maskonevalues.pop(0) + maskzero = maskzerovalues.pop(0) + else: + current_height += 1 + else: + current_width += 1 + else: + current_channel += 1 + for byte in data: + if(isinstance(byte, int)): + pass + else: + byte = ord(byte) + binv = bin(byte)[2:] + if(len(binv) > 8): + raise Exception("Binary Value larger than expected") + else: + while(len(binv) < 8): + binv = "0" + binv + for c in binv: + val = list(input_image[current_height, current_width]) + if(int(c) == 1): + val[current_channel] = int(val[current_channel]) | maskone + else: + val[current_channel] = int(val[current_channel]) & maskzero + input_image[current_height, current_width] = tuple(val) + if(current_channel == nbchannels-1): + current_channel = 0 + if(current_width == width-1): + current_width = 0 + if(current_height == height-1): + current_height = 0 + if maskone == 128: + raise Exception("No more space available in image") + else: + maskone = maskonevalues.pop(0) + maskzero = maskzerovalues.pop(0) + else: + current_height += 1 + else: + current_width += 1 + else: + current_channel += 1 + cv2.imwrite(output_image_name, input_image) + + +def decode(encoded_image_name, extracted_file_name): + encoded_image = cv2.imread(encoded_image_name) + + height, width, nbchannels = encoded_image.shape + size = width*height + current_width = 0 + current_height = 0 + current_channel = 0 + + maskonevalues = [1, 2, 4, 8, 16, 32, 64, 128] + maskone = maskonevalues.pop(0) + maskzerovalues = [254, 253, 251, 247, 239, 223, 191, 127] + maskzero = maskzerovalues.pop(0) + + bits = "" + for i in range(64): + value = encoded_image[current_height, current_width][current_channel] + value = int(value) & maskone + if(current_channel == nbchannels-1): + current_channel = 0 + if(current_width == width-1): + current_width = 0 + if(current_height == height-1): + current_height = 0 + if(maskone == 128): + raise Exception("No more space available in image") + else: + maskone = maskonevalues.pop(0) + maskzero = maskzerovalues.pop(0) + else: + current_height += 1 + else: + current_width += 1 + else: + current_channel += 1 + if(value > 0): + bits += "1" + else: + bits += "0" + length = int(bits, 2) + output = b"" + for i in range(length): + bits = "" + for i in range(8): + value = encoded_image[current_height, current_width][current_channel] + value = int(value) & maskone + if(current_channel == nbchannels-1): + current_channel = 0 + if(current_width == width-1): + current_width = 0 + if(current_height == height-1): + current_height = 0 + if(maskone == 128): + raise Exception("No more space available in image") + else: + maskone = maskonevalues.pop(0) + maskzero = maskzerovalues.pop(0) + else: + current_height += 1 + else: + current_width += 1 + else: + current_channel += 1 + if(value > 0): + bits += "1" + else: + bits += "0" + output += bytearray([int(bits, 2)]) + f = open(extracted_file_name, "wb") + f.write(output) + f.close() + +if __name__ == "__main__": + input_string = input() + #encode input_image_name output_image_name file_name + #decode encoded_image_name extracted_file_name + input_list = input_string.split() + if input_list[0] == "encode": + encode(input_list[1], input_list[2], input_list[3]) + print(f"{input_list[2]}") + elif input_list[0] == "decode": + decode(input_list[1], input_list[2]) + print(f"{input_list[2]}") + else: + print("Invalid Entry")