确定密码文本的字母频率 [英] Determining Letter Frequency Of Cipher Text

查看:174
本文介绍了确定密码文本的字母频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一种工具,找出某些类型的密文中的字母的频率。
假设它是全部小写的a-z没有数字。编码消息在txt文件中



我正在尝试构建一个脚本来帮助破解替换或可能的转置密码。



代码到目前为止:

  cipher = open('cipher.txt','U' ()
cipherfilter = cipher.lower()
cipherletters = list(cipherfilter)

alpha = list('abcdefghijklmnopqrstuvwxyz')
出现= {}
在字母中的字母:
出现[letter] = cipherfilter.count(letter)
出现的字母:
打印字母,出现[letter]

到目前为止,它显示的是一个字母出现多少次。
如何打印此文件中发现的所有字母的频率。

解决方案

  import collections 

d = collections.defaultdict(int)
for'c'中的c
d [c] + = 1

print d #defaultdict(< type'int'> {'s':1,'e':1,'t':2})

从一个文件:

  myfile = open('test.txt')
在我的文件中的行:
line = line.rstrip('\\\
')
for c in行:
d [c] + = 1

对于天才是 defaultdict 容器,我们必须感谢和赞美。否则我们都会这样做傻:

  s =andnowforsomethingcompletelydifferent
d = {}
为s的信件:
如果不在d中的字母:
d [letter] = 1
else:
d [letter] + = 1


I am trying to make a tool that finds the frequencies of letters in some type of cipher text. Lets suppose it is all lowercase a-z no numbers. The encoded message is in a txt file

I am trying to build a script to help in cracking of substitution or possibly transposition ciphers.

Code so far:

cipher = open('cipher.txt','U').read()
cipherfilter = cipher.lower()
cipherletters = list(cipherfilter)

alpha = list('abcdefghijklmnopqrstuvwxyz')
occurrences = {} 
for letter in alpha:
    occurrences[letter] = cipherfilter.count(letter)
for letter in occurrences:
    print letter, occurrences[letter]

All it does so far is show how many times a letter appears. How would I print the frequency of all letters found in this file.

解决方案

import collections

d = collections.defaultdict(int)
for c in 'test':
    d[c] += 1

print d # defaultdict(<type 'int'>, {'s': 1, 'e': 1, 't': 2})

From a file:

myfile = open('test.txt')
for line in myfile:
    line = line.rstrip('\n')
    for c in line:
        d[c] += 1

For the genius that is the defaultdict container, we must give thanks and praise. Otherwise we'd all be doing something silly like this:

s = "andnowforsomethingcompletelydifferent"
d = {}
for letter in s:
    if letter not in d:
        d[letter] = 1
    else:
        d[letter] += 1

这篇关于确定密码文本的字母频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