go语言实现将重要数据写入图片中

 更新时间:2015年03月20日 09:43:57   投稿:hebedich  
本文给大家分享的是go语言实现将数据的二进制形式写入图像红色通道数据二进制的低位,从而实现将重要数据隐藏,有需要的小伙伴参考下吧。

原理:将数据的二进制形式写入图像红色通道数据二进制的低位
只支持png格式的输出
写入数据
go run shadow.go -in="c.jpg" -data="hide me" -out="out.png"
读取数据
go run shadow.go -in="out.png"

复制代码 代码如下:

package main
import (
    "errors"
    "flag"
    "fmt"
    "image"
    "image/color"
    _ "image/jpeg"
    "image/png"
    "log"
    "math"
    "os"
)
var FLAG = [4]byte{0x13, 0x14, 0x52, 0x00} //shadow flag.
//byte to 8 bits
func Byte2bits(b byte) (a [8]byte) {
    var c uint8 = 7
    var i uint8
    for i = 0; i < 8; i++ {
        a[i] = b >> (c - i) & 1
    }
    return
}
//8 bits to byte.
func Bits2Byte(a [8]byte) (b byte) {
    for i := 0; i < 8; i++ {
        b += a[i] * uint8(math.Pow(2, float64(7-i)))
    }
    return
}
//uint32 to 4 bytes.
func Uint32ToBytes(i uint32) (b [4]byte) {
    b[0] = uint8(i >> 24)
    b[1] = uint8(i >> 16 & 0xffff)
    b[2] = uint8(i >> 8 & 0xff)
    b[3] = uint8(i & 0xff)
    return
}
//4 bytes to uint32.
func Bytes2Uint32(b [4]byte) (i uint32) {
    var j uint32
    for ; j < 4; j++ {
        i += uint32(b[j]) << (24 - j*8)
    }
    return
}
func BuildShadowHeader(length uint32) (b [8]byte) {
    var i int
    for ; i < 4; i++ {
        b[i] = FLAG[i]
    }
    a := Uint32ToBytes(length)
    for ; i < 8; i++ {
        b[i] = a[i-4]
    }
    return
}
func WriteShadow(b []byte, im image.Image) (out image.Image, err error) {
    max := im.Bounds().Max.X*im.Bounds().Max.Y/8 - 64
    b_len := len(b)
    if len(b) > max {
        return nil, errors.New("image does not have enough space for shadow.")
    }
    head := BuildShadowHeader(uint32(b_len))
    var bb byte
    var bs [8]byte
    var i int
    out, err = SetImage(im, func(index, x, y int, in, out image.Image) {
        rgba := readRGBAColor(im.At(x, y))
        if index < b_len*8+64 {
            if index < 64 {
                bb = head[index/8]
            } else {
                bb = b[index/8-8]
            }
            bs = Byte2bits(bb)
            i = index % 8
            if bs[i] != rgba.R&1 {
                if bs[i] == 0 {
                    rgba.R -= 1
                } else {
                    rgba.R += 1
                }
            }
        }
        if v := out.(*image.RGBA); v != nil {
            v.SetRGBA(x, y, rgba)
        }
    })
    if err != nil {
        return nil, err
    }
    return
}
func ReadShadowData(im image.Image) (b []byte, err error) {
    head, err := ReadShadowHeader(im)
    if err != nil {
        return nil, err
    }
    length := int(ReadShadowLength(head))
    var bk []byte = make([]byte, length*8)
    b = make([]byte, length)
    _, err = SetImage(im, func(index, x, y int, in, out image.Image) {
        if index >= 64 && index < length*8+64 {
            R := readRGBAColor(im.At(x, y)).R
            bk[index-64] = uint8(R & 1)
        }
    })
    var bb [8]byte
    var bs []byte
    for i := 0; i < length; i++ {
        bs = bk[8*i : 8*(i+1)]
        for j := 0; j < 8; j++ {
            bb[j] = bs[j]
        }
        b[i] = Bits2Byte(bb)
    }
    return
}
func ReadShadowHeader(im image.Image) (b [8]byte, err error) {
    var bm [64]byte
    _, err = SetImage(im, func(index, x, y int, in, out image.Image) {
        rgba := readRGBAColor(im.At(x, y))
        if index < 64 {
            bm[index] = uint8(rgba.R & 1)
        }
    })
    if err != nil {
        return
    }
    var bb [8]byte
    var bs []byte
    for i := 0; i < 8; i++ {
        bs = bm[8*i : 8*(i+1)]
        for j := 0; j < 8; j++ {
            bb[j] = bs[j]
        }
        b[i] = Bits2Byte(bb)
    }
    return
}
func ReadShadowFlag(b [8]byte) (a [4]byte) {
    for i := 0; i < 4; i++ {
        a[i] = b[i]
    }
    return
}
func ReadShadowLength(b [8]byte) uint32 {
    var bb [4]byte
    for i := 4; i < 8; i++ {
        bb[i-4] = b[i]
    }
    return Bytes2Uint32(bb)
}
func OpenImage(path string) (image.Image, error) {
    im_read, err := os.Open(path)
    defer im_read.Close()
    if err != nil {
        return nil, err
    }
    im, _, err := image.Decode(im_read)
    if err != nil {
        return nil, err
    }
    return im, nil
}
//modify image
func SetImage(im image.Image, f func(index, x, y int, in, out image.Image)) (out image.Image, err error) {
    if f == nil {
        return im, nil
    }
    index := 0
    bounds := im.Bounds()
    out = image.NewRGBA(bounds)
    var m *image.RGBA = out.(*image.RGBA)
    for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
        for x := bounds.Min.X; x < bounds.Max.X; x++ {
            m.Set(x, y, im.At(x, y))
            f(index, x, y, im, out)
            index += 1
        }
    }
    return out, nil
}
//conert any color to RABGA color.
func readRGBAColor(from_color color.Color) color.RGBA {
    return color.RGBAModel.Convert(from_color).(color.RGBA)
}
//only write to jpeg formats.
func WriteImage(path string, im image.Image) error {
    out, err := os.OpenFile(path, os.O_CREATE, os.ModePerm)
    defer out.Close()
    if err != nil {
        return err
    }
    err = png.Encode(out, im)
    if err != nil {
        return err
    }
    return nil
}
var read_in string
var write_out string
var data string
func init() {
    flag.StringVar(&read_in, "in", "", "image path read in.")
    flag.StringVar(&write_out, "out", "out.jpg", "image path write out.")
    flag.StringVar(&data, "data", "", "data to shadow.")
}
func errHandle(err error) {
    if err != nil {
        log.Fatal(err)
    }
}
func main() {
    flag.Parse()
    if read_in == "" {
        fmt.Println("Options:")
        flag.PrintDefaults()
        return
    }
    im, err := OpenImage(read_in)
    errHandle(err)
    if data != "" {
        out, err := WriteShadow([]byte(data), im)
        errHandle(err)
        err = WriteImage(write_out, out)
        errHandle(err)
    } else {
        head, err := ReadShadowHeader(im)
        errHandle(err)
        _flag := ReadShadowFlag(head)
        if _flag != FLAG {
            fmt.Println("image doesn't have shadow data.")
            return
        }
        data, err := ReadShadowData(im)
        errHandle(err)
        fmt.Println("shadow:", string(data))
    }
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关文章

  • 详解Go语言如何实现类似Python中的with上下文管理器

    详解Go语言如何实现类似Python中的with上下文管理器

    熟悉 Python 的同学应该知道 Python 中的上下文管理器非常好用,那么在 Go 中是否也能实现上下文管理器呢,下面小编就来和大家仔细讲讲吧
    2023-07-07
  • Go语言应用闭包之返回函数

    Go语言应用闭包之返回函数

    这篇文章主要介绍了Go语言应用闭包之返回函数,对于非常底层的纯 Go 语言代码或者包而言,在各个操作系统平台上的可移植性是非常强的,只需要将源码拷贝到相应平台上进行编译即可,或者可以使用交叉编译来构建目标平台的应用程序,需要的朋友可以参考下
    2023-07-07
  • 详解MongoDB Go Driver如何记录日志

    详解MongoDB Go Driver如何记录日志

    这篇文章主要为大家介绍了MongoDB Go Driver如何记录日志详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • Go结合Gin导出Mysql数据到Excel表格

    Go结合Gin导出Mysql数据到Excel表格

    本文主要介绍了Go结合Gin导出Mysql数据到Excel表格,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • go mod更新指定的tag的包后,go vendor内容未更新问题

    go mod更新指定的tag的包后,go vendor内容未更新问题

    这篇文章主要介绍了go mod更新指定的tag的包后,go vendor内容未更新问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • golang端口占用检测的使用

    golang端口占用检测的使用

    这篇文章主要介绍了golang端口占用检测的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • 解决golang时间字符串转time.Time的坑

    解决golang时间字符串转time.Time的坑

    这篇文章主要介绍了解决golang时间字符串转time.Time的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • GO语言操作Elasticsearch示例分享

    GO语言操作Elasticsearch示例分享

    这篇文章主要介绍了GO语言操作Elasticsearch示例分享的相关资料,需要的朋友可以参考下
    2023-01-01
  • golang goquery selector选择器使用示例大全

    golang goquery selector选择器使用示例大全

    这篇文章主要为大家介绍了golang goquery selector选择器使用示例大全,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • Golang通脉之map详情

    Golang通脉之map详情

    这篇文章主要介绍了Golang通脉之map,Go语言中提供的映射关系容器为map,其内部使用散列表(hash)实现,map 是一种无序的键值对的集合。map 最重要的一点是通过 key 来快速检索数据,key 类似于索引,指向数据的值 map 是一种集合,所以可以像迭代数组和切片那样迭代它
    2021-10-10

最新评论