Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def text_to_binary(text):
- return ''.join(format(ord(c), '08b') for c in text)
- def split_into_blocks(binary_str, block_size=4):
- return [binary_str[i:i+block_size] for i in range(0, len(binary_str), block_size)]
- def xor_block(block, key):
- return format(int(block, 2) ^ int(key, 2), '04b')
- def binary_to_decimal(binary_list):
- return [int(b, 2) for b in binary_list]
- def ecb_xor_encrypt(plain_text, key='1010'):
- binary_text = text_to_binary(plain_text)
- blocks = split_into_blocks(binary_text, 4)
- if len(blocks[-1]) < 4:
- blocks[-1] = blocks[-1].ljust(4, '0')
- encrypted_blocks = [xor_block(block, key) for block in blocks]
- encrypted_decimal = binary_to_decimal(encrypted_blocks)
- return encrypted_decimal
- # Plaintext and key
- plaintext = "HadiSasmito20230100121"
- key_4bit = "1010"
- # Encrypt
- encrypted_result = ecb_xor_encrypt(plaintext, key_4bit)
- # Output
- print("Plaintext:", plaintext)
- print("Kunci (4-bit):", key_4bit, "(decimal:", int(key_4bit, 2), ")")
- print("Hasil Enkripsi (Decimal per 4-bit blok):")
- print(encrypted_result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement