本文整理汇总了Python中Text.Text.clean_string方法的典型用法代码示例。如果您正苦于以下问题:Python Text.clean_string方法的具体用法?Python Text.clean_string怎么用?Python Text.clean_string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Text.Text
的用法示例。
在下文中一共展示了Text.clean_string方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __guess_key
# 需要导入模块: from Text import Text [as 别名]
# 或者: from Text.Text import clean_string [as 别名]
def __guess_key(self, min_len=1, max_len=9, display=False):
keylen = self.guess_key_length(min_len, max_len, display)
if keylen == 0:
print "[!] No key length found."
return -1
if display:
print "[*] Most probable key length : " + str(keylen) + "\n"
freq_fr = {'e': 14.715, 's': 7.948, 'a': 7.636, 'i': 7.529, 't': 7.244, 'n': 7.095, 'r': 6.553, 'u': 6.311, 'l': 5.456, 'o': 5.378, 'd': 3.669, 'c': 3.260, 'p': 3.021, 'm': 2.968, 'v': 1.628, 'q': 1.362, 'f': 1.066, 'b': 0.901, 'g': 0.866, 'h': 0.737, 'j': 0.545, 'x': 0.387, 'y': 0.308, 'z': 0.136, 'w': 0.114, 'k': 0.049}
password = ""
for i in range(keylen):
sub_alphabet = VigenereCipher(''.join([self._s[keylen*j + i] for j in range(self._len//keylen)]))
min_differential = 99999
password_letter = ""
for c in range(65, 65+26):
sub_alphabet.key = chr(c)
decrypted = VigenereCipher(sub_alphabet.decipher().tostring())
sub_alphabet.encipher()
freq_s = { k:round((v/decrypted.len)*100, 3) for k,v in dict(decrypted.get_frequencies()).items()}
differential = sum([abs(freq_fr[k.lower()]-v) for k,v in freq_s.items()])
if differential < min_differential:
min_differential = differential
password_letter = chr(c)
password += password_letter
# Little hack for repetitive password due to frequency analysis
for i in range(1, len(password)):
if len(password) % i == 0:
duplicate = True
s = [password[j*i:(j+1)*i] for j in range(len(password)//i)]
ex_prec = s[0]
for ex in s:
if ex != ex_prec:
duplicate = False
break
ex_prec = ex
if duplicate:
password = ex
if display:
print "[*] [UPDATE] Most probable key length : " + str(len(password)) + "\n"
break
return Text.clean_string(password)