Advertisement
Guest User

encrypt script

a guest
Nov 20th, 2024
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.03 KB | Cybersecurity | 0 0
  1. import os
  2. from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
  3. from cryptography.hazmat.primitives import hashes
  4.  
  5. # Constants
  6. ROUND_COUNT = 14  # For AES-256
  7. KEY_SIZE = 32     # 32 bytes for AES-256
  8. BLOCK_SIZE = 16   # AES block size in bytes
  9.  
  10. # Full AES S-Box
  11. S_BOX = [
  12.     0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5,
  13.     0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
  14.     0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0,
  15.     0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
  16.     0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC,
  17.     0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
  18.     0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A,
  19.     0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
  20.     0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0,
  21.     0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
  22.     0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B,
  23.     0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
  24.     0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85,
  25.     0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
  26.     0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5,
  27.     0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
  28.     0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17,
  29.     0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
  30.     0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88,
  31.     0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
  32.     0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C,
  33.     0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
  34.     0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9,
  35.     0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
  36.     0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6,
  37.     0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
  38.     0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E,
  39.     0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
  40.     0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94,
  41.     0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
  42.     0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68,
  43.     0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
  44. ]
  45.  
  46. # AES Rcon
  47. RCON = [
  48.     0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  49.     0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A
  50. ]
  51.  
  52. def generate_aes_key(password: bytes, salt: bytes = None, iterations: int = 1000000):
  53.     if salt is None:
  54.         salt = os.urandom(16)  # 16-byte salt
  55.     kdf = PBKDF2HMAC(
  56.         algorithm=hashes.SHA512(),
  57.         length=KEY_SIZE,
  58.         salt=salt,
  59.         iterations=iterations,
  60.     )
  61.     key = kdf.derive(password)
  62.     return key, salt
  63.  
  64. def sub_word(word):
  65.     return [S_BOX[b] for b in word]
  66.  
  67. def rot_word(word):
  68.     return word[1:] + word[:1]
  69.  
  70. def xor_words(word1, word2):
  71.     return [a ^ b for a, b in zip(word1, word2)]
  72.  
  73. def key_expansion(key):
  74.     key_symbols = [b for b in key]
  75.     key_schedule = []
  76.     n_k = KEY_SIZE // 4  # Number of 32-bit words in the key
  77.     n_r = ROUND_COUNT    # Number of rounds
  78.  
  79.     # Initialize the first n_k words of the key schedule with the cipher key
  80.     for i in range(n_k):
  81.         key_schedule.append(key_symbols[4*i : 4*(i+1)])
  82.  
  83.     # Generate the rest of the key schedule
  84.     for i in range(n_k, 4*(n_r+1)):
  85.         temp = key_schedule[i - 1][:]
  86.         if i % n_k == 0:
  87.             temp = xor_words(sub_word(rot_word(temp)), [RCON[(i//n_k)-1], 0, 0, 0])
  88.         elif n_k > 6 and i % n_k == 4:
  89.             temp = sub_word(temp)
  90.         key_schedule.append(xor_words(key_schedule[i - n_k], temp))
  91.  
  92.     # Convert key schedule into a list of round keys
  93.     round_keys = [key_schedule[4*i : 4*(i+1)] for i in range(n_r+1)]
  94.     return round_keys
  95.  
  96. def add_round_key(state, round_key):
  97.     return [[state[row][col] ^ round_key[row][col] for col in range(4)] for row in range(4)]
  98.  
  99. def sub_bytes(state):
  100.     return [[S_BOX[byte] for byte in row] for row in state]
  101.  
  102. def shift_rows(state):
  103.     shifted_state = []
  104.     for r in range(4):
  105.         shifted_state.append(state[r][r:] + state[r][:r])
  106.     return shifted_state
  107.  
  108. def mix_columns(state):
  109.     def xtime(a):
  110.         return (((a << 1) ^ 0x1B) & 0xFF) if (a & 0x80) else (a << 1)
  111.  
  112.     def mix_single_column(a):
  113.         t = a[0] ^ a[1] ^ a[2] ^ a[3]
  114.         u = a[0]
  115.         a[0] ^= t ^ xtime(a[0] ^ a[1])
  116.         a[1] ^= t ^ xtime(a[1] ^ a[2])
  117.         a[2] ^= t ^ xtime(a[2] ^ a[3])
  118.         a[3] ^= t ^ xtime(a[3] ^ u)
  119.         return a
  120.  
  121.     state_columns = [list(col) for col in zip(*state)]
  122.     for i in range(4):
  123.         state_columns[i] = mix_single_column(state_columns[i])
  124.     mixed_state = [list(row) for row in zip(*state_columns)]
  125.     return mixed_state
  126.  
  127. def aes_encrypt_block(plaintext_block, round_keys):
  128.     state = [list(plaintext_block[i:i+4]) for i in range(0, 16, 4)]
  129.  
  130.     # Initial Round
  131.     state = add_round_key(state, round_keys[0])
  132.  
  133.     # Main Rounds
  134.     for round_num in range(1, ROUND_COUNT):
  135.         state = sub_bytes(state)
  136.         state = shift_rows(state)
  137.         state = mix_columns(state)
  138.         state = add_round_key(state, round_keys[round_num])
  139.  
  140.     # Final Round
  141.     state = sub_bytes(state)
  142.     state = shift_rows(state)
  143.     state = add_round_key(state, round_keys[ROUND_COUNT])
  144.  
  145.     # Flatten the state to get the ciphertext block
  146.     ciphertext_block = [state[row][col] for col in range(4) for row in range(4)]
  147.     return bytes(ciphertext_block)
  148.  
  149. def pad_data(data):
  150.     padding_len = BLOCK_SIZE - (len(data) % BLOCK_SIZE)
  151.     padding = bytes([padding_len] * padding_len)
  152.     return data + padding
  153.  
  154. def generate_and_print_keys(password: bytes, iterations: int = 1000000):
  155.     for i in range(1, 101):  # Generate 100 keys
  156.         try:
  157.             generated_key, used_salt = generate_aes_key(password, iterations=iterations)
  158.             round_keys = key_expansion(generated_key)
  159.             # For demonstration, the AES functions are implemented but not used here
  160.             hex_key = generated_key.hex().upper()
  161.             print(f"Key {i}:\nGenerated 256-bit key (hexadecimal):\n{hex_key}\n")
  162.         except ValueError as ve:
  163.             print(ve)
  164.     input("Press Enter to exit...")
  165.  
  166. if __name__ == "__main__":
  167.     user_password = input("Enter password: ").encode()
  168.     generate_and_print_keys(user_password)
Tags: aes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement