• 主页
  • 相册
  • 随笔
  • 目录
  • 存档
Total 244
Search AboutMe

  • 主页
  • 相册
  • 随笔
  • 目录
  • 存档

实验:基于奇偶校验的LSB算法及卡方分析

2019-11-11

内容:

  • 奇偶校验的LSB水印隐写算法
  • 基于卡方分析的隐写分析

1. 奇偶校验的LSB水印隐写算法

_lsb.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/python
# -*- coding=utf-8 -*-
import cv2
import numpy as np
import math
size = 11


def water(watermark):
waterlen = len(watermark)
binwtrl = bin(waterlen)
#去掉前面的0b
binwtrl = binwtrl[2:]
#将不足8位的补0
while len(binwtrl) < 8:
binwtrl = "0" + binwtrl
for i in watermark:
cc = ord(i)
bincc = bin(cc)
bincc = bincc[2:]
while len(bincc) < 8:
bincc = "0" + bincc
binwtrl += bincc

return binwtrl


def lsbcnt(block):
cnt = 0
for i in range(size):
for j in range(size):
cnt += (block[i][j] % 2)
return str(cnt % 2)


def revrt(block):
for i in range(size):
for j in range(size):
tp = (block[i][j] % 2)
if(tp == 1): # 奇数
block[i][j] -= 1
else:
block[i][j] += 1
return block


def embedwater(img, wstr, rpath):
binwtrl = water(wstr)
im_array = img[:, :, 0]
w, h = im_array.shape
w_size = w//size
cnth = 0
cntl = 0
for i in binwtrl:
block = np.zeros((size, size), int)
block[:size, :size] = im_array[cnth * size:(cnth + 1) * size, cntl * size:(cntl + 1) *
size]
cnt = lsbcnt(block)
if(cnt != i): # 不等
block = revrt(block)
im_array[cnth * size:(cnth + 1) * size, cntl * size:(cntl + 1) *
size] = block
cntl += 1
if (cntl == w_size):
cnth += 1
cntl = 0

im = img.copy()
im[:, :, 0] = im_array
cv2.imwrite(rpath, im_array)
return im


def extrawater(rpath):
_cnt = 8
img = cv2.imread(rpath)
im_array = img[:, :, 0]
w, h = im_array.shape
w_size = w//size
cnth = 0
cntl = 0
lent = '0b'
water = "0b"
result = ""

while(_cnt > 0):
block = np.zeros((size, size), int)
block[:size, :size] = im_array[cnth * size:(cnth + 1) * size, cntl * size:(cntl + 1) *
size]
cnt = lsbcnt(block)
lent += cnt
_cnt -= 1
cntl += 1
if (cntl == w_size):
cnth += 1
cntl = 0
length = int(lent, 2)

while(length > 0):
block = np.zeros((size, size), int)
block[:size, :size] = im_array[cnth * size:(cnth + 1) * size, cntl * size:(cntl + 1) *
size]
cnt = lsbcnt(block)
water += cnt
if(len(water) == 10):
result += chr(int(water, 2))
water = "0b"
length -= 1
cntl += 1
if (cntl == w_size):
cnth += 1
cntl = 0
return result


def main():
path = "lena512.bmp"
wstr = ""
rpath = "result.bmp"
img = cv2.imread(path)
embedwater(img, wstr, rpath)
result = extrawater(rpath)
print(result)

简要说明

  • 奇偶校验基本原理
    • 将载体分成不重叠的区域,在每个区域中嵌入1比特信息
    • 奇偶校验位计算公式:$p(I)= \sum x_jmod2 $
    • If 奇偶校验位与秘密信息不同,则将区域 $I$中LSB取值反转
  • 水印嵌入
    • 计算水印长度,并置于二进制水印字串首部。在本程序中预留8位,即最大长度位256
    • 分块,由于奇偶校验的需要,需要划分为奇数幂的块
    • 考虑到像素点$pi=0$,故偶数加一,奇数减一

2. 基于卡方分析的隐写分析

_chisquare.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from scipy import stats
import cv2
import numpy as np


def grayHist(img):

grayDict = {}
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
for key in range(256):
grayDict[key] = 0
for i in range(img_gray.shape[0]):
for j in range(img_gray.shape[1]):
grayDict[img_gray[i][j]] += 1
'''
grayDict=cv2.calcHist([img],[0],None,[256],[0,256])
'''
return grayDict


def as_num(x):
y = '{:.10f}'.format(x) # .10f 保留10位小数
return y


if __name__ == "__main__":
rpath = "result.bmp"
rim = cv2.imread(rpath)
rim_grey = grayHist(rim)
tp = []
for i in range(256):
tp.append(rim_grey[i])
i = 0
exp = []
obs = []
while(i <= 127):
if(((tp[2*i]+tp[2*i+1])//2) == 0):
i += 1
continue
exp.append((tp[2*i]+tp[2*i+1])/2)
obs.append(tp[2*i])
i += 1
x = stats.chisquare(obs, f_exp=exp)
print(as_num(x[1]))

简要说明

  • 卡方分析

    • 比较理论频数和实际频数的吻合程度或拟合优度问题
    • 卡方分布:$r=\sum_i^{127}\frac{h_{2i}-h_{2i}^{}}{h_{2i}^{}}$
      • $h_{2i}^{*}=\frac{h_{2i}+h_{2i+1}}{2}$
    • 卡方分布概率密度函数(隐写率):$1-P(x^2 \leq r)$
  • as_num

    • 轮子,科学计数法转化为浮点型数据
  • stats.chisquare

    Calculate a one-way chi square test.

    The chi square test tests the null hypothesis that the categorical data has the given frequencies.

    • obs: 观测值
    • exp: 理论值
    • p_value:置信度。一般p大于95%,则可认为“观测到的值和预期值是相近的”

3. 参考资料

北邮 信息隐藏 DH_10.1基于LSB的隐写与隐写分析 - 百度文库

用Python进行卡方分析 - 简书

  • Lab
  • Computer Graphics
  • Lab
WebGoat、JuiceShop搭建以及实验
Evil Twin 实验
© 2024 何决云 载入天数...