Created
November 12, 2025 04:18
-
-
Save yehezkieldio/b2b32eb764eab5297cbaf4f03412ba5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Transposition Cipher with given key and inverse key | |
| def group_text(text, size=6, pad_char='X'): | |
| """Group text into fixed-size blocks, padding the last block if needed.""" | |
| text = text.replace(" ", "").upper() | |
| groups = [text[i:i + size] for i in range(0, len(text), size)] | |
| if len(groups[-1]) < size: | |
| groups[-1] = groups[-1].ljust(size, pad_char) | |
| return groups | |
| def transpose(group, key): | |
| """Apply transposition according to key order.""" | |
| return ''.join(group[k - 1] for k in key) | |
| def print_mapping(group, key): | |
| """Print the visual mapping between original and transposed letters.""" | |
| header = ' '.join(str(i) for i in range(1, len(group) + 1)) | |
| print(f"\nOriginal: {group}") | |
| print(f"Index: {header}") | |
| mapped = transpose(group, key) | |
| print(f"Key Map: {' '.join(str(k) for k in key)}") | |
| print(f"Result: {mapped}") | |
| return mapped | |
| def encrypt(plaintext, key): | |
| groups = group_text(plaintext) | |
| print("Encryption process:") | |
| ciphertext = ''.join(print_mapping(g, key) for g in groups) | |
| return ciphertext | |
| def decrypt(ciphertext, inverse_key): | |
| groups = group_text(ciphertext) | |
| print("\nDecryption process:") | |
| plaintext = ''.join(print_mapping(g, inverse_key) for g in groups) | |
| return plaintext | |
| # Given keys | |
| key = [3, 5, 1, 6, 4, 2] | |
| inverse_key = [3, 6, 1, 5, 2, 4] | |
| plaintext = "Pendidikan Gratis Diperlukan Untuk Cerdas" | |
| ciphertext = encrypt(plaintext, key) | |
| print("\nCiphertext:", ciphertext) | |
| decrypted = decrypt(ciphertext, inverse_key) | |
| print("\nDecrypted Text:", decrypted) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment