-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt.py
137 lines (119 loc) · 4.25 KB
/
crypt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
import gnupg
import getopt, sys
import os
#############################################
# Define these for proper encryption!
gpg = gnupg.GPG(gnupghome=r'path/to/gnupg')
recipient = "[email protected]"
#############################################
def encrypt(folders):
#Iterate over input dirs
for i in folders:
#Walk the file tree
for root, dirs, files in os.walk(i):
#Iterate through all the files
for file in files:
#Encrypt file and write
fname = os.path.join(root,file)
with open(fname, 'rb') as f:
out_filename = fname + '.gpg'
status = gpg.encrypt_file(
f, recipients=[recipient],
output = out_filename
)
#Remove original file
os.remove(fname)
def decrypt(folders):
#Iterate over input dirs
for i in folders:
#Walk the file tree
for root, dirs, files in os.walk(i):
#Iterate through all the files
for file in files:
#Encrypt file and write
fname = os.path.join(root, file)
with open(fname, 'rb') as f:
out_filename = fname.replace('.gpg', '');
status = gpg.decrypt_file(
f, output = out_filename
)
#Remove original file
os.remove(fname)
def usage():
print(
'''
Encrypts/Decrypts files in specified root folders and all subfolders.
Usage: python crypt.py
Requires: .cryptfile - one column text file where each row is a dir to encrypt
Options:
-h | --help Show help
-e | --encrypt Encryption mode
-d | --decrypt Decryption mode
Make sure to edit the script and specify the location of your gnupg dir before
running. The script also requires that you define a recipient which is a form of
id for the key that will be used for encryption (an e-mail address should do, or
a fingerprint or a key id). Keys should already be set-up and ready on your system.
Typical gnupg locations include:
Win: C:\\Users\\<Name>\\AppData\\Roaming\\gnupg
Linux: ~/.gnupg
Recipient example: Fingerprint, Key ID, e-mail ([email protected])
'''
)
def main():
try:
optlist, args = getopt.getopt(
sys.argv[1:], 'hed', [ 'help', 'encrypt', 'decrypt' ]
)
except getopt.GetoptError as err:
print(err)
usage()
sys.exit(2)
folders = []
try:
with open(".cryptfile") as f:
for line in f:
folders.append(line.strip());
except FileNotFoundError:
print(
'''
ERROR: Cryptfile not found!
Please create a .cryptfile inside the script directory that
specifies which folders you want to encrypt/decrypt. Try again
afterwards.
An cryptfile is a blank text file where each line contains the name of a
directory you want encrypted. Specifying a directory name in the cryptfile
defines a file tree root from which the script will start the encryption
process.
!!Attention!! crypt.py will encrypt all the data in the specified file trees,
so make sure this is the behavior you want.
Example structure of cryptfile:
folder1
folder2
Make sure to edit the script and specify the location of your gnupg dir before
running. The script also requires that you define a recipient which is a form of
id for the key that will be used for encryption (an e-mail address should do, or
a fingerprint or a key id).
Typical gnupg locations include:
Win: C:\\Users\\<Name>\\AppData\\Roaming\\gnupg
Linux: ~/.gnup g
Recipient example: Fingerprint, Key ID, e-mail ([email protected])
'''
)
sys.exit(1)
for o, a in optlist:
if o in ('-h', '--help'):
usage()
sys.exit()
elif o in ('-e', '--encrypt'):
encrypt(folders)
sys.exit()
elif o in ('-d', '--decrypt'):
decrypt(folders)
sys.exit()
else:
assert False, "unrecognized option"
usage()
sys.exit(1)
if __name__ == "__main__":
main()