-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyCipher_stream_encrypter.py
More file actions
156 lines (120 loc) · 4.7 KB
/
KeyCipher_stream_encrypter.py
File metadata and controls
156 lines (120 loc) · 4.7 KB
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
#!/usr/bin/python
#import pyperclip
import sys,string,os
import random as rd
import base64 as bs
STRING = 3
KEY = 2
MODE = 1
KEYMODE = 4
# Omitting some special chars that have a trouble with powershell
special_chars = ['\'','\"'] # Additional special chars ['(',')','{','}','$','`','&','%','#']
punctuation = string.punctuation
for char in special_chars:
punctuation = punctuation.replace(char, '')
# Alphanumerals [a-z:A-Z:0-9:all_special_chars]
LETTERS = string.letters+string.digits+punctuation
def encryptDecryptString(mode,key,str,keyMode):
strKey = key.__str__()
if len(strKey) > 2:
if keyMode == '-m':
encStrings = []
for num in key:
i = 0
key = int(num)
keyMode = '-n'
encstr = encryptDecryptString(mode,key,str,keyMode)
str = encryptDecryptString(mode,key,encstr,keyMode)
encStrings.append(str)
if encStrings[len(encStrings) - 1]:
print "{}".format(encStrings[len(encStrings) - 1])
return (encStrings[len(encStrings) - 1])
exit()
else:
try:
enstr = encryptDecryptString(mode,key,str,keyMode)
print "{}".format(enstr)
except RuntimeError as e:
print "[!] Encountered : {}\n[*] Avoid using single line ('-n') encryption with key of > 2 digits".format(e)
exit()
key = int(key)
trans = ''
for byte in str:
if byte in LETTERS:
num = LETTERS.find(byte)
if mode == 'encrypt':
num += key
elif mode == 'decrypt':
num -= key
if num >= len(LETTERS):
num -= len(LETTERS)
elif num < 0:
num = num + len(LETTERS)
try:
trans += LETTERS[num]
except IndexError:
print "[!]Unable to encrypt key size to large"
exit()
else:
trans += byte
if mode == "encrypt":
return trans
if mode == "decrypt":
return trans
def encodedecodeFile(mode,key,file,keyMode):
with open(file) as f:
filename = raw_input("\n[*]Enter path to save the (encrypted\decrypted) file : ")
if '~/' in filename:
filename = filename.replace('~/','')
elif '\\' in filename:
filename = filename.replace('\\',"\\")
print "\n{}:".format(filename.upper())
print "{}".format(('='*len(filename)))
for line in f.readlines():
if filename.endswith('.m4a') and keyMode == 'encrypt':
line = bs.b64encode(line)
elif filename.endswith('.m4a') and keyMode == 'decrypt':
line = bs.b64decode(line)
encline = encryptDecryptString(mode,key,line,keyMode)
with open(filename,'a') as encf:
encf.write(encline)
def KeyString(key):
key = key.__str__()
keylist = []
for char in key:
if char in LETTERS:
posKey = LETTERS.find(char)
if posKey <= 26:
CipherKey = posKey % 26
else:
CipherKey = posKey % 26
keylist.append(CipherKey.__str__())
else:
print "[!] Key String Entered cannot be used as a Cipher Key\n"
exit()
keys = ""
return keys.join(keylist)
def usage():
print "\nUsage: KeyCipher [OPTIONS] [<int> | <string> KEY] [STRING] -m\n"
print "OPTIONS\t\tDEFINATION\n"
print "=======\t\t==========\n"
print "--encrypt\tMode for encryption\n--decrypt\tMode for decryption\n--help\t\tDisplay help message\n--keyMode\tMultiple key encryption <'-m'>\n"
if __name__ == "__main__":
if len(sys.argv) != 5:
usage()
exit()
if sys.argv[MODE] == '--encrypt':
try:
if os.path.exists(sys.argv[STRING]) == True:
encodedecodeFile("encrypt",KeyString(sys.argv[KEY]),sys.argv[STRING],sys.argv[KEYMODE])
encryptDecryptString("encrypt",KeyString(sys.argv[KEY]),sys.argv[STRING],sys.argv[KEYMODE])
except ValueError as e:
print "[!] Encountered :{}\n[*] Please Use a key of type <int>".format(e)
exit()
elif sys.argv[MODE] == '--decrypt':
if os.path.exists(sys.argv[STRING]) == True:
encodedecodeFile("decrypt",KeyString(sys.argv[KEY]),sys.argv[STRING],sys.argv[KEYMODE])
encryptDecryptString("decrypt",KeyString(sys.argv[KEY]),sys.argv[STRING],sys.argv[KEYMODE])
else:
usage()
exit()