维吉尼亚密码破解过程

  • 问题分析
  • 利用重合指数攻击破解密钥长度
    • 维吉尼亚密码加解密算法的python3实现
    • 给定明文,进行维吉尼亚加密,将密文传给破解密钥的程序
    • 破解维吉尼亚密码密钥长度的python3实现
  • 破解密钥内容
  • 附录:相关函数

问题分析

我们从维吉尼亚密码加密方式可以发现,假如密钥的长度为 len_key ,那么明文中第 Xi ,Xi+k ,Xi+2*k,……就是用同一个密码加密,即凯撒密码,所以我们需要先把密钥长度搞出来,而后对每一组进行凯撒密钥的爆破。

利用重合指数攻击破解密钥长度

设一门语言由n个字母组成,每个字母出现的概率为 Pi ,则重合指数是指两个元素随机相同的概率之和,记作 CI =∑ Pi2 (1<= i <= n);

经分析,英文中,一段文字是随机的话,CI =0.0385 ;如果这段文字是有意义的,那么 CI=0.065 (约等)。实际上计算的CI应该用这个公式:


L:密文长度; fi :在密文中的出现次数。
这个的好处就是用我们猜测的密钥长度来进行分组,分别计算每组的CI,再求个平均,计算当前密钥长度下,CI 的值与0.065相差多少。然后按照最接近0.065的密钥长度进行排序,为了提高解密的成功率,我取前10个较为接近的密钥长度作为猜测,实验代码如下:

维吉尼亚密码加解密算法的python3实现

# -*- coding: utf-8 -*-
"""
Created on Thu Apr 25 21:11:29 2019@author: ZHANGJIAHAO
"""
def encrypt(message,key):#message=message.lower()key=key.lower()cipher=''non_alpha_count=0for i in range(len(message)):if message[i].isalpha():if message[i].isupper():offset=ord(key[(i-non_alpha_count)%len(key)])-ord('A')cipher+=chr((ord(message[i])-ord('A')+offset)%26+ord('A'))else:offset=ord(key[(i-non_alpha_count)%len(key)])-ord('a')cipher+=chr((ord(message[i])-ord('a')+offset)%26+ord('a'))else:cipher+=message[i]non_alpha_count+=1return cipher
def decrypt(ci,key):#message=message.lower()key=key.lower()cipher=''non_alpha_count=0for i in range(len(ci)):if ci[i].isalpha():if ci[i].isupper():offset=ord(key[(i-non_alpha_count)%len(key)])-ord('A')cipher+=chr((ord(ci[i])-ord('A')-offset)%26+ord('A'))else:offset=ord(key[(i-non_alpha_count)%len(key)])-ord('a')cipher+=chr((ord(ci[i])-ord('a')-offset)%26+ord('a'))else:cipher+=ci[i]non_alpha_count+=1return cipher
message=input("Please input text:")
key='ilovechina'
ci=encrypt(message,key)
print(ci)
di=decrypt(ci,key)
print(di)

给定明文,进行维吉尼亚加密,将密文传给破解密钥的程序

输入一篇英文文章进行维吉尼亚加密:
测试的明文如下

