|
|
|
@ -5,7 +5,6 @@ import (
|
|
|
|
|
"crypto/cipher"
|
|
|
|
|
"crypto/rand"
|
|
|
|
|
"crypto/sha512"
|
|
|
|
|
"encoding/base64"
|
|
|
|
|
"errors"
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
@ -145,40 +144,32 @@ func readPassword(passwordSalt *[]byte) ([]byte, []byte) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
|
func encrypt(key, data []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))
|
|
|
|
|
cipherText := make([]byte, aes.BlockSize+len(data))
|
|
|
|
|
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))
|
|
|
|
|
cfb.XORKeyStream(cipherText[aes.BlockSize:], data)
|
|
|
|
|
return cipherText, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func decrypt(key, text []byte) ([]byte, error) {
|
|
|
|
|
func decrypt(key, data []byte) ([]byte, error) {
|
|
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if len(text) < aes.BlockSize {
|
|
|
|
|
if len(data) < aes.BlockSize {
|
|
|
|
|
return nil, errors.New("ciphertext too short")
|
|
|
|
|
}
|
|
|
|
|
iv := text[:aes.BlockSize]
|
|
|
|
|
text = text[aes.BlockSize:]
|
|
|
|
|
iv := data[:aes.BlockSize]
|
|
|
|
|
data = data[aes.BlockSize:]
|
|
|
|
|
cfb := cipher.NewCFBDecrypter(block, iv)
|
|
|
|
|
cfb.XORKeyStream(text, text)
|
|
|
|
|
data, err := base64.StdEncoding.DecodeString(string(text))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
cfb.XORKeyStream(data, data)
|
|
|
|
|
return data, nil
|
|
|
|
|
}
|