|
|
@ -37,12 +37,20 @@ var outputFile string
|
|
|
|
var imageFile string
|
|
|
|
var imageFile string
|
|
|
|
var decryptImage bool
|
|
|
|
var decryptImage bool
|
|
|
|
func main() {
|
|
|
|
func main() {
|
|
|
|
flag.StringVar(&imageFile, "image", "image.png", "The path of the png file.")
|
|
|
|
encryptFlags := flag.NewFlagSet("encrypt", flag.ContinueOnError)
|
|
|
|
flag.BoolVar(&decryptImage, "decrypt", false, "If the input image should be decrypted.")
|
|
|
|
decryptFlags := flag.NewFlagSet("decrypt", flag.ContinueOnError)
|
|
|
|
flag.StringVar(&outputFile, "out", "out.png", "The output file for the encrypted/decrypted data.")
|
|
|
|
encryptFlags.StringVar(&imageFile, "img", "image.png", "The path of the png file.")
|
|
|
|
flag.StringVar(&inputFile, "in", "input.txt","The file with the input data.")
|
|
|
|
encryptFlags.StringVar(&imageFile, "image", "image.png", "The path of the png file.")
|
|
|
|
|
|
|
|
decryptFlags.StringVar(&imageFile, "img", "image.png", "The path of the png file.")
|
|
|
|
|
|
|
|
decryptFlags.StringVar(&imageFile, "image", "image.png", "The path of the png file.")
|
|
|
|
|
|
|
|
encryptFlags.StringVar(&inputFile, "in", "input.txt","The file with the input data.")
|
|
|
|
|
|
|
|
encryptFlags.StringVar(&outputFile, "out", "out.png", "The output filename for the image.")
|
|
|
|
|
|
|
|
decryptFlags.StringVar(&outputFile, "out", "out.txt", "The output file for the decrypted data.")
|
|
|
|
flag.Parse()
|
|
|
|
flag.Parse()
|
|
|
|
if decryptImage {
|
|
|
|
switch os.Args[1] {
|
|
|
|
|
|
|
|
case "encrypt":
|
|
|
|
|
|
|
|
err := encryptFlags.Parse(os.Args[2:])
|
|
|
|
|
|
|
|
check(err)
|
|
|
|
f, err := os.Open(imageFile)
|
|
|
|
f, err := os.Open(imageFile)
|
|
|
|
check(err)
|
|
|
|
check(err)
|
|
|
|
defer f.Close()
|
|
|
|
defer f.Close()
|
|
|
@ -50,8 +58,13 @@ func main() {
|
|
|
|
fout, err := os.Create(outputFile)
|
|
|
|
fout, err := os.Create(outputFile)
|
|
|
|
check(err)
|
|
|
|
check(err)
|
|
|
|
defer fout.Close()
|
|
|
|
defer fout.Close()
|
|
|
|
DecryptDataPng(f, fout)
|
|
|
|
fin, err := os.Open(inputFile)
|
|
|
|
} else {
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
defer fin.Close()
|
|
|
|
|
|
|
|
EncryptDataPng(f, fin, fout)
|
|
|
|
|
|
|
|
case "decrypt":
|
|
|
|
|
|
|
|
err := decryptFlags.Parse(os.Args[2:])
|
|
|
|
|
|
|
|
check(err)
|
|
|
|
f, err := os.Open(imageFile)
|
|
|
|
f, err := os.Open(imageFile)
|
|
|
|
check(err)
|
|
|
|
check(err)
|
|
|
|
defer f.Close()
|
|
|
|
defer f.Close()
|
|
|
@ -59,10 +72,7 @@ func main() {
|
|
|
|
fout, err := os.Create(outputFile)
|
|
|
|
fout, err := os.Create(outputFile)
|
|
|
|
check(err)
|
|
|
|
check(err)
|
|
|
|
defer fout.Close()
|
|
|
|
defer fout.Close()
|
|
|
|
fin, err := os.Open(inputFile)
|
|
|
|
DecryptDataPng(f, fout)
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
defer fin.Close()
|
|
|
|
|
|
|
|
EncryptDataPng(f, fin, fout)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -149,6 +159,7 @@ func readPassword(passwordSalt *[]byte) ([]byte, []byte) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// function to encrypt the data
|
|
|
|
func encrypt(key, data []byte) ([]byte, error) {
|
|
|
|
func encrypt(key, data []byte) ([]byte, error) {
|
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -164,13 +175,14 @@ func encrypt(key, data []byte) ([]byte, error) {
|
|
|
|
return cipherText, nil
|
|
|
|
return cipherText, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// function to decrypt the data
|
|
|
|
func decrypt(key, data []byte) ([]byte, error) {
|
|
|
|
func decrypt(key, data []byte) ([]byte, error) {
|
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(data) < aes.BlockSize {
|
|
|
|
if len(data) < aes.BlockSize {
|
|
|
|
return nil, errors.New("ciphertext too short")
|
|
|
|
return nil, errors.New("data too short")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iv := data[:aes.BlockSize]
|
|
|
|
iv := data[:aes.BlockSize]
|
|
|
|
data = data[aes.BlockSize:]
|
|
|
|
data = data[aes.BlockSize:]
|
|
|
|