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

147
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"flag" "flag"
"io" "io"
"log" "log"
@ -60,88 +61,17 @@ func main() {
log.Fatal("DstFile has Exists if override add flag -y ") log.Fatal("DstFile has Exists if override add flag -y ")
} }
// go to decrypt file flags.KeyFile = tools.ParsePath(flags.KeyFile)
if flags.Decrypt { flags.SrcFile = tools.ParsePath(flags.SrcFile)
decryptAction() flags.DstFile = tools.ParsePath(flags.DstFile)
}
if flags.Encrypt { if flags.Encrypt {
// encryptAction() // encryptAction()
encrypt() 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) { func handleError(err error) {
@ -154,48 +84,63 @@ func showUsage() {
flag.Usage() flag.Usage()
} }
func encrypt() { func decrypt() {
keyFile, err := os.Open(flags.KeyFile) keyFile, err := os.Open(flags.KeyFile)
if err != nil { handleError(err)
log.Fatal(err)
}
defer keyFile.Close() defer keyFile.Close()
var keyList openpgp.EntityList keyList, err := pgpcrypt.ReadKeyFile(keyFile)
keys, err := openpgp.ReadArmoredKeyRing(keyFile) handleError(err)
// keys, err := openpgp.ReadKeyRing(keyFile) if len(keyList) == 0 {
if err != nil { handleError(errors.New("key not found"))
log.Fatal(err)
} }
keyList = append(keyList, keys...) key := keyList[0]
distFile, err := os.Create(flags.DstFile) passphraseByte := []byte(flags.Password)
if err != nil { key.PrivateKey.Decrypt(passphraseByte)
log.Fatal(err) 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) srcFile, err := os.Open(flags.SrcFile)
if err != nil { handleError(err)
log.Fatal(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() defer srcFile.Close()
fhint := &openpgp.FileHints{} fhint := &openpgp.FileHints{}
fhint.IsBinary = true fhint.IsBinary = true
tmpWriter, err := openpgp.Encrypt(distFile, keyList, nil, fhint, nil) tmpWriter, err := openpgp.Encrypt(distFile, keyList, nil, fhint, nil)
if err != nil { handleError(err)
log.Fatal(err)
}
defer tmpWriter.Close() defer tmpWriter.Close()
_, err = io.Copy(tmpWriter, srcFile) _, err = io.Copy(tmpWriter, srcFile)
if err != nil { handleError(err)
log.Fatal(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 // ReadKeyFile - read key from file
func ReadKeyFile(r io.Reader) (openpgp.EntityList, error) { func ReadKeyFile(r io.Reader) (openpgp.EntityList, error) {
keys, err := openpgp.ReadArmoredKeyRing(r) keys, err := openpgp.ReadArmoredKeyRing(r)
if err != nil { if err != nil || len(keys) == 0 {
keys, err = openpgp.ReadKeyRing(r) keys, err = openpgp.ReadKeyRing(r)
if err != nil { if err != nil {
return nil, err return nil, err

View File

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