def DecryptOFB(self, iv, msg_bin, chunk_size): self.iv = iv self.chunk_size = chunk_size self.msg_bin = msg_bin str = StringBinaryToDecimal() iv_dec = str.strbin2dec(iv) msg_list = [str.strbin2dec(msg_bin[x:x+chunk_size]) for x in xrange(0,len(msg_bin),chunk_size)] msg_block = '' block_encryption = 0 count = 0 while (count < len(msg_list)): if (count == 0): block_encryption = iv_dec ^ KEY #change encryption algorithm later msg_list[count] ^= block_encryption else: block_encryption ^= KEY msg_list[count] ^= block_encryption msg_list[count] = bin(msg_list[count])[2:].zfill(chunk_size) msg_block += msg_list[count] count += 1 return msg_block
def DecryptCBC(self, msg_bin, chunk_size): #reverse of CipherBlockChaining #self.iv = iv self.msg_list = 0 self.key = 0 decrypted_msg = '' str = StringBinaryToDecimal() #b = [a[x:x+2] for x in xrange(0,len(a),2)] msg_list = [str.strbin2dec(msg_bin[x:x+chunk_size]) for x in xrange(0,len(msg_bin),chunk_size)] #grab last element of the CBC msg_list iv = msg_list[len(msg_list)-1] msg_list.pop(len(msg_list)-1) #pop initialization vector out of the list count = len(msg_list) - 1 while (count > 0): msg_list[count] ^= KEY msg_list[count] ^= msg_list[count-1] count -= 1 msg_list[count] ^= KEY msg_list[count] ^= iv for msg_block in msg_list: y_bin = bin(int(msg_block))[2:].zfill(chunk_size) decrypted_msg += y_bin return decrypted_msg
def DecryptCFB(self, encrypt_bin_msg, chunk_size): self.encrypt_bin_msg = encrypt_bin_msg self.chunk_size = chunk_size str = StringBinaryToDecimal() decrypted_msg = '' msg_list = [str.strbin2dec(msg_bin[x:x+chunk_size]) for x in xrange(0,len(msg_bin),chunk_size)]
def EncryptECB(self, bin_msg): self.bin_msg = bin_msg str = StringBinaryToDecimal() encrypt_bin = [] for x in bin_msg: y = bin(str.strbin2dec(x) ^ KEY)[2:] #change encryption algorithm encrypt_bin.append(y) return encrypt_bin
def Bin2Msg(self, decrypt_bin_msg, chunk_size): self.decrypt_bin_msg = decrypt_bin_msg self.chunk_size = chunk_size str = StringBinaryToDecimal() output_msg = '' msg_list = [str.strbin2dec(decrypt_bin_msg[x:x+ASCII_BIN_SIZE]) for x in xrange(0,len(decrypt_bin_msg),ASCII_BIN_SIZE)] for x in msg_list: y = chr(x) output_msg += y return output_msg
def EncryptCFB(self, iv, msg_list, chunk_size): self.iv = iv self.msg_list = msg_list self.chunk_size = chunk_size str = StringBinaryToDecimal() iv_dec = str.strbin2dec(iv) msg_bin = '' count = 0 while (count < len(msg_list)): msg_list[count] = str.strbin2dec(msg_list[count]) if (count == 0): block_encryption = iv_dec ^ KEY #change encryption algorithm msg_list[count] ^= block_encryption else: block_encryption = msg_list[count-1] ^ KEY #change encryption algorithm msg_list[count] ^= block_encryption msg_bin += bin(int(msg_list[count]))[2:].zfill(chunk_size) count += 1 return msg_bin
def EncryptCTR(self, iv, msg_list, chunk_size): self.iv = iv self.chunk_size = chunk_size self.msg_list = msg_list str = StringBinaryToDecimal() iv_dec = str.strbin2dec(iv) msg_bin = '' block_encryption = 0 count = 0 while (count < len(msg_list)): msg_list[count] = str.strbin2dec(msg_list[count]) block_encryption = iv_dec ^ KEY #change encryption algorithm later msg_list[count] ^= block_encryption msg_list[count] = bin(msg_list[count])[2:].zfill(chunk_size) msg_bin += msg_list[count] iv_dec +=1 count += 1 return msg_bin
def EncryptCBC(self, iv, msg_list, chunk_size): self.iv = iv self.msg_list = msg_list self.key = 0; #start the XOR-ing and encryption of the messages: m1 with iv, m2 with c1, m3 with c2 str = StringBinaryToDecimal() iv_dec = str.strbin2dec(iv) msg_bin = '' #key = 5 count = 0 while (count < len(msg_list)): if (count == 0): #XOR the very first element in the block with the initialization vector msg_list[count] = str.strbin2dec(msg_list[count]) msg_list[count] ^= iv_dec msg_list[count] = bin(msg_list[count])[2:].zfill(chunk_size) msg_list[count] = str.strbin2dec(msg_list[count]) msg_list[count] ^= KEY #change this algorithm else: msg_list[count] = str.strbin2dec(msg_list[count]) msg_list[count] ^= msg_list[count-1] msg_list[count] = bin(msg_list[count])[2:].zfill(chunk_size) msg_list[count] = str.strbin2dec(msg_list[count]) msg_list[count] ^= KEY #change this algorithm if (msg_list[count] > ((2**chunk_size)-1)): print "assert!!!!! You should not see this message. If you do....you messed up real bad" count += 1 msg_list.append(str.strbin2dec(iv)) #append the iv to the very end of the message (only needed for client/server communication) #convert encrypted message back into binary for x_bin in msg_list: y_bin = bin(int(x_bin))[2:].zfill(chunk_size) msg_bin += y_bin return msg_bin