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