while True: print("\n[1] Encrypt text") print("[2] Decrypt text") print("[3] Brute-force decrypt (auto-solve)") print("[4] Set shift key") print("[5] Encrypt file") print("[6] Decrypt file") print("[0] Exit") choice = input("Choose: ").strip() if choice == '0': break elif choice == '1': text = input("Enter text: ") print("Encrypted:", cipher.encrypt(text)) elif choice == '2': text = input("Enter ciphertext: ") print("Decrypted:", cipher.decrypt(text)) elif choice == '3': text = input("Enter ciphertext to crack: ") results = cipher.brute_force(text) print("\nTop possible plaintexts:") for shift, plain, _ in results: print(f"Shift {shift:2}: {plain[:80]}") elif choice == '4': new_shift = int(input("Enter shift (1-25): ")) % 26 cipher = CaesarCipher(new_shift) print(f"Shift set to {cipher.shift}") elif choice == '5': infile = input("Input file: ") outfile = input("Output file: ") with open(infile, 'r', encoding='utf-8') as f: encrypted = cipher.encrypt(f.read()) with open(outfile, 'w', encoding='utf-8') as f: f.write(encrypted) print("File encrypted successfully.") elif choice == '6': infile = input("Ciphertext file: ") outfile = input("Output plaintext file: ") with open(infile, 'r', encoding='utf-8') as f: decrypted = cipher.decrypt(f.read()) with open(outfile, 'w', encoding='utf-8') as f: f.write(decrypted) print("File decrypted successfully.") else: print("Invalid choice.") if == " main ": main()
def brute_force(self, ciphertext, top_n=5): """Try all 25 shifts and return most likely results.""" candidates = [] for shift in range(1, 26): temp_cipher = CaesarCipher(shift) decrypted = temp_cipher.decrypt(ciphertext) # Score by frequency of English letters (simple heuristic) score = sum(1 for ch in decrypted.lower() if ch in 'etaoin') candidates.append((shift, decrypted, score)) candidates.sort(key=lambda x: x[2], reverse=True) return candidates[:top_n] def main(): print("=== Caesar Cipher Software ===") cipher = CaesarCipher() caesar software
def encrypt(self, text): """Encrypt text using current shift.""" return ''.join(self._shift_char(c, self.shift) for c in text) score)) candidates.sort(key=lambda x: x[2]
""" Caesar Cipher Software - Professional Implementation Features: - Encrypt / Decrypt text with custom shift - Auto-solve (brute force all 25 shifts) - Preserve case and non-alphabet characters - File input/output support """ class CaesarCipher: def (self, shift=3): self.shift = shift % 26 or a different language (JavaScript
If you’d like this for a , GUI , or a different language (JavaScript, C++, etc.), let me know and I’ll adapt it.
def _shift_char(self, char, shift): """Shift a single character, preserving case.""" if char.isupper(): return chr((ord(char) - ord('A') + shift) % 26 + ord('A')) elif char.islower(): return chr((ord(char) - ord('a') + shift) % 26 + ord('a')) else: return char # non-alphabet unchanged