基于python的LSB隐写与分析

隐写

效果

隐写前

1717033-20190618203529805-1180578749.png

隐写后

1717033-20190618203544707-1311027845.png

灰度值直方图差别

1717033-20190618203625597-99192646.png

部分源码

def dec_to_bin(dec):
    return '{:08b}'.format(dec)

def bin_to_dec(binary_code):
    dec = 0
    for i in range(len(binary_code) - 1):
        dec = dec + int(binary_code[i]) * int(pow(2, 7 - i))
    return dec

# 文件信息转二进制流
def read_data_file(path):
    fp = open(path, "rb")
    stream = ""
    s = fp.read()
    for i in range(len(s)):
        tmp = bin(s[i]).zfill(8)
        stream = stream + tmp.replace('0b', '')
    fp.close()
    return stream

def lsb(image, data_stream, random_index):
    for i in range(len(stream)):
        x = random_index[i] % image.shape[0]
        y = int(random_index[i] / image.shape[0])
        value = image[x, y]
        if value % 2 != stream[i]:
            if value % 2 == 1:
                image[x, y] = value - 1
            else:
                image[x, y] = value + 1
    return image

分析

效果

由于二次隐写的随机性,分析图片存在误差,但能够看出是否被隐写

原图

1717033-20190618203759374-628799957.png

隐写后

1717033-20190618203812737-1053027593.png

部分源码

# 进行二次隐写
def random_steg(image, rate):
    pixel_len = image.shape[0] * image.shape[1]
    random_ls = random.sample(range(0, pixel_len), int(pixel_len * rate))
    random_ls.sort()
    for i in random_ls:
        k = random.randint(0, 1)
        x = i % image.shape[0]
        y = int(i / image.shape[0])
        value = image[x, y]
        if not value % 2 == k:
            if value % 2 == 1:
                image[x, y] = value - 1
            else:
                image[x, y] = value + 1
    return image

# 获取灰度值
def get_gary_value(my_img):
    pixel_value = []
    gary_index = []
    for i in range(256):
        pixel_value.append(0)
        gary_index.append(i)
    for i in range(my_img.shape[0]):
        for j in range(my_img.shape[1]):
            pixel_value[my_img[i][j]] = pixel_value[my_img[i][j]] + 1
    return pixel_value, gary_index

# 计算F1,F2
def calculate_f1f2(values):
    f1 = 0
    f2 = 0
    for i in range(128):
        tmp = abs(values[2 * i + 1] - values[2 * i])
        f1 += tmp
    for j in range(127):
        tmp = abs(values[2 * j + 2] - values[2 * j + 1])
        f2 += tmp
    f2 += abs(values[0] - values[255])
    return f1, f2

# 分析函数
def analysis(path):
    img = cv2.imread(path, 0)
    # 二次随机隐写
    F1 = []
    F2 = []
    index = []
    for k in range(11):
        rate = k / 10
        index.append(rate)
        new_img = random_steg(img, rate)
        new_count, new_index = get_gary_value(new_img)
        f_1, f_2 = calculate_f1f2(new_count)
        F1.append(f_1)
        F2.append(f_2)
    draw(F1, F2, index)

相关链接

项目链接

linzijie1998/lsb_steg_analysis

参考文档

隐写与隐写分析

转载于:https://www.cnblogs.com/linzijie1998/p/11045131.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值