古典密码解密(频率计算法)、英文字符统计及频率计算、按频率显示英文字符、相关matlab源代码

已知英文字符:

WB WI KJB MK RMIT BMIQ BJ RASHMWK RMVP YJERYRKB MKD WBI IWOKWXWVMKVR MKD IJYR YNIB URYMWK NKRASHMWKRD, BJ OWER M VJYSHRBR RASHMKMBWJK, JKR CJNHD PMER BJ LR FNMHWXWRD MKD WKISWURD BJ INVP MK RABRKB BPMB PR VJNHD URMVP BPR IBMBR JX RKHWOPBRKRD YWKD VMSMLHR JX URVJOKWGWKO IJNKDHRII IJNKD MKD IPMSRHRII IPMSR W DJ KJB DRRY YTIRHX BPR XWKMH MNBPJUWBT LNB YT RASRUWRKVR CWBP QMBM PMI HRXB KJ DJNLB BPMB BPR XJHHJCWKO WI BPR SUJSRU MSSHWVMBWJK MKD WKBRUSURBMBWJK W JXXRU YT BPRJUWRI WK BPR PJSR BPMB BPR RIIRKVR JX JQWKMCMK QMUMBR CWHH URYMWK WKBMVB

网上查找,理论上英文字符的概率分布(注意这个不是上文的概率分布!!!!!!!!!):

破译结果:

    'it is not an easy task to explain each movement and its significance and some must remain unexplained to give a complete explanation one would have to be qualified and inspired to such an extent that he could reach the state of enlightened mind capable of recogniqing soundless sound and shapeless shape i do not deem myself the final authority but my experience with kata has left no doubt that the following is the proper application and interpretation i offer my theories in the hope that the essence of okinawan karate will remain intact'

源代码:

本代码计算已知英文字符的概率分布,并进行概率对比,再猜单词,最终实现密文的破解。

clc;
clear;
str=fileread('message_code.txt'); %读入全文             
% str=(''); %读入全文
[c,~,freq] = find(accumarray(uint16(str(:)),1));
fprintf('%c %d\n',[c freq]')
fprintf('\n');
rank(:,1) = c;
rank(:,2) = freq;
ans=sortrows(rank,-2); %只根据第二列进行排序 -2表示降序
ans(:,3) = ans(:,2)/sum(ans(:,2));
fprintf('%c  %d  %f\n',[ans(:,1) ans(:,2) ans(:,3)]');
% plot(ans(:,1),ans(:,3));
figure;
bar(ans(:,3));
name = {' ','R','B','M','K','W', 'J', 'I', 'P', 'H', ...
    'D' 'S' 'U' 'V' 'Y' 'N' 'X' 'O' 'A' 'C' 'T' 'L' 'Q' 'E' 'F' 'G'};
set(gca,'xtick',1:1:26);
set(gca,'xticklabel',name);
for i = 1:size(str,2)
    if strcmp(str(i),'R')
        str(i) = 'e';
    end
    if strcmp(str(i),'B')
        str(i) = 't';
    end
    if strcmp(str(i),'M')
        str(i) = 'a';
    end
    if strcmp(str(i),'K')
        str(i) = 'n';
    end
    if strcmp(str(i),'W')
        str(i) = 'i';
    end    
    if strcmp(str(i),'J')
        str(i) = 'o';
    end
    if strcmp(str(i),'I')
        str(i) = 's';
    end
    if strcmp(str(i),'P')
        str(i) = 'h';
    end
    if strcmp(str(i),'H')
        str(i) = 'l';
    end
    if strcmp(str(i),'D')
        str(i) = 'd';
    end
    if strcmp(str(i),'S')
        str(i) = 'p';
    end
    if strcmp(str(i),'U')
        str(i) = 'r';
    end
    if strcmp(str(i),'V')
        str(i) = 'c';
    end
    if strcmp(str(i),'Y')
        str(i) = 'm';
    end
    if strcmp(str(i),'N')
        str(i) = 'u';
    end
    if strcmp(str(i),'X')
        str(i) = 'f';
    end
    if strcmp(str(i),'O')
        str(i) = 'g';
    end
    if strcmp(str(i),'A')
        str(i) = 'x';
    end
    if strcmp(str(i),'C')
        str(i) = 'w';
    end
    if strcmp(str(i),'T')
        str(i) = 'y';
    end
    if strcmp(str(i),'L')
        str(i) = 'b';
    end
    if strcmp(str(i),'Q')
        str(i) = 'k';
    end
    if strcmp(str(i),'E')
        str(i) = 'v';
    end
    if strcmp(str(i),'F')
        str(i) = 'q';
    end
    if strcmp(str(i),'G')
        str(i) = 'q';
    end
end

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值