I like to use OpenSSL to encrypt some files on my hard drive.  I cobbled
together the following script that allows me to "transparently" view and
updated OpenSSL-encrypted docs using vim on Linux:

    augroup encrypted
        au!

        " First make sure nothing is written to ~/.viminfo while editing
        " an encrypted file.
        autocmd BufReadPre,FileReadPre          *.des3 set viminfo=
        " We don't want a swap file, as it write unencrypted data to disk
        autocmd BufReadPre,FileReadPre          *.des3 set noswapfile
        " Switch to binary mode to read the encrypted file
        autocmd BufReadPre,FileReadPre          *.des3 set bin
        autocmd BufReadPre,FileReadPre          *.des3 let ch_save =
&ch|set ch=2
        autocmd BufReadPre,FileReadPre          *.des3 let shsave=&sh
        autocmd BufReadPre,FileReadPre          *.des3 let &sh='sh'
        autocmd BufReadPre,FileReadPre          *.des3 let ch_save =
&ch|set ch=2
        autocmd BufReadPost,FileReadPost        *.des3 '[,']!openssl enc
-d -des3 2> /dev/null
        autocmd BufReadPost,FileReadPost        *.des3 let &sh=shsave

        " Switch to normal mode for editing
        autocmd BufReadPost,FileReadPost        *.des3 set nobin
        autocmd BufReadPost,FileReadPost        *.des3 let &ch =
ch_save|unlet ch_save
        autocmd BufReadPost,FileReadPost        *.des3 execute ":doautocmd
BufReadPost " . expand("%:r")

        " Convert all text to encrypted text before writing
        autocmd BufWritePre,FileWritePre        *.des3 set bin
        autocmd BufWritePre,FileWritePre        *.des3 let shsave=&sh
        autocmd BufWritePre,FileWritePre        *.des3 let &sh='sh'
        autocmd BufWritePre,FileWritePre        *.des3 '[,']!openssl enc
-e -des3 -salt 2>/dev/null
        autocmd BufWritePre,FileWritePre        *.des3 let &sh=shsave

        " Undo the encryption so we are back in the normal text, directly
after the
        " file has been written.
        autocmd BufWritePost,FileWritePost      *.des3 silent u
        autocmd BufWritePost,FileWritePost      *.des3 set nobin
    augroup END


Basically, when I try to open a file with a "des3" file extension, this
chunk of code is executed.

To get this to work on Windows, I changed the "openssl" lines to the
following:

    autocmd BufReadPost,FileReadPost        *.des3
'[,']!C:\OpenSSL\bin\openssl.exe enc -d -des3 2> /dev/null
    ...
    autocmd BufWritePre,FileWritePre        *.des3
'[,']!C:\OpenSSL\bin\openssl.exe enc -e -des3 -salt 2>/dev/null

Also, I commented out the "let &sh='sh'" lines, since I get the
following value when I execute the ":echo &sh" command:

    C:\WINDOWS\system32\cmd.exe

However, when I try to open an encrypted file using this function, I get
the following error:

    c:> gvim .\secret.txt.des3
    ".\secret.txt.des3" [noeol][unix] 10L, 1304C
    shell returned 1
    10 lines filtered
    Press ENTER or type command to continue
    ...
    'sh' is not recognized as an internal or external command,
    operable program or batch file.

The weird part is that I commented out all of the lines that explicitly
reference 'sh'.

What am I doing wrong?  Is there anyone else who's usuing gpg or openssl
with Vim?

Thanks in advance!

Tom Purl


Reply via email to