Skip to content

Commit

Permalink
Automatically ask for pass if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
pp-johan committed Feb 5, 2020
1 parent 9d8cf7f commit ee1a7ff
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
9 changes: 1 addition & 8 deletions cmd/decrypt/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type decryption struct {
fileToDecrypt string
privateKey string
secretKey string
askPass bool
}

// Decrypt allows decryption of symmetric key using private key
Expand Down Expand Up @@ -52,12 +51,6 @@ func Decrypt() *cobra.Command {
"secret.key.enc",
"secret key to decrypt",
)
cmd.Flags().BoolVar(
&decrypt.askPass,
"askPass",
false,
"ask for password for private key",
)

return cmd
}
Expand All @@ -68,7 +61,7 @@ func (e *decryption) run() {
secretKey, _ := ioutil.ReadFile(e.secretKey)
fileToDecrypt, _ := ioutil.ReadFile(e.fileToDecrypt)

unecryptedSecret := dec.DecryptUsingPrivateKey(secretKey, privateKey, e.askPass)
unecryptedSecret := dec.DecryptUsingPrivateKey(secretKey, privateKey)

clearText := dec.DecryptUsingAsymmetricKey(fileToDecrypt, unecryptedSecret)

Expand Down
11 changes: 6 additions & 5 deletions dec/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dec

import (
"fmt"
"strings"

"crypto/aes"
"crypto/cipher"
Expand All @@ -15,18 +16,18 @@ import (
)

// DecryptUsingPrivateKey decrypt using private key
func DecryptUsingPrivateKey(toDecrypt, pKey []byte, askPass bool) []byte {
func DecryptUsingPrivateKey(toDecrypt, pKey []byte) []byte {
var privateKey *rsa.PrivateKey
if askPass {
if strings.Contains(string(pKey), "OPENSSH") {
pk, _ := ssh.ParseRawPrivateKey(pKey)
privateKey = pk.(*rsa.PrivateKey)
} else {
pkPassword := getPkPassword()

privateKeyPem, _ := pem.Decode(pKey)
decPrivateKey, _ := x509.DecryptPEMBlock(privateKeyPem, []byte(pkPassword))

privateKey, _ = x509.ParsePKCS1PrivateKey(decPrivateKey)
} else {
pk, _ := ssh.ParseRawPrivateKey(pKey)
privateKey = pk.(*rsa.PrivateKey)
}
unecryptedSecret, _ := rsa.DecryptPKCS1v15(rand.Reader, privateKey, toDecrypt)

Expand Down

0 comments on commit ee1a7ff

Please sign in to comment.