In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use Python to achieve least significant bit steganography". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Python to achieve least significant bit steganography".
LSB image steganography
LSB steganography is an image steganography technique, which hides the information in the image by replacing the least significant bit of each pixel with the information bit to be hidden. For better understanding, think of a digital image as a 2D pixel array, and the value each pixel contains depends on its type and depth.
We will consider the most widely used modes-RGB (3 × 8-bit pixels, true color) and RGBA (4x8-bit pixels, true color with transparent mask), which range from 0 to 255 (8-bit values).
Represents an image as a RGB pixel of a 2D array
By using the ASCII table, you can convert the message to a decimal value, then to a binary value, and then iterate over the pixel values one by one, replacing each least significant bit with the message bit in the sequence after converting the pixel value to binary.
To decode an encoded image, simply reverse the process: collect and store the last bit of each pixel, divide them into groups of eight, and convert them back to ASCII characters to get hidden information.
PYTHON operation
Try to implement the above concepts step by step using the Python libraries PIL and NumPY.
Step 1: import all required python libraries
Import numpy as np from PIL import Image
Step 2: enable the encoder function
First, write code to convert the source image into an array of NumPy pixels and store the size of the image. Check whether the mode of the image is RGB or RGBA, and then set the value of n. You also need to calculate the total number of pixels.
Def Encode (src, message, dest): img = Image.open (src,'r') width, height = img.size array = np.array (list (img.getdata () if img.mode = = 'RGB': n = 3 m = 0 elif img.mode = =' RGBA': n = 4 m = 1 total_pixels = array.size//n
Second, add a delimiter ("$T3G0") to the end of the secret message so that the program knows when to stop when decoding, converts the updated message into binary form, and calculates the required pixels.
Message + = "$t3g0" b_message = '.join ([format (ord (I), "08b") for i in message]) req_pixels = len (b_message)
Next, check whether the total available pixels are sufficient for secret messages. If you continue to iterate over the pixels one by one, and modify their least significant bits to the bits of the secret message until the complete message including the delimiter has been hidden.
If req_pixels > total_pixels: print ("ERROR: Need larger filesize") else: index=0 for p in range (total_pixels): for q in range (m, n): if index
< req_pixels: array[p][q] =int(bin(array[p][q])[2:9] + b_message[index], 2) index += 1 最后,有了更新后的像素数组,可以使用它来创建并保存为目标输出图像。 array=array.reshape(height, width, n) enc_img = Image.fromarray(array.astype('uint8'), img.mode) enc_img.save(dest)print("Image Encoded Successfully") 这样,编码器功能就完成了,是这样的: def Encode(src, message, dest): img = Image.open(src, 'r') width, height = img.size array = np.array(list(img.getdata())) if img.mode == 'RGB': n = 3 m = 0 elif img.mode == 'RGBA': n = 4 m = 1 total_pixels = array.size//n message += "$t3g0" b_message = ''.join([format(ord(i),"08b") for i in message]) req_pixels = len(b_message) if req_pixels >Total_pixels: print ("ERROR: Need largerfile size") else: index=0 for p in range (total_pixels): for q in range (m, n): if index
< req_pixels: array[p][q] =int(bin(array[p][q])[2:9] + b_message[index], 2) index += 1 array=array.reshape(height,width, n) enc_img =Image.fromarray(array.astype('uint8'), img.mode) enc_img.save(dest) print("Image EncodedSuccessfully") 步骤3:启用解码器功能 首先,重复类似的步骤,将源图像的像素保存为一个数组,计算模式,并计算总像素。 def Decode(src): img = Image.open(src, 'r') array = np.array(list(img.getdata())) if img.mode == 'RGB': n = 3 m = 0 elif img.mode == 'RGBA': n = 4 m = 1 total_pixels = array.size//n 其次,需要从图像左上角开始的每个像素中提取最低有效位,并以8个为一组存储。接下来,将这些组转换成ASCII字符来查找隐藏的消息,直到完全读取之前插入的分隔符。 hidden_bits = "" for p in range(total_pixels): for q in range(m, n): hidden_bits +=(bin(array[p][q])[2:][-1]) hidden_bits = [hidden_bits[i:i+8] for i in range(0, len(hidden_bits), 8)] message = "" for i in range(len(hidden_bits)): if message[-5:] == "$t3g0": break else: message +=chr(int(hidden_bits[i], 2)) 最后,检查是否找到了分隔符。如果没有,意味着图像中没有隐藏的信息。 if "$t3g0" in message: print("Hidden Message:",message[:-5]) else: print("No Hidden MessageFound") 这样,我们的解码器功能就完成了,看起来应该是这样的: def Decode(src): img = Image.open(src, 'r') array = np.array(list(img.getdata())) if img.mode == 'RGB': n = 3 m = 0 elif img.mode == 'RGBA': n = 4 m = 1 total_pixels = array.size//n hidden_bits = "" for p in range(total_pixels): for q in range(m, n): hidden_bits +=(bin(array[p][q])[2:][-1]) hidden_bits = [hidden_bits[i:i+8] fori in range(0, len(hidden_bits), 8)] message = "" for i in range(len(hidden_bits)): if message[-5:] =="$t3g0": break else: message +=chr(int(hidden_bits[i], 2)) if "$t3g0" in message: print("Hidden Message:",message[:-5]) else: print("No Hidden MessageFound") 步骤4:制作主要功能 对于主要功能,我们需询问用户想要执行哪个功能——编码还是解码。 若是编码,则要求用户输入以下内容——带扩展名的源图像名称、秘密消息和带扩展名的目标图像名称。若是解码,则要求用户提供隐藏了消息的源图像。 def Stego(): print("--Welcome to$t3g0--") print("1: Encode") print("2: Decode") func = input() if func == '1': print("Enter Source ImagePath") src = input() print("Enter Message toHide") message = input() print("Enter Destination ImagePath") dest = input() print("Encoding...") Encode(src, message, dest) elif func == '2': print("Enter Source ImagePath") src = input() print("Decoding...") Decode(src) else: print("ERROR: Invalid optionchosen") 步骤5:把以上所有功能放在一起,我们的LSB图像隐写程序就准备好了。 请注意,在一项研究中,观察到传统的LSB在JPEG的情况下是无效的,因为数据会因为其有损性质而在压缩时被操纵。而对于PNG图像,简单的LSB是适用的,在压缩时不会有任何数据损失。因此,最好只在PNG图像上运行你的程序。 举例1. Coding information
two。 Decode information
Thank you for your reading, the above is the content of "how to use Python to achieve least significant bit steganography". After the study of this article, I believe you have a deeper understanding of how to use Python to achieve least significant bit steganography, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.