【频率分析法介绍与Python程序代码实现(一)】

频率分析法介绍

频率分析是一种密码学中常用的技术,用于破解替换密码或者其他简单加密算法。

它基于一个简单的观察:
在任何自然语言中,某些字母或字母组合出现的频率比其他字母高。

通过统计加密文本中字母或字母组合的出现频率,可以推测出加密算法中使用的替换规则,从而还原出原始的明文。

频率分析的基本思路:首先统计加密文本中每个字母或字母组合的出现次数,然后与已知的自然语言字母频率进行比较。

通常来说,英语中字母E的出现频率最高,其次是T、A、O等。通过比较加密文本中各个字母的频率与自然语言中的频率,可以推测出加密文本中每个字母对应的明文字母。

然而,频率分析并不是一种完美的破解方法。它对于简单的替换密码可能非常有效,但对于复杂的加密算法或者密钥长度较长的密码则无能为力。此外,如果加密文本样本太小,统计结果可能不够准确,导致破解失败。

Python程序代码示例

下面是一个频率分析的简单Python程序代码示例:

先创建一个字母表:

dic={chr(i+97):0 for i in range(26)}

在这里插入图片描述

频率统计函数:

def frequency_analysis(text):
    # 统计每个字符出现的频率
    # 大小写合并
    for char in text:
        if char in dic:
            dic[char] += 1
        elif char.lower() in dic:
            dic[char.lower()] += 1
            
    
    # 按照频率从高到低排序
    sorted_freq = sorted(dic.items(), key=lambda x: x[1], reverse=True)
    
    # 输出结果
    for item in sorted_freq:
        print(item[0], ':', item[1])


输入与输出:

# 示例文本
text = "Hello, world!"

# 进行频率分析
frequency_analysis(text)

这段代码会统计文本中每个字母出现的频率,并按照频率从高到低进行排序输出。
在这里插入图片描述

将需要分析的文本赋值给text变量,然后调用frequency_analysis(text)函数进行分析。

大量文本进行测试:

text = '''1:1 亚伯拉罕的后裔,大卫的子孙,耶稣基督的家谱。(后裔子孙原文都作儿子下同)
The book of the generation of Jesus Christ, the son of David, the son of Abraham.
 
1:2 亚伯拉罕生以撒。以撒生雅各。雅各生犹大和他的弟兄。
Abraham begat Isaac; and Isaac begat Jacob; and Jacob begat Judas and his brethren;
 
1:3 犹大从他玛氏生法勒斯和谢拉。法勒斯生希斯仑。希斯仑生亚兰。
And Judas begat Phares and Zara of Thamar; and Phares begat Esrom; and Esrom begat Aram;
 
1:4 亚兰生亚米拿达。亚米拿达生拿顺。拿顺生撒门。
And Aram begat Aminadab; and Aminadab begat Naasson; and Naasson begat Salmon;
 
1:5 撒门从喇合氏生波阿斯。波阿斯从路得氏生俄备得。俄备得生耶西。
And Salmon begat Booz of Rachab; and Booz begat Obed of Ruth; and Obed begat Jesse;
 
1:6 耶西生大卫王。大卫从乌利亚的妻子生所罗门。
And Jesse begat David the king; and David the king begat Solomon of her that had been the wife of Urias;
 
1:7 所罗门生罗波安。罗波安生亚比雅。亚比雅生亚撒。
And Solomon begat Roboam; and Roboam begat Abia; and Abia begat Asa;
 
1:8 亚撒生约沙法。约沙法生约兰。约兰生乌西亚。
And Asa begat Josaphat; and Josaphat begat Joram; and Joram begat Ozias;
 
1:9 乌西亚生约坦。约坦生亚哈斯。亚哈斯生希西家。
And Ozias begat Joatham; and Joatham begat Achaz; and Achaz begat Ezekias;
 
1:10 希西家生玛拿西。玛拿西生亚们。亚们生约西亚。
And Ezekias begat Manasses; and Manasses begat Amon; and Amon begat Josias;
 
1:11 百姓被迁到巴比伦的时候,约西亚生耶哥尼雅和他的弟兄。
And Josias begat Jechonias and his brethren, about the time they were carried away to Babylon:
 
1:12 迁到巴比伦之后,耶哥尼雅生撒拉铁。撒拉铁生所罗巴伯。
And after they were brought to Babylon, Jechonias begat Salathiel; and Salathiel begat Zorobabel;
 
1:13 所罗巴伯生亚比玉。亚比玉生以利亚敬。以利亚敬生亚所。
And Zorobabel begat Abiud; and Abiud begat Eliakim; and Eliakim begat Azor;
 
1:14 亚所生撒督。撒督生亚金。亚金生以律。
And Azor begat Sadoc; and Sadoc begat Achim; and Achim begat Eliud;
 
1:15 以律生以利亚撒。以利亚撒生马但。马但生雅各。
And Eliud begat Eleazar; and Eleazar begat Matthan; and Matthan begat Jacob;
 
1:16 雅各生约瑟,就是马利亚的丈夫。那称为基督的耶稣,是从马利亚生的。
And Jacob begat Joseph the husband of Mary, of whom was born Jesus, who is called Christ.
 
1:17 这样,从亚伯拉罕到大卫,共有十四代。从大卫到迁至巴比伦的时候,也有十四代。从迁至巴比伦的时候到基督,又有十四代。
So all the generations from Abraham to David are fourteen generations; and from David until the carrying away into Babylon are fourteen generations; and from the carrying away into Babylon unto Christ are fourteen generations.
 
1:18 耶稣基督降生的事,记在下面。他母亲马利亚已经许配了约瑟,还没有迎娶,马利亚就从圣灵怀了孕。
Now the birth of Jesus Christ was on this wise: When as his mother Mary was espoused to Joseph, before they came together, she was found with child of the Holy Ghost.
 
1:19 他丈夫约瑟是个义人,不愿意明明的羞辱他,想暗暗的把他休了。
Then Joseph her husband, being a just man, and not willing to make her a publick example, was minded to put her away privily.
 
1:20 正思念这事的时候,有主的使者向他梦中显现,说大卫的子孙约瑟,不要怕,只管娶过你的妻子马利亚来。因他所怀的孕,是从圣灵来的。
But while he thought on these things, behold, the angel of the LORD appeared unto him in a dream, saying, Joseph, thou son of David, fear not to take unto thee Mary thy wife: for that which is conceived in her is of the Holy Ghost.
 
1:21 他将要生一个儿子。你要给他起名叫耶稣。因他要将自己的百姓从罪恶里救出来。
And she shall bring forth a son, and thou shalt call his name JESUS: for he shall save his people from their sins.
 
1:22 这一切的事成就,是要应验主藉先知所说的话,
Now all this was done, that it might be fulfilled which was spoken of the Lord by the prophet, saying,
 
1:23 说,必有童女,怀孕生子,人要称他的名为以马内利。(以马内利翻出来,就是神与我们同在
Behold, a virgin shall be with child, and shall bring forth a son, and they shall call his name Emmanuel, which being interpreted is, God with us.
 
1:24 约瑟醒了,起来,就遵着主使者的吩咐,把妻子娶过来。
Then Joseph being raised from sleep did as the angel of the Lord had bidden him, and took unto him his wife:
 
1:25 只是没有和他同房,等他生了儿子,(有古卷作等他生了头胎的儿子)就给他起名叫耶稣。
And knew her not till she had brought forth her firstborn son: and he called his name JESUS.'''

在这里插入图片描述

  • 15
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值