-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptshell.py
More file actions
142 lines (121 loc) · 3.85 KB
/
cryptshell.py
File metadata and controls
142 lines (121 loc) · 3.85 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
#!/usr/bin/python
import sys, getopt, itertools, getpass, os, shutil
# -h,--help : lists commands
# -f, --files : decrypts and displays files
# -d <file_name>, --decrypt <file_name>: decrypts and outputs file
# -s <file_path>, --save <file_path>: saves file and deletes it
# -c , --changepassword: changes password
# -i , --init: initializes system
# -r <file_name>, --remove <file_name> : removes encrypted file
#string used for validation, change to whatever you want
VAL_STRING = "10cc4a10-e51d-4775-bd84-91998b377325"
path = "./.files/"
def main(argv):
try:
opts, args = getopt.getopt(argv,"hfd:s:cir:",["help","files","decrypt=","save=","changepassword","init","remove="])
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit(2)
if len(opts) == 0:sys.exit()
[o,a] = opts[0]
#validates password
if o not in ("-h","-i","--help","--init"):
PASS = getpass.getpass("Password: ")
if PassEval(PASS):
cypher = CreateCypher(PASS)
else:
print "Invalid password."
sys.exit()
if o in ("-h","--help"):
print " -h,--help :\n lists commands\n"
print " -f, --files :\n decrypts and displays files\n"
print " -d <file_id>, --decrypt <file_id>:\n decrypts and outputs file\n"
print " -s <file_path>, --save <file_path>:\n saves file and deletes non-encrypted file\n"
print " -r <file_name>, --remove <file_name>:\n removes the encrypted file\n"
print " -c , --changepassword:\n changes password\n"
print " -i , --init:\n initializes system\n"
sys.exit()
elif o in("-f","--files"):
dirFiles = os.listdir(path)
if len(dirFiles) > 0:
print "\n".join([decrypt(i,cypher) for i in dirFiles])
elif o in("-d","--decrypt"):
files = os.listdir(path)
fileName = encrypt(a,cypher)
if fileName in files:
WriteToFile(
open(path+encrypt(a,cypher)),
open(a,"w"),
decrypt,
cypher)
else:
print "File does not exist"
sys.exit()
elif o in("-s","--save"):
WriteToFile(
open(a),
open((path + encrypt(a.split("/")[-1].strip("."),cypher)),"w"),
encrypt,
cypher)
os.unlink(a)
elif o in("-r","--remove"):
os.unlink(path + encrypt(a,cypher))
elif o in("-c","--changepassword"):
NEWPASS = getpass.getpass("New password: ")
newcypher = CreateCypher(NEWPASS)
for i in os.listdir(path):
ChangeCypher(i,cypher,newcypher)
open(".val.txt","w").write(encrypt(VAL_STRING,newcypher))
elif o in("-i","--init"):
cypher = CreateCypher(getpass.getpass("Password: "))
open(".val.txt","w").write(encrypt(VAL_STRING,cypher))
if os.path.exists('./.files'): shutil.rmtree(path)
os.mkdir(path)
else:
assert False, "unhandled option"
def ChangeCypher(filename, cypher_old, cypher_new):
fin = open(path + filename)
print encrypt(decrypt(filename,cypher_old),cypher_new)
fout = open(path + encrypt(decrypt(filename,cypher_old),cypher_new),"w")
while True:
c = fin.read(1)
if not c: break
fout.write(encrypt(decrypt(c,cypher_old),cypher_new))
fin.close()
fout.close()
os.unlink(path + filename)
def WriteToFile(fin, fout, func, cypher):
while True:
c = fin.read(1)
if not c: break
fout.write(func(c,cypher))
fin.close()
fout.close()
def CreateCypher(PASS):
dir=[i for i in xrange(256)]
nextChr = itertools.cycle(list(PASS))
for j in xrange(256):
c = nextChr.next()
tempIndex = (j+ord(c))%256
#swap
dir[i] += dir[tempIndex]
dir[tempIndex] = dir[i] - dir[tempIndex]
dir[i] -= dir[tempIndex]
return dir
def PassEval(PASS):
cypher = CreateCypher(PASS)
fin = open(".val.txt")
valstring = ''
while True:
c = fin.read(1)
if not c: break
valstring += decrypt(c,cypher)
if valstring == VAL_STRING: return True
else: return False
def decrypt(string,cypher):
return "".join(chr(cypher.index(ord(i))) for i in list(string))
def encrypt(string, cypher):
return "".join(chr(cypher[ord(i)]) for i in list(string))
if __name__ == "__main__":
main(sys.argv[1:])