Predicting the demise of amphibious warfare has been a hobby for many military historians, defense analysts and even veterans. The advent of new military capabilities, such as jet aircraft, quiet attack submarines, ballistic and cruise missiles, long-range intelligence, surveillance and reconnaissance and, of course, nuclear weapons, repeatedly caused the U.S. defense establishment to question the feasibility of large-scale amphibious operations. Most recently, critics point to the rise of so-called anti-access/area denial (A2/AD) capabilities.
However, to paraphrase Mark Twain, reports of the death of amphibious warfare have always been greatly exaggerated. Historically, amphibious operations evolved in response to the rise of new defensive capabilities. In many instances, the same technologies were employed both by the amphibious assault forces and by the defenders on the beach.
Those concerned about the anti-ship cruise missile threat to U.S. amphibious forces should recall that during the battle of Okinawa, the U.S. Navy’s Fifth Fleet experienced some 4,000 Japanese air strikes, including nearly 2,000 by the 1944 equivalent of a cruise missile, the kamikaze. The current A2/AD challenge is only the latest turn of the great offense-defense wheel.
It is worth pointing out that in modern times most amphibious operations have been successful in terms of achieving operational surprise, defeating shore defenses and putting a large number of troops and equipment ashore. The number of modern amphibious operations that were defeated on beaches can be counted on one hand. A larger number, but still a relative handful, failed to exploit an initial, successful landing by moving inland and thus were bottled up on the beachhead.
Major amphibious operations profoundly changed the course of the conflict in a major theater. Examples of such operations include the Japanese campaigns in Malaya and the Philippines, the Allied landings in North Africa, Sicily, Salerno and Normandy, MacArthur’s campaign in the Southwest Pacific, the Inchon landing during the Korean War and the British liberation on the Falkland Islands.
Ironically, the rise of the A2/AD threat only served to increase the value of amphibious forces. After the end of the Cold War, the U.S. withdrew most forward-deployed forces from Europe and East Asia, preferring a posture based on expeditionary deployments from the continental U.S. With the rise of high-end threats in regions of interest, the U.S. faced a serious challenge to its access regions.
The military needs to find ways around the A2/AD threat. Part of the answer will be technological. But as former Deputy Secretary of Defense Robert Work observed, success against an adversary with robust A2/AD capabilities will also require agile maneuver by amphibious forces, including large forcible entry operations.
Amphibious Ready Groups (ARGs) and their embarked Marine Expeditionary Units (MEUs) could be particularly useful in countering A2/AD systems. Forward deployed ARG/MEUs would be inside an adversary’s defensive umbrella. This might seem to be a vulnerable position, but, if properly sized and equipped, these formations could act from the opening of hostilities to blow holes in enemy defenses.
In addition to defeating hostile A2/AD systems, U.S. combatant commands will need to create their own version of an A2/AD “umbrella” in support of forward-deployed forces and allies and to deny an enemy’s ability to attack our air and sea lines of communications. The currently fashionable concept of Multi-Domain Battle envisions U.S. land forces not only conducting defensive operations but also offensive strikes against enemy targets from forward bases. If such positions do not exist or have been overrun, they must be seized or liberated. Hence, the role for major amphibious operations.
The Navy and Marine Corps are acquiring new platforms and systems that will enhance the effectiveness of amphibious forces in operations across the spectrum of conflict. The stealthy F-35B, able to operate from large deck amphibs and small airfields, provides a particularly useful capability for attacking defended targets and providing targeting information for other platforms and weapons systems. Improved ship-to-shore connectors, including the MV-22 Osprey, the CH-53K heavy-lift helicopter, and air cushion landing craft will enable Marine Corps units to project power onto the land further and faster.
The impact of new aviation assets on future amphibious operations cannot be overestimated. An analysis about the future of amphibious operations by the RAND Corporation observed that “Marine Corps aviation is on a path to significantly alter what even ARG/MEUs are capable of doing, and it is important to shape the rest of the force to acknowledge this change. An ARG/MEU with F-35Bs and MV-22s is not just capable of local influence, but can project power and provide defense in ways impossible just a few years ago.”
More needs to be done to exploit the potential of amphibious warfare forces in the 21st Century. First and foremost, the Navy needs to acquire additional amphibious warfare vessels. The minimum desired level is 38 ships, the number needed to lift 2.5 Marine Expeditionary Brigades and the number required to conduct a major landing operation. Several analyses propose higher numbers, between 40 to 50 ships. In addition, the Navy needs to move forward with its plans to procure additional Landing Helicopter Assault ships, capable of operating the F-35B and MV-22, and to replace its aging Landing Ship Dock fleet with the larger and more capable LR(X).
In anticipation of combat with more capable adversaries, U.S. amphibious forces will require greater reach and lethality. A study by the Center for Strategic and Budgetary Assessments proposes, inter alia, enhancing the aviation elements in the ARG/MEU, providing both amphibious ships and landing force with long-range fires and improving air and missile defense capabilities.
The Marine Corps is looking for ways to provide its truck-mounted High Mobility Artillery Rocket System (HIMARS) with an anti-ship capability. It recently experimented with firing the HIMARS from the deck of an amphib. The Army’s Long-Range Protected Fires program, one of the service’s modernization priorities, also could be useful for long-range strikes by amphibious warfare forces. Naval forces operating within an adversary’s A2/AD umbrella could carry large numbers of cheap drones equipped with jammers and other electronic warfare systems to blind sensors and interrupt networks.
Properly sized and configured and employing the right operational concepts, U.S. amphibious forces could be a powerful tool in a combatant commander’s arsenal.

如下图:ci为密文,

破解维吉尼亚密码密钥长度的python3实现

