/
RSATools.py
229 lines (183 loc) · 7.36 KB
/
RSATools.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#
# Code By M4rK0v
#
from Crypto.Hash import MD5, SHA, SHA224, SHA256, SHA384, SHA512
import codecs
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Util import number
import base64
import urllib.parse
from argparse import ArgumentParser
VERSION = '1.0'
HASH_ASN1 = {
'MD5': b'\x30\x20\x30\x0c\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x05\x05\x00\x04\x10',
'SHA-1': b'\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14',
'SHA-224': b'\x30\x2d\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04\x05\x00\x04\x1c',
'SHA-256': b'\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20',
'SHA-384': b'\x30\x41\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02\x05\x00\x04\x30',
'SHA-512': b'\x30\x51\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03\x05\x00\x04\x40',
}
def UrlDecode(msg):
return urllib.parse.unquote(msg)
def UrlEncode(msg):
return urllib.parse.quote(msg)
def removePadding(msg):
if msg[0] == 0:
msg = msg[1:]
if msg[0] == 1:
msg = msg[1:]
while True:
if msg[0] == 255:
msg = msg[1:]
else:
break
if msg[0] == 0:
msg = msg[1:]
for (hashname, asn1code) in HASH_ASN1.items():
if asn1code in msg:
msg = msg[(len(asn1code)):]
return hashname, msg
return -1, msg
else:
return -1, -1
def loadKey(filePath):
keyFile = open(filePath, 'rb')
key = RSA.importKey(keyFile.read())
keyFile.close()
return key
def main():
cliParser = ArgumentParser(description="RSA encryption Tool")
subparsers = cliParser.add_subparsers(help='sub-command help', dest='command')
# Add subparser
parserEncrypt = subparsers.add_parser('encrypt', help='RSA encrypt')
parserDecrypt = subparsers.add_parser('decrypt', help='RSA Decrypt')
parserSign = subparsers.add_parser('sign', help='RSA Sign')
parserVerify = subparsers.add_parser('verify', help='RSA Verify')
# add arg to Encrypt
parserEncrypt.add_argument('-pub', '--public', help='Public Key', required=True)
parserEncrypt.add_argument('-i', '--input', help="Input Clear text", required=True)
# add arg to Decrypt
parserDecrypt.add_argument('-pri', '--private', help='Private Key', required=True)
parserDecrypt.add_argument('-i', '--input', help="Input Cipher text", required=True)
# add arg to Sign
parserSign.add_argument('-pri', '--private', help='Private Key', required=True)
parserSign.add_argument('-b', '--base64', help="Base64 Decode", action='store_true')
parserSign.add_argument('-u', '--url', help="URL Decode", action='store_true')
parserSign.add_argument('-hh', '--hash', help="hash function {md5, sha-1, sha-224, sha-256, sha-384, sha-512}", required=True)
parserSign.add_argument('-i', '--input', help="Input Clear text", required=True)
# add arg to Verify
parserVerify.add_argument('-pub', '--public', help='Public Key', required=True)
parserVerify.add_argument('-b', '--base64', help="Base64 Decode", action='store_true')
parserVerify.add_argument('-u', '--url', help="URL Decode", action='store_true')
parserVerify.add_argument('-i', '--input', help="Input Signature text", required=True)
cliParser.add_argument('-v', '--version', action='version', version='RSA Tool {} - M4rK0v'.format(VERSION), help='Version of this tool')
args = cliParser.parse_args()
# Load input
try:
inputFile = open(args.input, 'rb')
except (FileExistsError, FileNotFoundError) as e:
print(e)
return -1
input = inputFile.read()
input = input.strip()
# check to do encrypt
if args.command == 'encrypt':
try:
key = loadKey(args.public)
except (FileNotFoundError, FileExistsError) as e:
print("[!] Key file not found.")
return 0
except (ValueError, TypeError, IndexError) as e:
print("[!] Key has improper format.")
return 0
pubKey = PKCS1_v1_5.new(key)
cipher = key.encrypt(input, 32)[0]
print("[+] Cipher Text:")
print(base64.b64encode(cipher))
# check to
elif args.command == 'decrypt':
try:
key = loadKey(args.private)
except (FileNotFoundError, FileExistsError) as e:
print("[!] Key file not found.")
return 0
except (ValueError, TypeError, IndexError) as e:
print("[!] Key has improper format.")
return 0
cipher = base64.b64decode(input)
clearText = key.decrypt(cipher)
print("[+] Clear Text:")
print(clearText)
elif args.command == 'sign':
try:
key = loadKey(args.private)
except (FileNotFoundError, FileExistsError) as e:
print("[!] Key file not found.")
return 0
except (ValueError, TypeError, IndexError) as e:
print("[!] Key has improper format.")
return 0
keySign = PKCS1_v1_5.new(key)
if args.hash:
if args.hash.lower() == 'md5':
digest = MD5.new()
elif args.hash.lower() == 'sha-1':
digest = SHA.new()
elif args.hash.lower() == 'sha-224':
digest = SHA224.new()
elif args.hash.lower() == 'sha-256':
digest = SHA256.new()
elif args.hash.lower() == 'sha-384':
digest = SHA384.new()
elif args.hash.lower() == 'SHA-512':
digest = SHA512.new()
else:
print("[!] Hash algorithm not found.")
return 0
digest.update(input)
sign = keySign.sign(digest)
print("[+] Hex Formart:")
print(codecs.getencoder('hex')(sign))
# check if encode base64
if args.base64:
signBase64 = base64.b64encode(sign)
print("[+] Base64 format:")
print(signBase64)
# check if encode url
if args.url:
signURL = UrlEncode(signBase64)
print("[+] URL encoded:")
print(signURL)
else:
return -1
elif args.command == 'verify':
try:
key = loadKey(args.public)
except (FileNotFoundError, FileExistsError) as e:
print("[!] Key file not found.")
return 0
except (ValueError, TypeError, IndexError) as e:
print("[!] Key has improper format.")
return 0
# Check to decode URL
if args.url:
input = UrlDecode(input.decode())
# Check to decode Base64
if args.base64:
input = base64.b64decode(input)
# Convert Input Bytes to Long
sigLong = number.bytes_to_long(input)
clearSigLong = pow(sigLong, key.e, key.n)
# Convert clear Sign Long to Bytes
clearSigByte = number.long_to_bytes(clearSigLong)
print(codecs.getencoder('hex')(clearSigByte))
hashArg, clearSig = removePadding(clearSigByte)
if hashArg==-1 or clearSig ==-1:
print("[!] Error! in remove padding.")
else:
print("[+] Hash algorithm: {}".format(hashArg))
print("[+] Clear Signature: ")
print(codecs.getencoder('hex')(clearSig))
if __name__ == '__main__':
main()