Advertisement
Guest User

infosec_exc5_stijn_and_jonathan

a guest
Sep 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.06 KB | None | 0 0
  1. import hashlib
  2. import argparse
  3.  
  4. def xor_list(char_list, key_int_list):
  5.     output = [chr(ord(char) ^ key_int) for char, key_int in zip(char_list, key_int_list)]
  6.     return output
  7.  
  8. def split_by_period(str_or_list_in, period):
  9.     splitsection_indices = range(0, len(str_or_list_in), period)
  10.     output = [str_or_list_in[start: start + period] for start in splitsection_indices]
  11.     return output
  12.  
  13. class FeistelCipher:
  14.     def __init__(self, unencrypted_text = "", password_in = "", encrypted_text = ""):
  15.         def get_double_sha_key_list(password):
  16.             def get_sha256(string_in):
  17.                 hash_object = hashlib.sha256(string_in.encode())
  18.                 return str(hash_object.hexdigest())
  19.            
  20.             first_hash = get_sha256(password)
  21.             second_hash = get_sha256(first_hash)
  22.             all_key_ints = split_by_period(first_hash+second_hash, period = 2)
  23.             # interpret as hexadecimals and convert to integer
  24.             all_key_hexs = [int(str("0x"+all_key_int),0) for all_key_int in all_key_ints]
  25.             # keylenghts = (1/2)*blocklength. 8-byte blocks ==> key_length = 4
  26.             key_list = split_by_period(all_key_hexs, period = 4)
  27.             return key_list
  28.         self.key_list = get_double_sha_key_list(password_in)
  29.         self.encrypted = encrypted_text
  30.         self.unencrypted = unencrypted_text
  31.         self.decrypted = []
  32.  
  33.     def block_splitting_routine(self, word, keys, round_function):
  34.         L = [word[0:int(len(word)/2)]]
  35.         R = [word[int(len(word)/2):len(word)]]
  36.         for key in keys:
  37.             L_new, R_new = round_function(R[-1], L[-1], key)
  38.             L.append(L_new)
  39.             R.append(R_new)
  40.         return list(L[-1] + R[-1])      
  41.      
  42.     def encrypt(self):
  43.         def next_round(R_in, L_in, key):
  44.             R_out = L_in
  45.             L_out = xor_list(R_in, key)
  46.             return (L_out, R_out)
  47.         encrypted_blocklist = []
  48.         for unencrypted_block in split_by_period(self.unencrypted,8):
  49.             encrypted_blocklist.append(self.block_splitting_routine(unencrypted_block,
  50.                                                                     self.key_list,
  51.                                                                     next_round))
  52.         self.encrypted = "".join(["".join(_) for _ in encrypted_blocklist])
  53.         print(self.encrypted)
  54.         return self.encrypted
  55.  
  56.     def decrypt(self):
  57.         def previous_round(R_in, L_in, key):
  58.             L_out = R_in
  59.             R_out = xor_list(L_in, key)
  60.             return (L_out, R_out)
  61.         decrypted_blocklist = []
  62.         for encrypted_block in split_by_period(self.encrypted,8):
  63.             decrypted_blocklist.append(self.block_splitting_routine(encrypted_block,
  64.                                                                     reversed(self.key_list),
  65.                                                                     previous_round))
  66.         self.decrypted = "".join(["".join(_) for _ in decrypted_blocklist])
  67.         print(self.decrypted)
  68.         return self.decrypted
  69.  
  70. def arg_handler():
  71.     parser = argparse.ArgumentParser()
  72.     parser.add_argument('password', type=str,
  73.                         help='password used for key-generation')
  74.     parser.add_argument('text', type=str,
  75.                         help='insert text to be de- or encrypted.')
  76.     parser.add_argument("-e", "--encrypt", action='store_true',
  77.                         help='encrypt text')
  78.     parser.add_argument("-d", "--decrypt", action='store_true',
  79.                         help='decrypt text')
  80.     args = parser.parse_args()  
  81.  
  82.     text = str(args.text)
  83.     password = str(args.password)
  84.     return text, password
  85.    
  86.    
  87. if __name__ == "__main__":
  88.     text, password = arg_handler()
  89.     unencrypted, encrypted = "",""
  90.     CipherObject = FeistelCipher(password_in = password)
  91.     if args.encrypt:
  92.         CipherObject.unencrypted = text
  93.         CipherObject.encrypt()
  94.     if args.decrypt:
  95.         CipherObject.encrypted = text
  96.         CipherObject.decrypt()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement