-
Notifications
You must be signed in to change notification settings - Fork 44
/
steganography.go
351 lines (282 loc) · 9.23 KB
/
steganography.go
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Package steganography is a library that provides functions to execute steganography encoding and decoding in a given image. It is also able to check the maximum encoding size, and the size of an encoded message.
package steganography
import (
"bytes"
"errors"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
)
// EncodeNRGBA encodes a given string into the input image using least significant bit encryption (LSB steganography)
// The minnimum image size is 24 pixels for one byte. For each additional byte, it is necessary 3 more pixels.
/*
Input:
writeBuffer *bytes.Buffer : the destination of the encoded image bytes
pictureInputFile image.NRGBA : image data used in encoding
message []byte : byte slice of the message to be encoded
Output:
bytes buffer ( io.writter ) to create file, or send data.
*/
func EncodeNRGBA(writeBuffer *bytes.Buffer, rgbImage *image.NRGBA, message []byte) error {
var messageLength = uint32(len(message))
var width = rgbImage.Bounds().Dx()
var height = rgbImage.Bounds().Dy()
var c color.NRGBA
var bit byte
var ok bool
//var encodedImage image.Image
if MaxEncodeSize(rgbImage) < messageLength+4 {
return errors.New("message too large for image")
}
one, two, three, four := splitToBytes(messageLength)
message = append([]byte{four}, message...)
message = append([]byte{three}, message...)
message = append([]byte{two}, message...)
message = append([]byte{one}, message...)
ch := make(chan byte, 100)
go getNextBitFromString(message, ch)
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
c = rgbImage.NRGBAAt(x, y) // get the color at this pixel
/* RED */
bit, ok = <-ch
if !ok { // if we don't have any more bits left in our message
rgbImage.SetNRGBA(x, y, c)
png.Encode(writeBuffer, rgbImage)
// return *writeBuffer, nil
}
setLSB(&c.R, bit)
/* GREEN */
bit, ok = <-ch
if !ok {
rgbImage.SetNRGBA(x, y, c)
png.Encode(writeBuffer, rgbImage)
return nil
}
setLSB(&c.G, bit)
/* BLUE */
bit, ok = <-ch
if !ok {
rgbImage.SetNRGBA(x, y, c)
png.Encode(writeBuffer, rgbImage)
return nil
}
setLSB(&c.B, bit)
rgbImage.SetNRGBA(x, y, c)
}
}
err := png.Encode(writeBuffer, rgbImage)
fmt.Println("err")
return err
}
// Encode encodes a given string into the input image using least significant bit encryption (LSB steganography)
// The minnimum image size is 23 pixels
// It wraps EncodeNRGBA making the conversion from image.Image to image.NRGBA
/*
Input:
writeBuffer *bytes.Buffer : the destination of the encoded image bytes
message []byte : byte slice of the message to be encoded
pictureInputFile image.Image : image data used in encoding
Output:
bytes buffer ( io.writter ) to create file, or send data.
*/
func Encode(writeBuffer *bytes.Buffer, pictureInputFile image.Image, message []byte) error {
rgbImage := imageToNRGBA(pictureInputFile)
return EncodeNRGBA(writeBuffer, rgbImage, message)
}
// decodeNRGBA gets messages from pictures using LSB steganography, decode the message from the picture and return it as a sequence of bytes
/*
Input:
startOffset uint32 : number of bytes used to declare size of message
msgLen uint32 : size of the message to be decoded
pictureInputFile image.NRGBA : image data used in decoding
Output:
message []byte decoded from image
*/
func decodeNRGBA(startOffset uint32, msgLen uint32, rgbImage *image.NRGBA) (message []byte) {
var byteIndex uint32
var bitIndex uint32
width := rgbImage.Bounds().Dx()
height := rgbImage.Bounds().Dy()
var c color.NRGBA
var lsb byte
message = append(message, 0)
// iterate through every pixel in the image and stitch together the message bit by bit
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
c = rgbImage.NRGBAAt(x, y) // get the color of the pixel
/* RED */
lsb = getLSB(c.R) // get the least significant bit from the red component of this pixel
message[byteIndex] = setBitInByte(message[byteIndex], bitIndex, lsb) // add this bit to the message
bitIndex++
if bitIndex > 7 { // when we have filled up a byte, move on to the next byte
bitIndex = 0
byteIndex++
if byteIndex >= msgLen+startOffset {
return message[startOffset : msgLen+startOffset]
}
message = append(message, 0)
}
/* GREEN */
lsb = getLSB(c.G)
message[byteIndex] = setBitInByte(message[byteIndex], bitIndex, lsb)
bitIndex++
if bitIndex > 7 {
bitIndex = 0
byteIndex++
if byteIndex >= msgLen+startOffset {
return message[startOffset : msgLen+startOffset]
}
message = append(message, 0)
}
/* BLUE */
lsb = getLSB(c.B)
message[byteIndex] = setBitInByte(message[byteIndex], bitIndex, lsb)
bitIndex++
if bitIndex > 7 {
bitIndex = 0
byteIndex++
if byteIndex >= msgLen+startOffset {
return message[startOffset : msgLen+startOffset]
}
message = append(message, 0)
}
}
}
return
}
// decode gets messages from pictures using LSB steganography, decode the message from the picture and return it as a sequence of bytes
// It wraps EncodeNRGBA making the conversion from image.Image to image.NRGBA
/*
Input:
startOffset uint32 : number of bytes used to declare size of message
msgLen uint32 : size of the message to be decoded
pictureInputFile image.Image : image data used in decoding
Output:
message []byte decoded from image
*/
func decode(startOffset uint32, msgLen uint32, pictureInputFile image.Image) (message []byte) {
rgbImage := imageToNRGBA(pictureInputFile)
return decodeNRGBA(startOffset, msgLen, rgbImage)
}
// Decode gets messages from pictures using LSB steganography, decode the message from the picture and return it as a sequence of bytes
// It wraps EncodeNRGBA making the conversion from image.Image to image.NRGBA
/*
Input:
msgLen uint32 : size of the message to be decoded
pictureInputFile image.Image : image data used in decoding
Output:
message []byte decoded from image
*/
func Decode(msgLen uint32, pictureInputFile image.Image) (message []byte) {
return decode(4, msgLen, pictureInputFile) // the offset of 4 skips the "header" where message length is defined
}
// MaxEncodeSize given an image will find how many bytes can be stored in that image using least significant bit encoding
// ((width * height * 3) / 8 ) - 4
// The result must be at least 4,
func MaxEncodeSize(img image.Image) uint32 {
width := img.Bounds().Dx()
height := img.Bounds().Dy()
eval := ((width * height * 3) / 8) - 4
if eval < 4 {
eval = 0
}
return uint32(eval)
}
// GetMessageSizeFromImage gets the size of the message from the first four bytes encoded in the image
func GetMessageSizeFromImage(pictureInputFile image.Image) (size uint32) {
sizeAsByteArray := decode(0, 4, pictureInputFile)
size = combineToInt(sizeAsByteArray[0], sizeAsByteArray[1], sizeAsByteArray[2], sizeAsByteArray[3])
return
}
// getNextBitFromString each call will return the next subsequent bit in the string
func getNextBitFromString(byteArray []byte, ch chan byte) {
var offsetInBytes int
var offsetInBitsIntoByte int
var choiceByte byte
lenOfString := len(byteArray)
for {
if offsetInBytes >= lenOfString {
close(ch)
return
}
choiceByte = byteArray[offsetInBytes]
ch <- getBitFromByte(choiceByte, offsetInBitsIntoByte)
offsetInBitsIntoByte++
if offsetInBitsIntoByte >= 8 {
offsetInBitsIntoByte = 0
offsetInBytes++
}
}
}
// getLSB given a byte, will return the least significant bit of that byte
func getLSB(b byte) byte {
if b%2 == 0 {
return 0
}
return 1
}
// setLSB given a byte will set that byte's least significant bit to a given value (where true is 1 and false is 0)
func setLSB(b *byte, bit byte) {
if bit == 1 {
*b = *b | 1
} else if bit == 0 {
var mask byte = 0xFE
*b = *b & mask
}
}
// getBitFromByte given a bit will return a bit from that byte
func getBitFromByte(b byte, indexInByte int) byte {
b = b << uint(indexInByte)
var mask byte = 0x80
var bit = mask & b
if bit == 128 {
return 1
}
return 0
}
// setBitInByte sets a specific bit in a byte to a given value and returns the new byte
func setBitInByte(b byte, indexInByte uint32, bit byte) byte {
var mask byte = 0x80
mask = mask >> uint(indexInByte)
if bit == 0 {
mask = ^mask
b = b & mask
} else if bit == 1 {
b = b | mask
}
return b
}
// combineToInt given four bytes, will return the 32 bit unsigned integer which is the composition of those four bytes (one is MSB)
func combineToInt(one, two, three, four byte) (ret uint32) {
ret = uint32(one)
ret = ret << 8
ret = ret | uint32(two)
ret = ret << 8
ret = ret | uint32(three)
ret = ret << 8
ret = ret | uint32(four)
return
}
// splitToBytes given an unsigned integer, will split this integer into its four bytes
func splitToBytes(x uint32) (one, two, three, four byte) {
one = byte(x >> 24)
var mask uint32 = 255
two = byte((x >> 16) & mask)
three = byte((x >> 8) & mask)
four = byte(x & mask)
return
}
// imageToNRGBA converts image.Image to image.NRGBA
func imageToNRGBA(src image.Image) *image.NRGBA {
bounds := src.Bounds()
var m *image.NRGBA
var width, height int
width = bounds.Dx()
height = bounds.Dy()
m = image.NewNRGBA(image.Rect(0, 0, width, height))
draw.Draw(m, m.Bounds(), src, bounds.Min, draw.Src)
return m
}