# -*- coding: utf-8 -*-
"""
Created on Thu Jun 27 10:16:10 2019@author: 55416
"""
import re
#列表去重函数
def simplifiy_list(alist):set_list=set(alist)alist=[]for i in set_list:alist.append(i)#print("The simplified list is:",alist)return alist
#定义列表元素求和函数
def add_the_list(alist):the_sum=0for i in range(len(alist)):the_sum=alist[i]+the_sumreturn the_sum
#test_list=[1,2,3,4,5.1]
#t1=add_the_list(test_list)
#print("t1 is :",t1)
def sub_stdfre(fre):return abs(fre-0.06549669949999998)
def zip_flist(fre,n):return (n,fre)
def all_lower(c):return c.lower()
flist=[]
ave_flist=[]
len_list=[]
result=[]
res=[]
initial_text=input("Please input text:")#读入文本
simplified_text=re.sub('[^a-zA-Z]','',initial_text)#去除非英文字母字符
letter_cnt=len(simplified_text)#英文字母总个数
simp_text=map(all_lower,simplified_text)#都换成小写字母
text_list=list(simp_text)#将简化的文本列表化
for key_len in range(2,50):   len_list.append(key_len)for i in range(key_len):templist=text_list[i::key_len]#print(templist)sqcnt_list=[]temp_len=len(templist)#分组字母个数once_list=simplifiy_list(templist)#对当前分组去重复for letter in range(len(once_list)):#对于非重复列表中的每一个字母,查找它在templist出现了多少次cnt=templist.count(once_list[letter])#字母在templist出现的次数sqcnt=cnt*(cnt-1)#我们假设文本数量足够大,不会出现cnt==1的情况sqcnt_list.append(sqcnt)#存放的都是sqcntflist.append(add_the_list(sqcnt_list)/(temp_len*(temp_len-1)))ave_fre=add_the_list(flist)/len(flist)ave_flist.append(ave_fre)
res=list(zip(len_list,(list(map(sub_stdfre,ave_flist)))))
result=sorted(res, key=lambda x:x[1])
print("最有可能的前十个密钥长度及其对应频率和0.065差值为:")
for i in range(10):print(result[i])

运行破解密钥长度程序后的结果如下:

可以看出,密钥key='ilovechina’的情况下长度为10,推测出的最有可能的密钥长度为10,其他情况好多都是10的整数倍,于是我们有足够把握确定密钥长度就是10.
把密钥长度存到lth变量中待用。

破解密钥内容

密钥长度知道了,还是根据统计学:我们可以知道每个字母在英文中的频率

字母表频率

  #标准的频率表freq=[0.08167  ,0.01492,   0.02782,    0.04253,    0.12702,    0.02228,            0.02015,0.06094 ,0.06966    ,0.00153    ,0.00772    ,0.04025,   0.02406,    0.06749 , 0.07507   ,0.01929    ,0.00095    ,0.05987    ,0.06327,   0.09056,    0.02758,0.00978 ,0.0236 ,0.0015 ,0.01974    ,0.00074]#标准的fi平方的求和:0.065

正常的文本中,每个字母出现的频率是遵循上述规则。

那么破解密钥就变得很简单了,我们统计在某个密钥长度下的密各个组的字母频率,这样对单个组来说,就是凯撒密码,我们循环26次,判断哪种情况下字母频率与统计字母频率的乘积的和与0.065的差最小即可。

这样我们对密钥的某个单个字符破解出来了,同理我们可以破解出密钥。
破解代码如下:

def MyKey(text,length): key = [] array =makelist(text,length)for i in range(length):flist = f_list([row[i] for row in array]) multi = [] for j in range(26):Sum = 0.0for k in range(26):Sum += freq[k]*flist[k]multi.append(Sum)flist = flist[1:]+flist[:1]n = 100ch = ''for j in range(len(multi)):if abs(multi[j] -0.065)<n: n = abs(multi[j] -0.065)ch = chr(j+97)key.append(ch)return key
k=MyKey(simplified_text,lth)
print("猜测的密钥为:",k)

我们再来看输出结果:

成功进行了密钥的破解。

附录:相关函数

# 根据密钥长度将密文分组
def makelist(text,length): textarray = []row = []index = 0for ch in text:row.append(ch)index += 1if index % length ==0:textarray.append(row)row = []return textarray
#标准的频率表
freq=[0.08167  ,0.01492,   0.02782,    0.04253,    0.12702,    0.02228,            0.02015,0.06094 ,0.06966    ,0.00153    ,0.00772    ,0.04025,   0.02406,    0.06749 , 0.07507   ,0.01929    ,0.00095    ,0.05987    ,0.06327,   0.09056,    0.02758,0.00978 ,0.0236 ,0.0015 ,0.01974    ,0.00074]
# 统计字母频度
def f_list(lis): li = []alphabet = [chr(i) for i in range(97,123)]for c in alphabet:count = 0for ch in lis:if ch == c:count+=1li.append(count/len(lis))return li

