-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeEncrypt.py
More file actions
72 lines (58 loc) · 2.23 KB
/
codeEncrypt.py
File metadata and controls
72 lines (58 loc) · 2.23 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
import sys
import base64
from codeUtilities import *
usage = "Uso: {0} {1}".format(sys.argv[0], '[archivo]')
class codeEncrypt(object):
def __init__(self, data, key):
self.data = data
self.key = key
# Encrypt a block of 32 bytes and encode it
def encryptBlock(self, block):
return base64.b64encode(codeUtilities.operate(block,self.key))
def encryptText(self):
message = "\nMessage: "
byteArray = bytearray()
byteArray.extend(self.data.encode('utf-8'))
arrayBlock = []
for w in byteArray:
if len(arrayBlock) < 32:
arrayBlock += [w]
else:
message += self.encryptBlock(arrayBlock).decode("utf-8")
arrayBlock = [w]
while len(arrayBlock) < 32:
arrayBlock += [0]
message += self.encryptBlock(arrayBlock).decode("utf-8")
print(message)
def encryptFile(self):
ext = self.data.split(".")[len(self.data.split("."))-1]
inputFile = open(self.data,"rb")
finalFile = open("{0}.{1}".format("crypt",ext),"wb")
arrayBlock = []
# Read file in binary in blocks of 32 bytes
for l in inputFile:
for w in l:
if len(arrayBlock) < 32:
arrayBlock += [w]
else:
# Write the encrypted block in base64
finalFile.write( self.encryptBlock(arrayBlock) )
arrayBlock = [w]
# Add padding if needed and write the last block
while len(arrayBlock) < 32:
arrayBlock += [0]
finalFile.write( self.encryptBlock(arrayBlock) )
inputFile.close()
finalFile.close()
if __name__ == "__main__":
if len(sys.argv) > 1:
print ("\n\t---- Welcome to CodeCrypt 1.1 ----")
e = codeEncrypt(sys.argv[1], codeUtilities.generateKey(sys.argv[2]))
print ("\nProceeding to encrypt...")
if (len(sys.argv) > 3 and sys.argv[3].lower() == "file"):
e.encryptFile()
else:
e.encryptText()
print ("\nSuccess")
else:
print (usage)