package main import ( "flag" "fmt" "os" ) const ( S = 54 // standard size of bmp headers T = 32 // number of bytes needed to hide the text length C = 4 // number of bytes needed to hide a character ) // modify hides an integer to a byte slice func modify(value int, pix []byte, size int) { for i := 0; i < size; i++ { pix[i] = (pix[i] & 0xFC) | byte(value&0x03) value >>= 2 } // TODO: write your code here // replace last 2 bits of pix[i] with the last 2 bits of value // the next iteration repeats with the next 2 bits of value } var ( srcImage string // input image name srcTxt string // input text name destImage string // output doctored image name ) // init sets command line arguments func init() { // DON'T modify this function!!! flag.StringVar(&srcImage, "i", "", "input image name") flag.StringVar(&srcTxt, "t", "", "input text name") flag.StringVar(&destImage, "d", "", "output doctored image name") } func main() { // parse command line arguments flag.Parse() if srcImage == "" || srcTxt == "" || destImage == "" { flag.PrintDefaults() os.Exit(1) } // read input image to a byte slice p p, err := os.ReadFile(srcImage) if err != nil { fmt.Printf("Read image file failed, err = %v\n", err) os.Exit(1) } // read input text to a byte slice t t, err := os.ReadFile(srcTxt) if err != nil { fmt.Printf("Read text file failed, err = %v\n", err) os.Exit(1) } // check if the text is too big if T+len(t)*C > len(p[S:]) { fmt.Println("The text file is too big") os.Exit(1) } // save the text length to p modify(len(t), p[S:S+T], T) // save the content of text to p for i := 0; i < len(t); i++ { offset := S + T + C*i modify(int(t[i]), p[offset:offset+C], C) } // write the modified p to destImage err = os.WriteFile(destImage, p, 0644) if err != nil { fmt.Printf("Write doctored image failed, err = %v\n", err) os.Exit(1) } }这是一个将文字隐藏到图片中的代码,请帮我讲下面这个恢复文字的代码完善package main import ( "flag" "os" "fmt" ) const ( S = 54 // standard size of header T = 32 // number of bytes needed to hide the text length C = 4 // number of bytes needed to hide a character ) func modify(pix []byte, value int, size int){ for i := size-1; i >= 0; i-- { value = int((pix[i] & 0x03) | byte(value&0xFC)) value <<= 2 } } var ( image string // input doctor image name txt string // output text name ) // init sets command line arguments func init() { // DON'T modify this function!!! flag.StringVar(&image, "i", "", "input image name") flag.StringVar(&txt, "t", "", "output text name") } func main() { // parse command line arguments flag.Parse() if image == "" || txt == "" { flag.PrintDefaults() os.Exit(1) } p, err := os.ReadFile(image) if err != nil { fmt.Printf("Read image file failed, err = %v\n", err) os.Exit(1) } modify(p[S:S+T], len(o), T) for i := len(o)-1; i >= 0; i-- { offset := S + T + C*i modify(p[offset:offset+C], int(o[i]) , C) } err = os.WriteFile(txt, o, 0644) if err != nil { fmt.Printf("Write doctored image failed, err = %v\n", err) os.Exit(1) } }
时间: 2023-06-15 15:05:35 浏览: 202
在这个恢复文字的代码中,我们需要完成以下几个任务:
1. 读取输入的图片文件和输出的文本文件名。
2. 从输入的图片文件中读取已经隐藏的文本长度和文本内容。
3. 将读取到的文本内容写入到输出的文本文件中。
以下是完整的代码实现:
```go
package main
import (
"flag"
"fmt"
"os"
)
const (
S = 54 // standard size of header
T = 32 // number of bytes needed to hide the text length
C = 4 // number of bytes needed to hide a character
)
func modify(pix []byte, value int, size int) {
for i := size - 1; i >= 0; i-- {
pix[i] = (pix[i] & 0xFC) | byte(value&0x03)
value >>= 2
}
}
var (
image string // input doctor image name
txt string // output text name
)
// init sets command line arguments
func init() {
// DON'T modify this function!!!
flag.StringVar(&image, "i", "", "input image name")
flag.StringVar(&txt, "t", "", "output text name")
}
func main() {
// parse command line arguments
flag.Parse()
if image == "" || txt == "" {
flag.PrintDefaults()
os.Exit(1)
}
p, err := os.ReadFile(image)
if err != nil {
fmt.Printf("Read image file failed, err = %v\n", err)
os.Exit(1)
}
// read the length of the hidden text
var textLen int
for i := 0; i < T; i++ {
textLen |= int(p[S+i]&0x03) << (2 * i)
}
// read the content of the hidden text
o := make([]byte, textLen)
for i := 0; i < textLen; i++ {
offset := S + T + C*i
var c int
for j := 0; j < C; j++ {
c |= int(p[offset+j]&0x03) << (2 * j)
}
o[i] = byte(c)
}
// write the content of the hidden text to the output file
err = os.WriteFile(txt, o, 0644)
if err != nil {
fmt.Printf("Write text file failed, err = %v\n", err)
os.Exit(1)
}
}
```
在这个代码中,我们首先读取输入的图片文件和输出的文本文件名。接着,我们从输入的图片文件中读取已经隐藏的文本长度和文本内容,并将文本内容写入到输出的文本文件中。其中,在读取文本长度和文本内容时,我们使用了类似于隐藏文本的代码中的 `modify` 函数,将每个字节的最后两位替换成了需要隐藏的信息。这样,我们就成功地完成了恢复文本的任务。
阅读全文