-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.py
122 lines (86 loc) · 2.96 KB
/
crypto.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
import sys
import os.path
FILE_POS_DATA_LEN = 128
FILE_POS_DATA_START = 138
def loadText(textPath):
with open(textPath, 'r') as textFile:
data = textFile.read()
return bytearray(data, 'utf-8')
def loadImage(imagePath):
with open(imagePath, 'rb') as imageFile:
data = imageFile.read()
return bytearray(data)
def setDataLen(data, dataLen):
resDataLen = bytearray(str(dataLen), 'utf-8')
deltaZero = (FILE_POS_DATA_START - FILE_POS_DATA_LEN) - resDataLen.__len__()
indexDataLen = FILE_POS_DATA_LEN
while deltaZero:
data[indexDataLen] = ord('0')
indexDataLen = indexDataLen + 1
deltaZero = deltaZero - 1
indexResDataLen = 0
while indexDataLen < FILE_POS_DATA_START:
data[indexDataLen] = resDataLen[indexResDataLen]
indexDataLen = indexDataLen + 1
indexResDataLen = indexResDataLen + 1
return data
def getDataLen(data):
resDataLen = data[FILE_POS_DATA_LEN:FILE_POS_DATA_START]
result = ''
for ch in resDataLen:
result = result + chr(ch)
return int(result)
def encrypt(imagePath, textPath):
dataTxt = loadText(textPath)
dataTxtLen = dataTxt.__len__()
dataImg = loadImage(imagePath)
dataImgLen = dataImg.__len__()
indexImg = FILE_POS_DATA_START
indexTxt = 0
setDataLen(dataImg, dataTxtLen)
while indexImg < dataImgLen and indexTxt < dataTxtLen:
dataImg[indexImg] = dataTxt[indexTxt]
indexImg = indexImg + 3
indexTxt = indexTxt + 1
with open('imgresult.bmp', 'wb') as imgResult:
imgResult.write(dataImg)
def decrypt(imagePath):
dataImg = loadImage(imagePath)
dataTxtLen = getDataLen(dataImg)
resultTxt = []
indexTxt = FILE_POS_DATA_START
indexCh = 0
while indexCh < dataTxtLen:
resultTxt.append(dataImg[indexTxt])
indexTxt = indexTxt + 3
indexCh = indexCh + 1
with open('txtresult.txt', 'w') as txtResult:
txtResult.write(str(bytes(resultTxt)))
def fileExists(filePath):
return os.path.exists(filePath)
argvHelp = """Cryptographer text in picture v1.0
USAGE:
encryption:\tcoder -e <image.bmp> <text.txt>
decryption:\tcoder -d <image.bmp>
help:\t\tcoder -h
Supported File formats:\t TXT, BMP
"""
def main():
if sys.argv.__len__() < 3 or (sys.argv.__len__() == 2 and sys.argv[2] == '-h'):
print(argvHelp)
elif sys.argv.__len__() == 3 and sys.argv[1] == '-d':
if fileExists(sys.argv[2]):
decrypt(sys.argv[2])
print('OK!')
else:
print('File ' + sys.argv[2] + ' does not exists')
elif sys.argv.__len__() == 4 and sys.argv[1] == '-e':
if fileExists(sys.argv[2]) and fileExists(sys.argv[3]):
encrypt(sys.argv[2], sys.argv[3])
print('OK!')
else:
print('One of the files ' + sys.argv[2] + ' or ' + sys.argv[3] + ' does not exists')
else:
print('Wrong format command\n', argvHelp)
if __name__ == '__main__':
main()