Sridhar Ratnakumar
Sridhar Ratnakumar

Reputation: 85462

Decrypt VIM encrypted file in Python

In my Python web app, I would need to decrypt a file that was encrypted using VIM. Assuming the web app knows the password used to encrypt the file in VIM, how do I write code to decrypt ?

Upvotes: 3

Views: 2356

Answers (3)

BaiJiFeiLong
BaiJiFeiLong

Reputation: 4655

The python3 version:

from pathlib import Path

from zipfile import _ZipDecrypter

print(_ZipDecrypter(b"<password>")(Path(r"<path>").read_bytes()[12:]).decode("utf8"))

Upvotes: 0

Willem Hengeveld
Willem Hengeveld

Reputation: 2776

I wrote a tool to do exactly that, also supporting the more recent encryption methods:

https://github.com/nlitsme/vimdecrypt

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799200

Turns out that vim uses the same encryption as PKZIP:

from zipfile import _ZipDecrypter

fp = open(somefile, 'rb')
zd = _ZipDecrypter(somekey)

fp.read(12)
print ''.join(zd(c) for c in fp.read())

fp.close()

Upvotes: 7

Related Questions