隐秘术是在任何文件中隐蔽秘密数据的艺术。秘密数据可以是任何格式的数据,例如文本,乃至是文件。简而言之,隐写术的紧张目的是在任何文件(常日是图像,音频或视频)中隐蔽预期的信息,而无需实际变动文件的外不雅观,即,其外不雅观应与以前相同。
本期我们将一起学习基于图像的隐写术,即在图像中隐蔽秘密数据。
但是在深入研究之前,让我们先看看图像的组成。

1. 像素是图像的根本。
2. 每个像素包含三个值:(赤色,绿色,蓝色)也称为RGB值。
3. 每个RGB值的范围是0到255。
现在,让我们看看如何将数据编码和解码为图像形式。
01.编码办法
有很多算法可将数据编码到图像中,实际上,大家也可以自己制作。我们将利用的一个大略易于理解和履行的算法。详细步骤如下:
1. 对付数据中的每个字符,均采取其ASCII值并将其转换为8位二进制[1]。
2. 一次读取三个像素,统共具有3 3 = 9 RGB值。前八个RGB值用于存储一个字符,该字符将转换为8位二进制数。
3. 比较相应的RGB值和二进制数据。如果二进制数字为1,则RGB值将转换为奇数,否则将转换为偶数。
4. 第九个值确定是否应读取更多像素。如果还有更多数据要读取(即编码或解码),则第九个像素变为偶数。否则,如果我们想停滞进一步读取像素,则将其设为奇数。
重复此过程,直到所有数据都编码到图像中。
02.运用实例
假设要隐蔽的是‘Hii’。
该为三个字节,因此,对数据进行编码所需的像素为3 x 3 =9。请考虑一个4 x 3的图像,统共12个像素,足以对给定的数据进行编码。
[(27,64,164),(248,244,194),(174,246,250),(149,95,232),(188,156,169),(71,167,127),( 132、173、97),(113、69、206),(255、29、213),(53、153、220),(246、225、229),(142、82、175)]
第一步
的ASCII值为H72,其二进制等效值为01001000。
第二步
读取前三个像素。
(27,64,164),(248,244,194),(174,246,250)
第三步
现在,将像素值变动为1的奇数,乃至变动为0的奇数,如数据的二进制等价形式。
例如,第一个二进制数为0,第一个RGB值为27,则须要将其转换为偶数26。
类似地,由于下一个二进制数是,因此64被转换63为,1因此RGB值应设为奇数。
因此,修正后的像素为:
(26,63,164),(248,243,194),(174,246,250)
第四步
由于我们必须编码更多的数据,因此末了一个值该当是偶数。同样,i可以在此图像中进行编码。
通过+1或-1使像素值奇/偶时,应把稳二进制条件。即,像素值应大于或即是0且小于或即是255。
新图像将如下所示:
[(26,63,164),(248,243,194),(174,246,250),(148,95,231),(188,155,168),(70,167,126),( 132、173、97),(112、69、206),(254、29、213),(53、153、220),(246、225、229),(142、82、175)]
03.解码
对付解码,我们将考试测验找到如何逆转以前用于编码数据的算法。
1. 同样,一次读取三个像素。前8个RGB值向我们供应有关机密数据的信息,第9个值见告我们是否连续提高。
2. 对付前八个值,如果该值为奇数,则二进制位为1,否则为0。
3. 这些位被连接成一个字符串,每三个像素,我们得到一个字节的秘密数据,这意味着一个字符。
4. 现在,如果第九个值是偶数,那么我们将一次读取三个像素,否则我们将停滞。
让我们开始一次读取三个像素。考虑我们以前编码的图像。
[(26,63,164),(248,243,194),(174,246,250),(148,95,231),(188,155,168),(70,167,126),( 132、173、97),(112、69、206),(254、29、213),(53、153、220),(246、225、229),(142、82、175)]
第1步
我们首先阅读三个像素:
[(26,63,164),(248,243,194),(174,246,250)
第2步
读取第一个值:26,它是偶数,因此二进制位是0。同样,对63,二进制位1和164它0。该过程一贯持续到八个RGB值为止。
第三步
我们终极得到二进制值:01001000将所有单个二进制值连接在一起之后。终极的二进制数据对应于十进制值72,并以ASCII表示字符H。
第4步
由于第九个值是偶数,因此我们重复上述步骤。当碰着的第九个值是奇数时,我们停滞。
结果,我们得到的原始是Hii。
用于上述算法的Python程序如下:
# Python program implementing Image Steganography# PIL module is used to extract# pixels of image and modify itfrom PIL import Image# Convert encoding data into 8-bit binary# form using ASCII value of charactersdef genData(data): # list of binary codes # of given data newd = [] for i in data: newd.append(format(ord(i), '08b')) return newd# Pixels are modified according to the# 8-bit binary data and finally returneddef modPix(pix, data): datalist = genData(data) lendata = len(datalist) imdata = iter(pix) for i in range(lendata): # Extracting 3 pixels at a time pix = [value for value in imdata.__next__()[:3] + imdata.__next__()[:3] + imdata.__next__()[:3]] # Pixel value should be made # odd for 1 and even for 0 for j in range(0, 8): if (datalist[i][j] == '0' and pix[j]% 2 != 0): pix[j] -= 1 elif (datalist[i][j] == '1' and pix[j] % 2 == 0): if(pix[j] != 0): pix[j] -= 1 else: pix[j] += 1 # pix[j] -= 1 # Eighth pixel of every set tells # whether to stop ot read further. # 0 means keep reading; 1 means thec # message is over. if (i == lendata - 1): if (pix[-1] % 2 == 0): if(pix[-1] != 0): pix[-1] -= 1 else: pix[-1] += 1 else: if (pix[-1] % 2 != 0): pix[-1] -= 1 pix = tuple(pix) yield pix[0:3] yield pix[3:6] yield pix[6:9]def encode_enc(newimg, data): w = newimg.size[0] (x, y) = (0, 0) for pixel in modPix(newimg.getdata(), data): # Putting modified pixels in the new image newimg.putpixel((x, y), pixel) if (x == w - 1): x = 0 y += 1 else: x += 1# Encode data into imagedef encode(): img = input("Enter image name(with extension) : ") image = Image.open(img, 'r') data = input("Enter data to be encoded : ") if (len(data) == 0): raise ValueError('Data is empty') newimg = image.copy() encode_enc(newimg, data) new_img_name = input("Enter the name of new image(with extension) : ") newimg.save(new_img_name, str(new_img_name.split(".")[1].upper()))# Decode the data in the imagedef decode(): img = input("Enter image name(with extension) : ") image = Image.open(img, 'r') data = '' imgdata = iter(image.getdata()) while (True): pixels = [value for value in imgdata.__next__()[:3] + imgdata.__next__()[:3] + imgdata.__next__()[:3]] # string of binary data binstr = '' for i in pixels[:8]: if (i % 2 == 0): binstr += '0' else: binstr += '1' data += chr(int(binstr, 2)) if (pixels[-1] % 2 != 0): return data# Main Functiondef main(): a = int(input(":: Welcome to Steganography ::\n" "1. Encode\n2. Decode\n")) if (a == 1): encode() elif (a == 2): print("Decoded Word : " + decode()) else: raise Exception("Enter correct input")# Driver Codeif __name__ == '__main__' : # Calling main function main()
该程序中利用的模块是PIL ,代表Python Imaging Library。它使我们能够在Python中对图像实行操作。
04.程序示例
数据编码
数据解码
输入图像
medium.png
输出图像
newImage.png
05.局限性
对付JPEG 图像,此程序可能无法正常事情,由于JPEG利用有损压缩,这意味着修正了像素以压缩图像并降落质量,因此发生数据丢失。