forked from not-pyroman/decenc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecenc.py
executable file
·133 lines (106 loc) · 4.29 KB
/
decenc.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
#!/bin/env python3
from __future__ import print_function,division
import sys
if sys.version_info < (3, 0):
range = xrange
import argparse
import hn_encryption as hn
def read_input(fname:str) -> str:
if fname == '-':
return sys.stdin.read()
else:
with open(fname) as f:
return f.read()
# Modes
def decypher(args):
cipher = read_input(args.infile)
if args.brute:
dec,plain = hn.decrypt_brute(cipher, args.layers, args.verbose, args.OS)
else:
dec,plain = hn.decrypt_with_pass(cipher, args.password.encode('utf-8'), args.layers, args.verbose, args.OS)
if args.outfile == '-':
sys.stdout.write(plain.decode(encoding='utf-8', errors='replace'))
else:
outfile = args.outfile+dec.extension
with open(outfile, 'wb') as f:
f.write(plain)
def dechead(args):
cipher = read_input(args.infile)
hn.decrypt_header_only(cipher, args.OS)
def encypher(args):
plain = read_input(args.infile)
i = args.infile.rfind('.')
extension = args.infile[i:] if i != -1 else ''
cipher = hn.encrypt_with_pass(args.comment, args.signature, args.OS, extension,
plain, args.password.encode('utf-8'))
if args.outfile == '-':
sys.stdout.write(cipher)
else:
outfile = args.outfile
with open(outfile, 'w') as f:
f.write(cipher)
# Input parsing
parser = argparse.ArgumentParser()
parser.description = """
Decodes the DEC_ENC format used in Hacknet.
"""
parser.add_argument('-v',
action="store_true", dest='verbose',
help="verbose mode")
parser.set_defaults(mode=lambda x:parser.print_usage())
subparsers = parser.add_subparsers(
title='modes')
#description='valid modes'
# Decypher
dec_parser = subparsers.add_parser('decypher', help='decryption mode')
dec_parser.add_argument('infile',
nargs='?', default='-',
help="input file")
dec_parser.add_argument('password',
nargs='?', default='',
help="password to use, defaults to no password")
dec_parser.add_argument('-o',
metavar='outfile', default='-', dest='outfile',
help="output file base name")
dec_parser.add_argument('--brute',
action="store_true",
help="brute force solve the file, with verbose also prints a valid password")
dec_parser.add_argument('--layers', '-n',
default=1, type=int, metavar='n',
help="layers of decryption")
dec_parser.add_argument('--OS', '-O',
default=sys.platform,
help="force a specific hashing algorithm, defaults to current OS (windows,linux)")
dec_parser.set_defaults(mode=decypher)
# Dechead
head_parser = subparsers.add_parser('dechead', help='header decryption mode')
head_parser.add_argument('infile',
nargs='?', default='-',
help="input file")
head_parser.add_argument('--OS', '-O',
default=sys.platform,
help="force a specific hashing algorithm, defaults to current OS (windows,linux)")
head_parser.set_defaults(mode=dechead)
# Encypher
enc_parser = subparsers.add_parser('encypher', help='encryption mode')
enc_parser.add_argument('infile',
nargs='?', default='-',
help="input file")
enc_parser.add_argument('password',
nargs='?', default='',
help="password to use, defaults to no password")
enc_parser.add_argument('-o',
metavar='outfile', default='-', dest='outfile',
help="output file")
enc_parser.add_argument('--comment', '-c',
default='',
help="header comment")
enc_parser.add_argument('--signature', '-s',
default='',
help="header signature")
enc_parser.add_argument('--OS', '-O',
default=sys.platform,
help="force a specific hashing algorithm, defaults to current OS (windows,linux)")
enc_parser.set_defaults(mode=encypher)
args = parser.parse_args()
args.mode(args)