如何在Python旋转密码暴力破解工具中保留特殊字符与空格?
Fixing Rotational Cipher Crack Tool to Preserve Non-Alphabet Characters
Got it, let's sort this out for you! The problem with your current code is that it attempts to rotate every single character in the input—including spaces, punctuation, and other special symbols—instead of only targeting alphabetic characters. Here's how to adjust it so non-letters stay completely untouched:
Modified Code
#!/usr/bin/env python3 import sys alpha_upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" alpha_lower = "abcdefghijklmnopqrstuvwxyz" cipher_text = sys.argv[1] def crack_rotation(): for key in range(len(alpha_lower)): plain_text = '' for character in cipher_text: # Skip rotation for non-alphabet characters: just add them as-is if not character.isalpha(): plain_text += character continue # Original rotation logic only runs for letters if character.islower(): index = alpha_lower.find(character) index = (index - key) % len(alpha_lower) plain_text += alpha_lower[index] else: index = alpha_upper.find(character) index = (index - key) % len(alpha_upper) plain_text += alpha_upper[index] print('Trying with key %s. Result = %s' % (key, plain_text)) crack_rotation()
What Changed?
- Added a check using
character.isalpha(): this tells us if the current character is an uppercase or lowercase letter. - If the character isn't a letter, we immediately add it to
plain_textand usecontinueto skip the rest of the rotation logic for that character. - For actual letters, we keep your original rotation logic intact—so uppercase and lowercase letters still get shifted correctly based on the key.
This way, spaces, commas, exclamation points, numbers, and any other non-alphabet characters will appear exactly as they did in the input cipher text in every decrypted result.
内容的提问来源于stack exchange,提问作者t50hg
相关产品推荐
相关产品推荐

