Advertisement
keselyoleren

Untitled

Jun 21st, 2025
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. def text_to_binary(text):
  2.     return ''.join(format(ord(c), '08b') for c in text)
  3.  
  4. def split_into_blocks(binary_str, block_size=4):
  5.     return [binary_str[i:i+block_size] for i in range(0, len(binary_str), block_size)]
  6.  
  7. def xor_block(block, key):
  8.     return format(int(block, 2) ^ int(key, 2), '04b')
  9.  
  10. def binary_to_decimal(binary_list):
  11.     return [int(b, 2) for b in binary_list]
  12.  
  13. def ecb_xor_encrypt(plain_text, key='1010'):
  14.     binary_text = text_to_binary(plain_text)
  15.     blocks = split_into_blocks(binary_text, 4)
  16.  
  17.     if len(blocks[-1]) < 4:
  18.         blocks[-1] = blocks[-1].ljust(4, '0')
  19.  
  20.     encrypted_blocks = [xor_block(block, key) for block in blocks]
  21.     encrypted_decimal = binary_to_decimal(encrypted_blocks)
  22.  
  23.     return encrypted_decimal
  24.  
  25. # Plaintext and key
  26. plaintext = "HadiSasmito20230100121"
  27. key_4bit = "1010"
  28.  
  29. # Encrypt
  30. encrypted_result = ecb_xor_encrypt(plaintext, key_4bit)
  31.  
  32. # Output
  33. print("Plaintext:", plaintext)
  34. print("Kunci (4-bit):", key_4bit, "(decimal:", int(key_4bit, 2), ")")
  35. print("Hasil Enkripsi (Decimal per 4-bit blok):")
  36. print(encrypted_result)
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement