def server(): host = socket.gethostname() port = 5000 server_socket = socket.socket() server_socket.bind((host, port)) # bind host address and port together server_socket.listen(3) print("...............server is listening...............") conn, address = server_socket.accept() print("server connected to: " + str(address)) data = conn.recv(2048).decode() data = data.split("$$") id_client = data[0] id_tgt = data[1] time = str(datetime.datetime.now()) key = randomString() print(key) ticket = key + "$$" + id_client + "$$" + id_tgt + "$$" + time ticket = encryption(tgt_key, ticket) print(ticket) message = key + "$$" + id_tgt + "$$" + time + "$$" + str( base64.b64encode(ticket), 'utf-8') message = encryption(client_key, message) conn.send(message) conn.close()
def server(): host = socket.gethostname() port = 7000 server_socket = socket.socket() server_socket.bind((host, port)) # bind host address and port together server_socket.listen(3) print("...............server is listening...............") while True: conn, address = server_socket.accept() print("server connected to: "+ str(address)) data=conn.recv(2048).decode() data = data.split("$$") ticket = data[0] autheticator = data[1] ticket = decryption(server_key,base64.b64decode(ticket)) ticket = ticket.split("$$") key_c_v = ticket[0] time = decryption(key_c_v , base64.b64decode(autheticator)).split("$$") print(time) message = encryption(key_c_v,time[-1]) conn.send(message) conn.close()
def wrongencrypt(blockSize, plaintextBlockList, shiftRegister): key = "11001001110010011100100111001001110010011100100111001001110010011100100111001001110010011100100111001001110010011100100111001001" ciphertextBlockList = [] for j in range(0, len(plaintextBlockList)): plaintextBlock = plaintextBlockList[j] # tempRegister = string_to_ind_bit(shiftRegister) tempRegister = string_to_ind_bit(aes.encryption(shiftRegister, key)) ciphertextBlock = [] # Get the first r bits, then XOR with plaintext, and append to ciphertextblock for m in range(0, blockSize): if j == 1 and m == 1: # to change a single bit bit = tempRegister[m] ^ plaintextBlock[m] if bit == 1: bit = 0 else: bit = 1 ciphertextBlock.append(bit) else: ciphertextBlock.append(tempRegister[m] ^ plaintextBlock[m]) # Shift the bits to the left, then add temp register bits shiftRegister = string_to_ind_bit(shiftRegister) for n in range(0, blockSize): shiftRegister.pop(0) shiftRegister.append(tempRegister[n]) shiftRegister = ind_bit_to_string(shiftRegister) ciphertextBlockList.append(ciphertextBlock) return ciphertextBlockList
def connect_to_tgs(key_c_tgs, ticket): host = socket.gethostname() port = 6000 #server port client_socket = socket.socket() client_socket.connect((host, port)) print("...............connection established................") id_client = "1" id_server = "4" ts3 = datetime.datetime.now() #create authenticator auth = encryption(key_c_tgs, id_client + "$$" + str(ts3)) message = id_server + "$$" + ticket + "$$" + str(auth) message = message.encode() client_socket.send(message) data = client_socket.recv(2024) data = decryption(key_c_tgs, data) data = data.split("$$") key_c_v = data[0] print(key_c_v) ticket_v = data[-1] print(ticket_v) return key_c_v, ticket_v
def getCounter(original_Counter,count,key): encryCounter_List =[] counter = int(original_Counter,16) for i in xrange(0,count,1): counter=counter + i counter_binary=bin(counter)[2:].zfill(len(original_Counter)*4) encryCounter = aes.encryption(key,tl.h2b(tl.b2h(counter_binary))) encryCounter_List.append(encryCounter) return encryCounter_List
def encryptionCFB(key,plaintext,iv): count = len(plaintext)/64 cipher = '' for i in xrange(0,count,1): key_plain = tl.h2b(aes.encryption(key,iv)) c = tl.b2h(tl.xor(key_plain,plaintext[i*64:(i*64)+64])) cipher += c iv = tl.h2b(c) return cipher
def encryptionOFB(iv, key, plaintext): biPlain = plaintext count = len(plaintext)/64 cipher = [] for i in xrange(0,count,1): iv = tl.h2b(aes.encryption(key, iv)) biP = biPlain[i*64:(i*64)+64] cipher += tl.xor(iv, biP) cipherStr = tl.b2h(cipher) return cipherStr
def encryptionECB(key,plaintext): result = "" if (len(plaintext)%64 <> 0): sys.exit(0) # AES for x in xrange(0, (len(plaintext)/64)): cipher = aes.encryption(key, plaintext[(x*64):(x*64+64)]) result += cipher # need to transformed to ASCII before write return result
def encryptionCBC(key,plaintext,iv): count = len(plaintext)/64 cipher = '' for i in xrange(0,count,1): plain = tl.xor(plaintext[i*64:(i*64)+64],iv) c = aes.encryption(key,plain) cipher += c iv = tl.h2b(c) return cipher
def decryptionOFB(iv, key, ciphertext,padding_size): biCipher = ciphertext count = len(biCipher)/64 plain = [] for i in xrange(0,count,1): iv = tl.h2b(aes.encryption(key, iv)) biC = ciphertext[i*64:(i*64)+64] plain += tl.xor(iv, biC) plainStr = tl.b2h(plain) plainStr = tl.unPadding(plainStr,padding_size) return plainStr
def decryptionCFB(key,ciphertext,iv): hexCipher = tl.h2b(ciphertext[0]) count = len(tl.h2b(ciphertext[0]))/64 plain = '' for i in xrange(0,count,1): key_plain = tl.h2b(aes.encryption(key,iv)) p = tl.b2h(tl.xor(key_plain,hexCipher[i*64:(i*64)+64])) iv = hexCipher[i*64:(i*64)+64] plain += p plain = tl.unPadding(plain, ciphertext[1]) return plain
def encrypt(blockSize, plaintextBlockList, shiftRegister): key = "11001001110010011100100111001001110010011100100111001001110010011100100111001001110010011100100111001001110010011100100111001001" # plaintextBlock = [None] * blockSize ciphertextBlockList = [] for j in range(0, len(plaintextBlockList)): plaintextBlock = plaintextBlockList[j] # tempRegister = string_to_ind_bit(shiftRegister) tempRegister = string_to_ind_bit(aes.encryption(shiftRegister, key)) ciphertextBlock = [] # Get the first r bits, then XOR with plaintext, and append to ciphertextblock for m in range(0, blockSize): ciphertextBlock.append(tempRegister[m] ^ plaintextBlock[m]) # Shift the bits to the left, then add temp register bits shiftRegister = string_to_ind_bit(shiftRegister) for n in range(0, blockSize): shiftRegister.pop(0) shiftRegister.append(tempRegister[n]) shiftRegister = ind_bit_to_string(shiftRegister) ciphertextBlockList.append(ciphertextBlock) return ciphertextBlockList
def make_authenticator(key): id_client = "1" time = str(datetime.datetime.now()) message = id_client + "$$" + time return encryption(key, message), time