-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathver.py
executable file
·152 lines (132 loc) · 5.65 KB
/
ver.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
import os
import random
import click
def encryptFile(input_file, key, output):
print('encrypting', input_file, end='...')
with open(input_file, 'rb') as inf, open(key, 'wb') as keyf, open(output, 'wb') as outf:
byte_in = inf.read(1)
while byte_in:
new_key = random.randint(0, 255)
keyf.write(bytes([new_key]))
new_byte = bytes([ord(byte_in) ^ new_key])
outf.write(new_byte)
byte_in = inf.read(1)
print('done')
def decryptFile(input_file, key, output_file):
print('decrypting', input_file, end='...')
with open(input_file, 'rb') as inf, open(key, 'rb') as keyf, open(output_file, 'wb') as outf:
byte_in = inf.read(1)
while byte_in:
new_key = keyf.read(1)
new_byte = bytes([ord(new_key) ^ ord(byte_in)])
outf.write(new_byte)
byte_in = inf.read(1)
print('done')
@click.group()
def process():
"""
Vernam's algorithm. Version v1.0
Encrypting and decrypting files via Vernam`s algorithm.
Using XOR operation with each bytes.
After encryption, you will receive an encrypted (*.dec)
and a key (*.dec.key) files. The key is secret information.
For decryption, you must give the decrypted and key files."""
pass
@click.command()
@click.option("-s", "--seed", type=click.INT, help="Seed of random")
@click.option("-i", "--input", "fileinput", type=click.Path(exists=True, file_okay=True, dir_okay=True),
help="Source of input files. File or folder")
@click.option("-o", "--output", type=click.Path(file_okay=True, dir_okay=True), help="Destination of output file. File "
"or folder (if input is folder - "
"folder only!) ")
@click.option("-k", "--key", type=click.Path(file_okay=True, dir_okay=True),
help="Destination of key files. File or folder (if input is folder - folder only!)")
def encrypt(seed, fileinput, output, key):
"""
Encrypt files
"""
random.seed(seed)
if os.path.isfile(fileinput): # and not folders: # work with one file
if not output:
output = fileinput + '.dec'
elif os.path.isdir(output):
output = os.path.join(output, os.path.basename(fileinput) + '.dec')
if not key:
key = output + '.key'
elif os.path.isdir(key):
key = os.path.join(key, os.path.basename(output) + '.key')
try:
encryptFile(fileinput, key, output)
except FileNotFoundError as e:
print('error!')
print(e, 'skipping..')
else:
if not key:
key = fileinput
if not output:
output = fileinput
if not os.path.isfile(key) and not os.path.isfile(output): # decrypt many files
if not os.path.isdir(key):
os.mkdir(key)
if not os.path.isdir(output):
os.mkdir(output)
for file in os.listdir(fileinput):
if os.path.isfile(os.path.join(fileinput, file)):
try:
encryptFile(os.path.join(fileinput, file), os.path.join(key, file + '.dec.key'),
os.path.join(output, file + '.dec'))
except FileNotFoundError as e:
print('error!')
print(e, 'skipping..')
else:
print(file, 'is dir - SKIP')
@click.command()
@click.option("-i", "--input", "fileinput", type=click.Path(exists=True, file_okay=True), required=True,
help="Source of input encrypted filed. File of folder")
@click.option("-k", "--key", type=click.Path(exists=True, file_okay=True, dir_okay=True),
help="Source of keys. File or folder (if input is folder - folder only!)")
@click.option("-o", "--output", type=click.Path(dir_okay=True, file_okay=True),
help="Destination of output decrypted files. File of folder (if input is folder - folder only!)")
def decrypt(fileinput, key, output):
"""
Decrypt files
"""
if os.path.isfile(fileinput):
if not key:
key = fileinput + '.key'
elif os.path.isdir(key):
key = os.path.join(key, fileinput + '.key')
if not output:
output = fileinput.split('.dec')[0]
elif os.path.isdir(output):
output = os.path.join(output, fileinput.split('.dec')[0])
try:
decryptFile(fileinput, key, output)
except FileNotFoundError as e:
print('error!')
print(e, 'skipping..')
else:
if not key:
key = fileinput
if not output:
output = fileinput
if not os.path.exists(output):
os.mkdir(output)
if not os.path.isfile(key) and not os.path.isfile(output): # decrypt many files
files = os.listdir(fileinput)
for file in files:
if os.path.isfile(os.path.join(fileinput, file)):
key_file = os.path.join(key, file + '.key')
output_file = os.path.join(output, file.split('.dec')[0])
try:
decryptFile(os.path.join(fileinput, file), key_file, output_file)
except FileNotFoundError as e:
print('error!')
print(e, 'skipping..')
else:
print(file, 'is dir - SKIP')
process.add_command(encrypt)
process.add_command(decrypt)
if __name__ == "__main__":
process()