You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
3.6 KiB
Go
155 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
func check(err error) {
|
|
if err != nil {
|
|
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()
|
|
info, _ := f.Stat()
|
|
fmt.Printf("size: %d\n",info.Size())
|
|
check(err)
|
|
fout, err := os.Create(outputFile)
|
|
check(err)
|
|
defer fout.Close()
|
|
DecryptDataPng(f, fout)
|
|
} else {
|
|
f, err := os.Open(imageFile)
|
|
check(err)
|
|
defer f.Close()
|
|
check(err)
|
|
fout, err := os.Create(outputFile)
|
|
check(err)
|
|
defer fout.Close()
|
|
fin, err := os.Open(inputFile)
|
|
check(err)
|
|
defer fin.Close()
|
|
EncryptDataPng(f, fin, fout)
|
|
}
|
|
}
|
|
|
|
func EncryptDataPng(f *os.File, fin *os.File, fout *os.File) {
|
|
png := PngData{}
|
|
err := png.Read(f)
|
|
check(err)
|
|
inputData := readFileFull(fin)
|
|
inputData, err = encryptData(inputData)
|
|
check(err)
|
|
cryptChunk := CreateChunk(inputData, "crPt")
|
|
png.AddMetaChunk(cryptChunk)
|
|
err = png.Write(fout)
|
|
check(err)
|
|
}
|
|
|
|
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 {
|
|
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
|
|
func encryptData(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 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
|
|
// https://stackoverflow.com/questions/18817336/golang-encrypting-a-string-with-aes-and-base64
|
|
|
|
func encrypt(key, text []byte) ([]byte, error) {
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
b := base64.StdEncoding.EncodeToString(text)
|
|
cipherText := make([]byte, aes.BlockSize+len(b))
|
|
iv := cipherText[:aes.BlockSize]
|
|
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
|
return nil, err
|
|
}
|
|
cfb := cipher.NewCFBEncrypter(block, iv)
|
|
cfb.XORKeyStream(cipherText[aes.BlockSize:], []byte(b))
|
|
return cipherText, nil
|
|
}
|
|
|
|
func decrypt(key, text []byte) ([]byte, error) {
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(text) < aes.BlockSize {
|
|
return nil, errors.New("ciphertext too short")
|
|
}
|
|
iv := text[:aes.BlockSize]
|
|
text = text[aes.BlockSize:]
|
|
cfb := cipher.NewCFBDecrypter(block, iv)
|
|
cfb.XORKeyStream(text, text)
|
|
data, err := base64.StdEncoding.DecodeString(string(text))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return data, nil
|
|
} |