|
|
@ -14,60 +14,89 @@ import (
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var inputFile string
|
|
|
|
func check(err error) {
|
|
|
|
var filename string
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
flag.StringVar(&inputFile, "input", "input.txt","The file with the input data.")
|
|
|
|
|
|
|
|
flag.StringVar(&filename, "image", "image.png", "The path of the png file.")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
fmt.Println(filename)
|
|
|
|
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var inputFile string
|
|
|
|
|
|
|
|
var outputFile string
|
|
|
|
|
|
|
|
var imageFile string
|
|
|
|
|
|
|
|
var decryptImage bool
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
flag.StringVar(&imageFile, "image", "image.png", "The path of the png file.")
|
|
|
|
|
|
|
|
flag.BoolVar(&decryptImage, "decrypt", false, "If the input image should be decrypted.")
|
|
|
|
|
|
|
|
flag.StringVar(&outputFile, "out", "out.png", "The output file for the encrypted/decrypted data.")
|
|
|
|
|
|
|
|
flag.StringVar(&inputFile, "in", "input.txt","The file with the input data.")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if decryptImage {
|
|
|
|
|
|
|
|
f, err := os.Open(imageFile)
|
|
|
|
|
|
|
|
check(err)
|
|
|
|
defer f.Close()
|
|
|
|
defer f.Close()
|
|
|
|
valid, header := ValidatePng(f)
|
|
|
|
info, _ := f.Stat()
|
|
|
|
if valid {
|
|
|
|
fmt.Printf("size: %d\n",info.Size())
|
|
|
|
fout, err := os.Create("encrypted-" + filename)
|
|
|
|
check(err)
|
|
|
|
if err != nil {
|
|
|
|
fout, err := os.Create(outputFile)
|
|
|
|
log.Fatal(err)
|
|
|
|
check(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer fout.Close()
|
|
|
|
defer fout.Close()
|
|
|
|
_, _ = fout.Write(header)
|
|
|
|
DecryptDataPng(f, fout)
|
|
|
|
chunk, err := ReadChunk(f)
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
f, err := os.Open(imageFile)
|
|
|
|
log.Fatal(err)
|
|
|
|
check(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
for chunk.name != "IDAT" {
|
|
|
|
check(err)
|
|
|
|
fmt.Printf("l: %d, n: %s, c: %d\n", chunk.length, chunk.name, chunk.crc)
|
|
|
|
fout, err := os.Create(outputFile)
|
|
|
|
_, _ = fout.Write(chunk.raw)
|
|
|
|
check(err)
|
|
|
|
chunk, err = ReadChunk(f)
|
|
|
|
defer fout.Close()
|
|
|
|
if err != nil {
|
|
|
|
fin, err := os.Open(inputFile)
|
|
|
|
log.Fatal(err)
|
|
|
|
check(err)
|
|
|
|
}
|
|
|
|
defer fin.Close()
|
|
|
|
|
|
|
|
EncryptDataPng(f, fin, fout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
}
|
|
|
|
fmt.Print("Data to encrypt: ")
|
|
|
|
|
|
|
|
inputDataString, _ := reader.ReadString('\n')
|
|
|
|
func EncryptDataPng(f *os.File, fin *os.File, fout *os.File) {
|
|
|
|
inputData := []byte(inputDataString)
|
|
|
|
png := PngData{}
|
|
|
|
|
|
|
|
err := png.Read(f)
|
|
|
|
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
inputData := readFileFull(fin)
|
|
|
|
inputData, err = encryptData(inputData)
|
|
|
|
inputData, err = encryptData(inputData)
|
|
|
|
if err != nil {
|
|
|
|
check(err)
|
|
|
|
panic(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cryptChunk := CreateChunk(inputData, "crPt")
|
|
|
|
cryptChunk := CreateChunk(inputData, "crPt")
|
|
|
|
_, _ = fout.Write(cryptChunk.raw)
|
|
|
|
png.AddMetaChunk(cryptChunk)
|
|
|
|
for {
|
|
|
|
err = png.Write(fout)
|
|
|
|
_, _ = fout.Write(chunk.raw)
|
|
|
|
check(err)
|
|
|
|
chunk, err = ReadChunk(f)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
break
|
|
|
|
func DecryptDataPng(f *os.File, fout *os.File) {
|
|
|
|
}
|
|
|
|
png := PngData{}
|
|
|
|
}
|
|
|
|
err := png.Read(f)
|
|
|
|
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
cryptChunk := png.GetChunk("crPt")
|
|
|
|
|
|
|
|
if cryptChunk != nil {
|
|
|
|
|
|
|
|
data, err := decryptData(cryptChunk.data)
|
|
|
|
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
_, err = fout.Write(data)
|
|
|
|
|
|
|
|
check(err)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
log.Fatal("Invalid png.")
|
|
|
|
log.Fatal("no encrypted data inside the input image")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// reads all bytes of a file
|
|
|
|
|
|
|
|
func readFileFull(f *os.File) []byte {
|
|
|
|
|
|
|
|
tmp := make([]byte, 8)
|
|
|
|
|
|
|
|
data := make([]byte, 0)
|
|
|
|
|
|
|
|
_, err := f.Read(tmp)
|
|
|
|
|
|
|
|
for err != io.EOF {
|
|
|
|
|
|
|
|
data = append(data, tmp...)
|
|
|
|
|
|
|
|
_, err = f.Read(tmp)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
data = append(data, tmp...)
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// creates an encrypted png chunk
|
|
|
|
// creates an encrypted png chunk
|
|
|
|
func encryptData(data []byte) ([]byte, error) {
|
|
|
|
func encryptData(data []byte) ([]byte, error) {
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
@ -78,6 +107,15 @@ func encryptData(data []byte) ([]byte, error) {
|
|
|
|
return encrypt(key, data)
|
|
|
|
return encrypt(key, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func decryptData(data []byte) ([]byte, error) {
|
|
|
|
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
|
|
|
|
fmt.Print("Password: ")
|
|
|
|
|
|
|
|
pw, _ := reader.ReadString('\n')
|
|
|
|
|
|
|
|
key := make([]byte, 32 - len(pw))
|
|
|
|
|
|
|
|
key = append(key, []byte(pw)...)
|
|
|
|
|
|
|
|
return decrypt(key, data)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// encrypt and decrypt functions taken from
|
|
|
|
// encrypt and decrypt functions taken from
|
|
|
|
// https://stackoverflow.com/questions/18817336/golang-encrypting-a-string-with-aes-and-base64
|
|
|
|
// https://stackoverflow.com/questions/18817336/golang-encrypting-a-string-with-aes-and-base64
|
|
|
|
|
|
|
|
|
|
|
|