fix enc/dec bug -.-

This commit is contained in:
Jay 2018-03-09 14:04:27 +08:00
parent 24c1f3f0e7
commit 1609e0ad30
4 changed files with 54 additions and 182 deletions

145
main.go
View File

@ -1,6 +1,7 @@
package main
import (
"errors"
"flag"
"io"
"log"
@ -60,88 +61,17 @@ func main() {
log.Fatal("DstFile has Exists if override add flag -y ")
}
// go to decrypt file
if flags.Decrypt {
decryptAction()
}
flags.KeyFile = tools.ParsePath(flags.KeyFile)
flags.SrcFile = tools.ParsePath(flags.SrcFile)
flags.DstFile = tools.ParsePath(flags.DstFile)
if flags.Encrypt {
// encryptAction()
encrypt()
}
if flags.Decrypt {
decrypt()
}
func decryptAction() {
// open key file
keyFile, err := os.Open(flags.KeyFile)
handleError(err)
defer keyFile.Close()
keys, err := pgpcrypt.ReadKeyFile(keyFile)
handleError(err)
if len(keys) == 0 {
log.Fatal("key file not validate")
}
srcFile, err := os.Open(flags.SrcFile)
handleError(err)
var dstFile *os.File
if tools.CheckExists(flags.DstFile, false) {
dstFile, err = os.Open(flags.DstFile)
handleError(err)
defer dstFile.Close()
dstStat, err := dstFile.Stat()
handleError(err)
err = dstFile.Truncate(dstStat.Size())
handleError(err)
} else {
dstFile, err = os.Create(flags.DstFile)
handleError(err)
defer dstFile.Close()
}
key := keys[0]
err = pgpcrypt.Decrypt(key, flags.Password, srcFile, dstFile)
handleError(err)
}
func encryptAction() {
// open key file
keyFile, err := os.Open(flags.KeyFile)
handleError(err)
defer keyFile.Close()
keys, err := pgpcrypt.ReadKeyFile(keyFile)
handleError(err)
if len(keys) == 0 {
log.Fatal("key file not validate")
}
srcFile, err := os.Open(flags.SrcFile)
handleError(err)
// encBytes, err := pgpcrypt.EncryptBytes(keys, srcFile)
// handleError(err)
// fmt.Println("bytes ::: ", len(encBytes))
// var dstFile *os.File
// if tools.CheckExists(flags.DstFile, false) {
// dstFile, err = os.Open(flags.DstFile)
// handleError(err)
// defer dstFile.Close()
// dstStat, err := dstFile.Stat()
// handleError(err)
// err = dstFile.Truncate(dstStat.Size())
// handleError(err)
// } else {
dstFile, err := os.Create(flags.DstFile)
handleError(err)
defer dstFile.Close()
// }
err = pgpcrypt.Encrypt(keys, srcFile, dstFile)
handleError(err)
}
func handleError(err error) {
@ -154,48 +84,63 @@ func showUsage() {
flag.Usage()
}
func encrypt() {
func decrypt() {
keyFile, err := os.Open(flags.KeyFile)
if err != nil {
log.Fatal(err)
}
handleError(err)
defer keyFile.Close()
var keyList openpgp.EntityList
keys, err := openpgp.ReadArmoredKeyRing(keyFile)
// keys, err := openpgp.ReadKeyRing(keyFile)
if err != nil {
log.Fatal(err)
keyList, err := pgpcrypt.ReadKeyFile(keyFile)
handleError(err)
if len(keyList) == 0 {
handleError(errors.New("key not found"))
}
keyList = append(keyList, keys...)
key := keyList[0]
distFile, err := os.Create(flags.DstFile)
if err != nil {
log.Fatal(err)
passphraseByte := []byte(flags.Password)
key.PrivateKey.Decrypt(passphraseByte)
for _, sub := range key.Subkeys {
sub.PrivateKey.Decrypt(passphraseByte)
}
defer distFile.Close()
// distBuf := new(bytes.Buffer)
dstFile, err := os.Create(flags.DstFile)
handleError(err)
defer dstFile.Close()
srcFile, err := os.Open(flags.SrcFile)
if err != nil {
log.Fatal(err)
handleError(err)
defer srcFile.Close()
md, err := openpgp.ReadMessage(srcFile, keyList, nil, nil)
_, err = io.Copy(dstFile, md.UnverifiedBody)
handleError(err)
}
func encrypt() {
keyFile, err := os.Open(flags.KeyFile)
handleError(err)
defer keyFile.Close()
keyList, err := pgpcrypt.ReadKeyFile(keyFile)
handleError(err)
distFile, err := os.Create(flags.DstFile)
handleError(err)
defer distFile.Close()
srcFile, err := os.Open(flags.SrcFile)
handleError(err)
defer srcFile.Close()
fhint := &openpgp.FileHints{}
fhint.IsBinary = true
tmpWriter, err := openpgp.Encrypt(distFile, keyList, nil, fhint, nil)
if err != nil {
log.Fatal(err)
}
handleError(err)
defer tmpWriter.Close()
_, err = io.Copy(tmpWriter, srcFile)
if err != nil {
log.Fatal(err)
}
handleError(err)
}

View File

@ -1,79 +0,0 @@
package pgpcrypt
import (
"bytes"
"io"
"io/ioutil"
"time"
"golang.org/x/crypto/openpgp"
)
// Encrypt - pgp encrypt func
func Encrypt(key openpgp.EntityList, src io.Reader, dst io.Writer) error {
fileHint := &openpgp.FileHints{}
fileHint.IsBinary = true
fileHint.ModTime = time.Now()
encWriter, err := openpgp.Encrypt(dst, key, nil, fileHint, nil)
if err != nil {
return err
}
_, err = io.Copy(encWriter, src)
if err != nil {
return err
}
return nil
}
// EncryptBytes -
func EncryptBytes(key openpgp.EntityList, src io.Reader) ([]byte, error) {
buf := new(bytes.Buffer)
fileHint := &openpgp.FileHints{}
fileHint.IsBinary = true
encWriter, err := openpgp.Encrypt(buf, key, nil, fileHint, nil)
if err != nil {
return nil, err
}
_, err = io.Copy(encWriter, src)
if err != nil {
return nil, err
}
encBytes, err := ioutil.ReadAll(buf)
if err != nil {
return nil, err
}
return encBytes, nil
}
// Decrypt - pgp decrypt func
func Decrypt(key *openpgp.Entity, keyPassword string, src io.Reader, dst io.Writer) error {
// decode private key
passphraseByte := []byte(keyPassword)
key.PrivateKey.Decrypt(passphraseByte)
for _, sub := range key.Subkeys {
sub.PrivateKey.Decrypt(passphraseByte)
}
var keyList openpgp.EntityList
keyList = append(keyList, key)
md, err := openpgp.ReadMessage(src, keyList, nil, nil)
if err != nil {
return err
}
_, err = io.Copy(dst, md.UnverifiedBody)
if err != nil {
return err
}
return nil
}

View File

@ -9,7 +9,7 @@ import (
// ReadKeyFile - read key from file
func ReadKeyFile(r io.Reader) (openpgp.EntityList, error) {
keys, err := openpgp.ReadArmoredKeyRing(r)
if err != nil {
if err != nil || len(keys) == 0 {
keys, err = openpgp.ReadKeyRing(r)
if err != nil {
return nil, err

View File

@ -8,7 +8,12 @@ import (
)
// ParsePath - parse file path to absPath
func ParsePath(wd, dst string) string {
func ParsePath(dst string) string {
wd, err := os.Getwd()
if err != nil {
wd = ""
}
if []rune(dst)[0] == '~' {
home := UserHomeDir()
if len(home) > 0 {
@ -39,6 +44,7 @@ func UserHomeDir() string {
// CheckExists - check file exists
func CheckExists(filePath string, allowDir bool) bool {
filePath = ParsePath(filePath)
stat, err := os.Stat(filePath)
if err != nil && !os.IsExist(err) {
return false