''' Embeeding code ''' import math # Read Secret bits from file file1 = open('Secrets.txt', 'r') SecretCount = 0 Secret = [] while True: S = file1.read(1) if S != "": SecretCount += 1 Secret.append(int(S)) else: break #Add a redundant bit if number of secret bits is odd if SecretCount % 2 == 1: Secret.append(0) file1.close() #Find the number of characters in the cover text including spaces file2 = open('Cover.txt', 'r') CoverLength = file2.read() file2.close() #Find the number of characters in the cover text excluding spaces and remove white spaces file2 = open('Cover.txt', 'r') file3 = open('NewCover.txt', 'w') i = 0 CharacterCount = 0 SpaceCount = 0 while i < len(CoverLength): Char = file2.read(1) CharHex = (''.join([f'\\u{ord(Char):0>4x}'])) if ((CharHex == '\\u061c') or (CharHex == '\\u200f') or (CharHex == '\\u200c') or (CharHex == '\\u034f')): None else: file3.write(Char) if Char == ' ': SpaceCount += 1 else: CharacterCount += 1 i += 1 file3.close() file2.close() file2 = open('NewCover.txt', 'a') file2.write(' ') file2.close() print('# Characters = ', CharacterCount, ', # Spaces = ', SpaceCount, ', # Secret bits =', SecretCount) print('# Characters before hiding = ', CharacterCount + SpaceCount) if len(Secret) > CharacterCount*2: print('Secret bits cannot be embedded in the given cover text') print('Required number of Characters:', int(len(Secret)/2)) else: file2 = open('NewCover.txt', 'r') CharCount = 0 CoverRead = 0 i = 0 T = [] T2 = [] Text = [] D = 0 C = 0 DC = [] j = 0 #Find the XOR of the secret bits with the cover texts while i < len(Secret): Text.append(file2.read(1)) CoverRead += 1 T.append(''.join([f'\\u{ord(Text[j]):0>4x}'])) while (Text[j] != " ") and (i < len(Secret)): CharCount += 1 if Text[j] in ["ب" ,"ت" ,"ث","ج","خ","ذ","ز", "ش","ض","ظ","غ", "ف","ق", "ن","ي","ة"]: D = 1 # The letter is dotted else: D = 0 # The letter is undotted if Text[j] in ["ء" ,"أ" ,"ا" ,"آ","إ","د","ذ","ر", "ز","و","ؤ","ى","ة"]: C = 0 # The letter is disconnected else: C = 1 # The letter is connected DC.append(D ^ Secret[i]) DC.append(C ^ Secret[i+1]) j += 1 i +=2 Text.append(file2.read(1)) CoverRead += 1 T.append(''.join([f'\\u{ord(Text[j]):0>4x}'])) j += 1 i = 0 j = 0 CoverLimit = len(T) T_New = [] CharCount = 0 Embedded = 0 #Embed pseudo spaces to represent secret bits while i < len(DC): while j < CoverLimit: if (T[j] == '\\u0020'): Count = 0 while Count < CharCount: if i < len(Secret): if DC[i] == 0: if DC[i+1] == 0: T_New.append('\\u061C') else: T_New.append('\\u200F') else: if DC[i+1] == 0: T_New.append('\\u200C') else: T_New.append('\\u034F ') Count += 1 Embedded += 1 i += 2 else: break T_New.append('\\u0020') CharCount = 0 else: T_New.append(T[j]) CharCount += 1 j += 1 i += 2 T_len_before = len(T) #if there are still some secret bits not embedded if Embedded < len(DC)/2: k = len(Text)-1 while (Text[k] != ' '): #read more characters tuntil the end of a word (a space) Text.append(file2.read(1)) CoverRead += 1 k += 1 k = len(T) while k < len(Text): T.append(''.join([f'\\u{ord(Text[k]):0>4x}'])) k += 1 T_len_after = len(T) k1 = T_len_before k2 = T_len_after - 1 # until before the last read space while k1 < k2: T_New.append(T[k1]) k1 += 1 #Embed the remaining secret bits i = 2*Embedded j = Embedded CoverLimit = len(T) while j < CoverLimit: if i < len(DC): if DC[i] == 0: if DC[i+1] == 0: T_New.append('\\u061C') else: T_New.append('\\u200F') else: if DC[i+1] == 0: T_New.append('\\u200C') else: T_New.append('\\u034F ') Embedded += 1 i += 2 else: break j += 1 #indicate the end of secret bits if SecretCount % 2 == 1: T_New.append('\\uFE00') # number of secret bits is odd else: T_New.append('\\uFE01') # number of secret bits is even k1 = len(T) - 1 T_New.append(T[k1]) # Embed the last space ''' # Compute the capacity (method 1) StegoSize = len(T_New) * 16 print('# characters after hiding = ', len(T_New), ', StegoSize 2 = ', StegoSize) print(T_New) Capacity1 = (SecretCount / StegoSize) * 100 print("Capacity 1 = ", Capacity1) ''' #convert the unicode to characters Character = [] Count = 0 while Count < len(T_New): T2.append(T_New[Count].replace('\\u','0x')) Char = chr(int(T2[Count], base = 16)) Character.append(Char) Count += 1 file3 = open('Stego.txt', 'w') # Write the characters to the output file i=0 while i < len(Character): file3.write(Character[i]) i += 1 # Write the remaining characters of the cover text to the output file while CoverRead < len(CoverLength): file3.write(file2.read(1)) CoverRead += 1 file3.close() file2.close()