实验编号: 003
图像隐写工具
概念阶段用 Go 实现基于最低有效位(LSB)的图像隐写,将带长度前缀的载荷嵌入 PNG 像素的红色通道。
目标
通过修改像素数据的最低有效位实现隐蔽信道,并核验提取结果与原始载荷完全一致。
约束
载荷大小限于(宽度 × 高度)/ 8 字节减去 4 字节长度头。不能抵抗 JPEG 重新编码或图像缩放。
- Go
- 密码学
- 图像处理
源代码 · 原始语言src/main.go
1package main23import (4 "encoding/binary"5 "fmt"6 "image"7 "image/color"8 "image/png"9 "os"10)1112func embed(img *image.NRGBA, payload []byte) error {13 header := make([]byte, 4)14 binary.BigEndian.PutUint32(header, uint32(len(payload)))15 bits := append(header, payload...)1617 idx := 018 for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {19 for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {20 if idx >= len(bits)*8 {21 return nil22 }23 c := img.NRGBAAt(x, y)24 bytePos, bitPos := idx/8, uint(7-idx%8)25 bit := (bits[bytePos] >> bitPos) & 126 c.R = (c.R & 0xFE) | bit27 img.SetNRGBA(x, y, color.NRGBA{R: c.R, G: c.G, B: c.B, A: c.A})28 idx++29 }30 }31 if idx == len(bits)*8 {32 return nil // The payload filled the image exactly.33 }34 return fmt.Errorf("image too small for payload")35}3637func main() {38 f, _ := os.Open("cover.png")39 defer f.Close()40 src, _ := png.Decode(f)4142 dst := image.NewNRGBA(src.Bounds())43 for y := dst.Bounds().Min.Y; y < dst.Bounds().Max.Y; y++ {44 for x := dst.Bounds().Min.X; x < dst.Bounds().Max.X; x++ {45 dst.Set(x, y, src.At(x, y))46 }47 }4849 _ = embed(dst, []byte("HIDDEN_MESSAGE"))5051 out, _ := os.Create("stego.png")52 defer out.Close()53 png.Encode(out, dst)54 fmt.Println("Done — payload embedded into stego.png")55}只读代码UTF-8