版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28697571/article/details/72353358
LSB循序替换隐写算法
原理
首先将需要隐写的文件用二进制流的方式读取并保存在字符串S当中,然后顺序提取BMP图片的像素点,每一个像素点包含K(R、G、B)三种颜色,每一种颜色能够嵌入1bit信息,每一个像素点就能够嵌入3bit的信息。
隐写部分:
Kr = Kr – mod(Kr,2) + int(S[i])
Kg = Kg – mod(Kg,2) + int(S[i+1])
Kb = Kb – mod(Kb,2) + int(S[i+2])
提取部分:
S = S + mod(Kr,2)
S = S + mod(Kg,2)
S = S + mod(Kb,2)
隐写部分代码
# -*- coding: UTF-8 -*-
from PIL import Image
def plus(str):
return str.zfill(8)
def get_key(strr):
tmp = strr
f = file(tmp,"rb")
str = ""
s = f.read()
for i in range(len(s)):
str = str+plus(bin(ord(s[i])).replace('0b',''))
f.closed
return str
def mod(x,y):
return x%y;
*str1为载体图片路径,str2为隐写文件,str3为加密图片保存的路径*
def func(str1,str2,str3):
im = Image.open(str1)
width = im.size[0]
height = im.size[1]
count = 0
key = get_key(str2)
keylen = len(key)
for h in range(0,height):
for w in range(0,width):
pixel = im.getpixel((w,h))
a=pixel[0]
b=pixel[1]
c=pixel[2]
if count == keylen:
break
a= a-mod(a,2)+int(key[count])
count+=1
if count == keylen:
im.putpixel((w,h),(a,b,c))
break
b =b-mod(b,2)+int(key[count])
count+=1
if count == keylen:
im.putpixel((w,h),(a,b,c))
break
c= c-mod(c,2)+int(key[count])
count+=1
if count == keylen:
im.putpixel((w,h),(a,b,c))
break
if count % 3 == 0:
im.putpixel((w,h),(a,b,c))
im.save(str3)
提取部分代码
```
# -*- coding:UTF-8 -*-
from PIL import Image
def mod(x,y):
return x%y;
def toasc(strr):
return int(strr, 2)
*le为文件的长度,str1为加密载体图片的路径,str2为提取文件的保存路径*
def func(le,str1,str2):
a=""
b=""
im = Image.open(str1)
lenth = le*8
width = im.size[0]
height = im.size[1]
for h in range(0, height):
for w in range(0, width):
pixel = im.getpixel((w, h))
if count%3==0:
count+=1
b=b+str((mod(int(pixel[0]),2)))
if count ==lenth:
break
if count%3==1:
count+=1
b=b+str((mod(int(pixel[1]),2)))
if count ==lenth:
break
if count%3==2:
count+=1
b=b+str((mod(int(pixel[2]),2)))
if count ==lenth:
break
if count == lenth:
break
with open(str2,"wb") as f:
for i in range(0,len(b),8):
stra = toasc(b[i:i+8])
f.write(chr(stra))
stra =""
f.closed
“`