维吉尼亚密码破解过程相关推荐

  1. 维吉尼亚密码破解(Python完整详细源码)

    维吉尼亚密码破解(Python完整详细源码) 欢迎大家访问我的GitHub博客 https://lunan0320.github.io/ 文章目录 维吉尼亚密码破解(Python完整详细源码) 1.写 ...

  2. 古典密码之维吉尼亚密码破解思路

    首先要说一句,这是我第一次用python coding,所以很多地方都不太懂,吐槽自己竟然用sum当变量名,之后有用sum()函数,然后报了错还不知道为什么.不知道用ascii码要做chr()转化,等 ...

  3. 维吉尼亚密码加解密与破解

    实验目的 1.进一步理解维吉尼亚密码的加解密原理 2.进一步理解维吉尼亚密码的缺陷和利用方法 3.提高编程实践能力 实验要求 1.实现维吉尼亚加解密函数 2.破解维吉尼亚加密的密文 实验步骤 1.维吉 ...

  4. 密码学(二):古典密码之维吉尼亚密码的破解

    维吉尼亚密码的破解 一.引言   上一章我们介绍了维吉尼亚密码的原理,是通过移位替换的加密方法进行加密,但是因为概率论的出现这种简单的移位或替换就容易破解了,其原理很简单,英文中字母出现的频率是不一样 ...

  5. 维吉尼亚密码及其破解

    凯撒密码 凯撒密码是一种简单的加密方法,即将文本中的每一个字符都位移相同的位置. 如选定位移3位: 原文:a b c 密文:d e f 由于出现了字母频度分析,凯撒密码变得很容易破解. "如 ...

  6. 维吉尼亚密码(Vigenère Cipher)

    由于频率分析法可以有效的破解单表替换密码,法国密码学家维吉尼亚于1586年提出一种多表替换密码,即维吉尼亚密码,也称维热纳尔密码.维吉尼亚密码引入了"密钥"的概念,即根据密钥来决定 ...

  7. 用python实现维吉尼亚密码的加密与唯密文解密

    目录 加密过程 加密原理 代码加密的思路 代码预览 代码解析与运行效果 唯密文解密过程 解密原理 代码预览 代码分析与运行结果 加密过程 加密原理 字母表中每一个字母都对应着一个数字,从A~ Z依次是 ...

  8. 密码学——维吉尼亚密码

    文章目录 前言 一.维吉尼亚密码简介 二.维吉尼亚算法 1.加密算法 2.解密算法 3.主函数 总结 前言 本内容为博主学习维吉尼亚密码的笔记,如有错误,烦请指正. 一.维吉尼亚密码简介 维吉尼亚密码 ...

  9. 几分钟学会密码学(一)——维吉尼亚密码

    1.维吉尼亚密码加密原理: 26个英文字母(a-z)对应26个个自然数(0-25),也就是a对应0,b对应1,c对应2,以此类推. 2.维吉尼亚密码加密方法: 假设明文信息是abcd,密码是ab,那么 ...

最新文章

  1. 第7章——狄克特斯拉算法
  2. springmvc学习笔记--Interceptor机制和实践
  3. jsp 跳到servlet路径_想打开一个jsp的页面就跳转到servlet中去,并向servlet传值。用什么跳转...
  4. 机器学习碎碎念:霍夫丁不等式
  5. Intel图形库Mesa的持续集成
  6. Linux进程防杀/防崩溃monitor的实现
  7. Ubuntu 16.04下ASP.NET Core+ MySql + Dapper在 Jexus、nginx 下的简单测试
  8. 草稿 datagridview的显示与修改
  9. 算法专题(一)1.1 希尔排序,快速排序
  10. 在linux下编译android下的opencv,使用cmake的方法
  11. linux mysql跑高_linux 下如何查看mysql跑了哪些服务
  12. Origin 2021 创建双y轴
  13. Redis技能—底层IO多路复用
  14. LibOpenCM3(二) 项目模板 Makefile分析
  15. 微信小程序实战 wx.showNavigationBarLoading(),下拉动画配置无效
  16. java 时间类 joda_Java Joda-Time 处理时间工具类(JDK1.7以上)
  17. Windows和Linux的环境变量
  18. 2019年度受欢迎的开源软件
  19. vue3.2中ref高效的秘密:依赖收集错级位运算操作(bit/dep.w/dep.n)
  20. 物联网无线技术具体是怎么分类的,主要的应用场景是什么?

热门文章

  1. Unity Render Streaming(亲测可用)
  2. 用c语言实现键盘画图,用C语言实现键盘画图
  3. scp命令获取远程文件
  4. stm32双串口相互通信
  5. Redis-15、发布订阅
  6. 海康监控平台监控“视频巡逻”做投屏监控器
  7. Linux下UID和GID的简介
  8. jq动画插件,自制基于vue的圆形时钟
  9. 改装版 hiwifi 1S 路由器
  10. ubuntu和petalinux常用命令 cnpmjs bt.ly j.mp