当前位置: 首页>>代码示例>>Python>>正文


Python Text.clean_string方法代码示例

本文整理汇总了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)
开发者ID:Hackndo,项目名称:misc,代码行数:43,代码来源:VigenereCipher.py


注:本文中的Text.Text.clean_string方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。