jamessan / vim-gnupg

This script implements transparent editing of gpg encrypted files.
http://www.vim.org/scripts/script.php?script_id=3645
728 stars 73 forks source link

"File is not encrypted ..." error in Cygwin with Gpg4win #62

Closed kenny-evitt closed 7 years ago

kenny-evitt commented 8 years ago

I suspect the problem is that the gpg (and gpg2) commands installed by Gpg4win expect (require) Windows-style paths.

I followed the steps outlined in this comment:

  1. I opened Vim by itself.
  2. I ran :let g:GPGDebugLevel=3 and then :let g:GPGDebugLog="debug.log".
  3. I opened an encrypted file.

The contents of the debug.log file:

GnuPG: >>>>>>>> Entering s:GPGInit(1)
GnuPG: gnupg.vim 2.6
GnuPG: shellredirsave: >%s 2>&1
GnuPG: shellsave: /bin/bash
GnuPG: shelltempsave: 1
GnuPG: shell: /bin/sh
GnuPG: shellcmdflag: -c
GnuPG: shellxquote:
GnuPG: shellredir: >%s 2>&1
GnuPG: stderrredirnull: 2>/dev/null
GnuPG: shell implementation: /bin/sh
GnuPG: command: gpg --trust-model always --version 2>/dev/null
GnuPG: rc: 0
GnuPG: output: gpg (GnuPG) 2.0.30 (Gpg4win 2.3.2)^M^@libgcrypt 1.6.5^M^@Copyright (C) 2015 Free Software Foundation, Inc.^M^@License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>^M^@This is free software: you are free to change and redistribute it.^M^@There is NO WARRANTY, to the extent permitted by law.^M^@^M^@Home: C:/Users/kevitt/AppData/Roaming/gnupg^M^@Supported algorithms:^M^@Pubkey: RSA, RSA, RSA, ELG, DSA^M^@Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,^M^@        CAMELLIA128, CAMELLIA192, CAMELLIA256^M^@Hash: MD5, SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224^M^@Compression: Uncompressed, ZIP, ZLIB, BZIP2^M^@
GnuPG: public key algorithms: RSA, RSA, RSA, ELG, DSA^M
GnuPG: cipher algorithms: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,^M
GnuPG: hashing algorithms: MD5, SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224^M
GnuPG: compression algorithms: Uncompressed, ZIP, ZLIB, BZIP2^M
GnuPG: <<<<<<<< Leaving s:GPGInit()
GnuPG: >>>>>>>> Entering s:GPGDecrypt(1)
GnuPG: command: gpg --trust-model always --no-use-agent --verbose --decrypt --list-only --dry-run --no-use-agent --logger-fd 1 '/home/kevitt/pass-neck-restore-test/home/kevitt/.password-store/neck/TeamCity administrator.gpg' 2>/dev/null
GnuPG: rc: 2
GnuPG: output: gpg: can't open `/home/kevitt/pass-neck-restore-test/home/kevitt/.password-store/neck/TeamCity administrator.gpg': Invalid argument^M^@gpg: decrypt_message failed: Invalid argument^M^@
GnuPG: this file is not encrypted
GnuPG: <<<<<<<< Leaving s:GPGDecrypt()
GnuPG: >>>>>>>> Entering s:GPGCleanup()
GnuPG: <<<<<<<< Leaving s:GPGCleanup()

A Bash script (for managing passwords) that also uses gpg and targets Cygwin uses a function to handle Windows paths:

...

# replaces Cygwin-style filenames with their Windows counterparts
gpg_winpath() {
    local args=("$@")
    # as soon as an argument (from back to front) is no file, it can only be a filename argument if it is preceeded by '-o'
    local could_be_filenames="true"
    local i
    for ((i=${#args[@]}-1; i>=0; i--)); do
        if ( [ $i -gt 0 ] && [ "${args[$i-1]}" = "-o" ] && [ "${args[$i]}" != "-" ] ); then
            args[$i]="$(cygpath -am "${args[$i]}")"
        elif [ $could_be_filenames = "true" ]; then
            if [ -e "${args[$i]}" ]; then
                args[$i]="$(cygpath -am "${args[$i]}")"
            else
                could_be_filenames="false"
            fi
        fi
    done
    $GPG_ORIG "${args[@]}"
}

if $GPG --help | grep -q 'Home: [A-Z]:[/\\]'; then
    GPG_ORIG="$GPG"
    GPG=gpg_winpath
fi

The key to the function is that Cygwin supplies a builtin command cygpath for converting Unix-style paths to Windows-style paths.

kenny-evitt commented 8 years ago

The plugin works fine with gVim opened via a Windows Explorer or Windows Command Prompt window.

jamessan commented 7 years ago

What's the difference in :echo has('win32unix') in the config where it works and where it doesn't? Hopefully, :echo has('win32unix') returns 1 in the case where you're having the problem.

kenny-evitt commented 7 years ago

@jamessan Sorry for not replying to your comment six months ago! And thanks for fixing it!