-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.py
More file actions
251 lines (188 loc) · 6.26 KB
/
practice.py
File metadata and controls
251 lines (188 loc) · 6.26 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import struct
import sys
#FUNCTIONS######################################################################
def tobits(s):
result = []
for c in s:
bits = bin(ord(c))[2:]
bits = '00000000'[len(bits):] + bits
result.extend([int(b) for b in bits])
return result
def frombits(bits):
chars = []
for b in range(len(bits) // 8):
byte = bits[b*8:(b+1)*8]
chars.append(chr(int(''.join([str(bit) for bit in byte]), 2)))
return ''.join(chars)
def bitfield(n):
return [int(digit) for digit in bin(n)[2:]] # [2:] to chop off the "0b" part
def readLittleEndian4Byte(bytes):
sum = 0
list(bytes).reverse()
for i in range(4):
sum+=(int(bytes[i]) << (8*i))
return sum
def twosComplementFix(int):
if(int < 0):
int = 255 - (abs(int) - 1)
return int
def bitsToBytes(bits):
bytesOut = []
for i in range(0, len(bits)//8):
intByte = 0
buffer = bits[i*8:8+i*8] #read next 8 bits
#buffer.reverse() #little endian
j = 0
for b in buffer:
intByte += int(b)<<j
j += 1
bytesOut.append(intByte)
return bytesOut
#CODE###########################################################################
#Open fileSizeHEx
wav_filename = r"Silent.wav"
with open(wav_filename, 'rb') as f_wav:
wav = f_wav.read()
entry = wav[:]
entries = struct.unpack("{}b".format(len(entry)), entry) #unpack bytes
hexentries = list(map("{:02X}".format, entries)) #format the input into hex
###fix negative values: signed to unsigned
entries = list(map(twosComplementFix,entries))
#print(entries[:10])
#entries is in ints BYTES
# cipher text bit now
cipherText = input("Enter your cipher text: \n")
cipherTextBits = tobits(cipherText)
#
#print("cipher text bits: ",cipherTextBits," \n")
##create header (length) and repeat 4 times for payload
length = len(cipherTextBits) #####length in bits
print(length)
header = bitfield(length)
print(header)
if(len(header)<8):
diff = (8 - len(header))*[0]
diff.extend(header)
header = diff
print("Header: ",header)
header.reverse()
#print("header: ",header," \n")
##############################TEMPORARY##############################
# CANT HANDLE SIZES OVER 255
if(len(header) > 8):
print("ERROR: Message length exceeded. \n")
sys.exit(-1)
payload = []
for i in range(0,len(cipherTextBits)):
temp = [cipherTextBits[i]] * 4
#print("TEMP: ",temp," \n")
for x in temp:
payload.append(x)
#print("Payload: ",payload," \n")
#print(len(payload))
header.extend(payload)
finalPayload = header
#print("Final Payload: ",finalPayload," \n\n\n")
##FINAL PAYLOAD format
# bits 0-7 = size of ciphertext in tobits
#bits 8-... = ciphertext as bits repeated 4 times so amount of bits after size header is 4*size
#############
#now need to read in the wav file, read in header and inject my data in
#hexentries= bytes
wavHeader = entries[:44]
#fileSizeInt = ''.join(wavHeader[4:8])
#int.from_bytes(wavHeader[4:8], byteorder='little')####
#print(wavHeader,"\n")
fileSizeInt = readLittleEndian4Byte(wavHeader[4:8]) #read file size from wavHeader
finalPayloadLengthBytes = len(finalPayload) // 8
print("Payload Length Bytes: ",finalPayloadLengthBytes)
if(finalPayloadLengthBytes > fileSizeInt - 44):
print("Size mismatch of payload and file. Adjust either payload or file. \n")
sys.exit(-1)
#insert payload now
#how many keybits per byte? try 4 to start with
#create list of new sample data
newSampleData = []
exitIndex = 0
#for i in range(0,finalPayloadLengthBytes):
# #read 44+i*4:48+i*4 in as bytes
# #put 0+i*8:4+i*8 of final payload into byte 1
# #put 4+i*8:8+i*8 of final payload into byte3
# #reconstruct bytes 1-4
# #add to new sample data
#fixed for now
for i in range(0,int(finalPayloadLengthBytes)):
buffer = entries[44+i*2:46+i*2] #read in next 2 bytes
bufferBits0 = bitfield(buffer[0])
bufferBits1 = bitfield(buffer[1])
#extend to 8 bits###################
if(len(bufferBits0)<8):
diff = (8 - len(bufferBits0))*[0]
bufferBits0.extend(diff)
if(len(bufferBits1)<8):
diff = (8 - len(bufferBits1))*[0]
bufferBits1.extend(diff)
#####################################
bufferBits = bufferBits0
bufferBits.extend(bufferBits1)
print("Buffer bits: ",bufferBits)
newByte = finalPayload[i*8:8+i*8]
newByte.extend(bufferBits[8:16])
newPair = newByte
newSampleData.extend(newPair)
print("New Pair: ",newPair)
exitIndex = int(44+finalPayloadLengthBytes) #where the wav returns to normal
#now need to reconstruct
#convert new sample data to BYTES
newSampleDataBytes = bitsToBytes(newSampleData)
#print("WAV header: ",wavHeader)
print("Payload Length: ",len(newSampleDataBytes))
cipherFile = wavHeader
cipherFile.extend(newSampleDataBytes)
cipherFile.extend(entries[exitIndex:])
cipherFile = bytes(cipherFile)
wav_filename = r"SilentCipher.wav"
with open(wav_filename, 'wb') as f_wav:
f_wav.write(cipherFile)
#works!
#######DECODER###############
wav_filename = r"SilentCipher.wav"
with open(wav_filename, 'rb') as f_wav:
wav = f_wav.read()
entryDecode = wav[:]
entriesDecode = struct.unpack("{}b".format(len(entryDecode)), entryDecode) #unpack bytes
###fix negative values: signed to unsigned
entriesDecode = list(map(twosComplementFix,entriesDecode))
print(entriesDecode[:50])
#print("Entries decoded: ",entriesDecode[:44])
##skip first 44
#read first byte to get Size
decodeSize = entriesDecode[44]
print("Decode Size: ",decodeSize+1) #including size byte
print(entriesDecode[44:50])
#print("FILE SIZE", len(entriesDecode))
#read the next size*2 bytes
#checking lower 4 bits for value
#append value to results list
#convert list to ascii
#join list a sstring and print "voiLa"
messageBits = []
#every other byte
for i in range(0,decodeSize*2,2):
#print(entriesDecode[46+i])
bufferBits = bitfield(entriesDecode[46+i])
if(len(bufferBits)<8):
bufferBits = [0]*(8-len(bufferBits)) + bufferBits
#print(bufferBits)
if(bufferBits[4] == 1):
messageBits.append(1)
else:
messageBits.append(0)
if(bufferBits[0] == 1):
messageBits.append(1)
else:
messageBits.append(0)
print(messageBits)
print(frombits(messageBits))
#######
#BUGS: LENGTH OVER 1 BYTE