关于对Enigma机原理的理解和实现
前言
在南邮的第二年里,终于接触到了密码学这部分内容。而第一次实验课,我们在老师的指导下完成了对恩尼格玛(Enigma)机的算法模拟就充满了挑战与乐趣。
基本原理
在我们的密码学课程中,首先接触的就是古典密码学,具体包括置换密码和代换密码。虽然没有融入复杂的数学算法,但是置换和代换的思想却是密码学的核心,一直伴随着密码学的发展。
而恩尼格玛机是古典代换密码的集大成者,通过迭代和多表代换的方式,将古典密码的内涵发扬的淋漓精致。该机器确切地说,是对二战时期纳粹德国使用的一系列相似的转子机械加解密机器的统称,它包括了许多不同的型号,为密码学对称加密算法的流加密的的典型。而且实际加密转轮机的精密和反射器的加密解密一体的设计更是将这一机器推向更高的高度。关于具体的加密算法步骤,本文就不过多介绍,很多相关文章都已经将转轮机的原理介绍的很详细了。
实现思路
在这次实验中,要求我们实现以下转轮机的加密解密过程:
在本次实验中,我运用的是python语言,因为我认为python里的字典数据类型可以很好的刻画转轮机中三个轮子间的一一对应关系。程序的主要部分包括:映射表、密钥设置函数、位移函数、反向位移函数、加密函数、解密函数、控制流程页面。
首先,映射表可以表达出三个轮子的对应关系,由于是三个轮子(慢,中,快三个轮子)和最后的输入输出组成的,所以一共有四种不同的对应关系,我用l_table,m_table,s_able,f_table表示它们,具体过程并不复杂,只需要写出四个字典就完成了。
然后,位移函数和反向位移函数是指转轮机在每次输出一个密文字符之后,都会改变原本的对应关系。所以位移函数的作用就是模拟轮子转动,改变映射表,使其向下位移一格,而反向位移函数,就是使得映射表向上位移一格,这在解密过程中会用到。
接着,密钥设置是模拟实际情况,说白了就是在输入密码之前,可以任意设置三个轮子的初始映射关系,我用k1,k2,k3分别表示快,中,慢三个轮子的初始位移量,这样可以使得加密系统更加多元复杂。
再接着,加密解密函数就是程序的核心部分,每一次明文字符依次通过前三个轮子的映射关系,从而得到密文字符。并且转动一次快轮子(中轮子和慢轮子也会依次变化),保证下一个明文字符会通过一个不同的加密映射表继续加密。而解密就是其逆过程,运用了字典键值对互换,列表倒序等算法。
最后,就是控制流程页面,这个就是使得上述过程流程化输入输出,并不需要过多介绍。
代码
1 | # 转轮机映射表 |
测试
实验要求我们加密以下文本内容:
明文:
cryptology, science concerned with data communication and storage in secure and usually secret form. It encompasses both cryptography and cryptanalysis.
Cryptography was originally the study of the principles and techniques by which information could be concealed in ciphers and later revealed by legitimate users employing the secret key.
It now encompasses the whole area of key-controlled transformations of information into forms that are either impossible or computationally infeasible for unauthorized persons to duplicate or undo.
Cryptanalysis (from the Greek kryptós and analýein, “to loosen” or “to untie”) is the science (and art) of recovering or forging cryptographically secured information without knowledge of the key. Cryptology is often—and mistakenly—considered a synonym for cryptography and occasionally for cryptanalysis, but specialists in the field have for years adopted the convention that cryptology is the more inclusive term, encompassing both cryptography and cryptanalysis.
Cryptography was initially only concerned with providing secrecy for written messages, especially in times of war. Its principles apply equally well, however, to securing data flowing between computers or data stored in them, to encrypting facsimile and television signals, to verifying the identity of participants in electronic commerce (e-commerce) and providing legally acceptable records of those transactions. Because of this broadened interpretation of cryptography, the field of cryptanalysis has also been enlarged.
This article discusses the basic elements of cryptology, delineating the principal systems and techniques of cryptography as well as the general types and procedures of cryptanalysis. It also provides a concise historical survey of the development of cryptosystems and cryptodevices. A brief introduction is also given to the revolution in cryptology brought on by the information age, e-commerce, and the Internet. For additional information on the encoding and encryption of facsimile and television signals and of computer data, see telecommunications system and information processing.
带入上述模拟恩尼格玛转轮机的代码,可以得出以下密文:
Tipmymzipx, rmscqak oeclurmmt frko fqxe enpejhfpjrorl vis btqssmo yo zggigu mkp pljpkfz acptfv hmfh. nr yihimnwkevb alng zersrbqxuapb pun tmwxwigbuemrx.
Sriazhlbppfu aar qcztsvhosb eiq apfud bn ndc kasfurrsmv niq yjmfpnhzbt ge xeysy pgnqjddvrfh nftdw ha acnumaszq nu gwljffe jfa ctrnc ihgylrlu ad ifwaqzkzqn zqjkd lwwdqbifs wom ofhewy ynl.
Uh uwp vjjpozfxfly ppe jyvtt iqav lp gef-avbucrjdtl zrjvraugptymjhh iq wtkcwyaqnbz lytm egtmp amdj ltw rddnrb woigihxiey tp cwkoljrecnpczxl ujrboxoodt zrx ffsswqdwtmws xqeapgq qw bdjtzlrky yg vvxz.
Flxxtmgwztmtj (raeh fgy ogang qbnwwót xkd jsvbýhtc, “rp dwjxte” xe “ku bfhbb”) uc iwm cphcoku (dgu myh) tr sdnlptaejn pv rrtmkgk tdbnrbhgxpacvigee ctarnzp jgdovwmyqim hfbpeao qpbyusoxa gu sbk jeq. miykxlsieg fv dqblj—icm yuwaxgtswi—qfelbnxzop w dwdllfi ncn mkplmmekpaib vyf swavbjrdwjmw fhw rvdrwczbrveqw, dlp yhamsyydrrm yl cgi xzzij orec jkc xdyiv gznuoqw mjc pqcmtrniuh arts bezhcnldsh rm fhf sozo sowmmvqrz vzcb, afvwyvmuoxzq frsp mulmbuzcqbnf hqo azomgysoalctv.
Qiivszqnmfpq oyu djqtkdoen opki yoqpfbeqe tvoy amfkhnuze qxkclcj eym ffwapec xuzcongt, yfaquidlvp zh xtdyi pz gyp. sao nkcippstlo antuv wjlmbza lynd, bsvnqcu, kg xpvycmgg uopz orlzgkf kkpvnxb xuyjttjhh bb npum zmollk jm rrbk, am moladcmcsx bbckriyuu xfr nrgmthhezl srubmko, yi zeoysclgu ybw atibgdzx mq eyumjieskwci eg dfrvozzyac alqlyihs (a-msazuaow) btr qifvywpfd wovifpw qnxwyrcvdg zugygan pi iuawb glnbfckvhjyx. nemvhrp aj nvmv zfygwrfeg suloogyutfglul nv mrskforymnke, eec orsrr vw brkuugbiepnnh woz ntpn gbew xmsgrlxo.
Dvad smzkaza nwuknrpcj vjn gubst mpeexogt ca yllvshkiqt, wauulxbgvxq mco hmttaqrgm uyrjgtc cpb mjiibughwl vy otemnhzpavvf tp rqer dx qlf qmoxiki fapju cip ddlvdqmlyj mq whszpxanhwnlo. uu ecxl cmuktzxi j khzbfqw qhbelzdyrt bewavz cd pqx vfgsewgfcqr vr iopwnnrbzekng cnb wpqszbibawsqe. n rtosb fsryyxhdmlno nz lpho pqxee mt wfc tvrfsqsudd ff kcopdaohot ltiwohr tj mx epy oiddwqqbblh cul, y-cpuokpoi, kom tsc pcalneuv. gjc vgwqijvgwd uikvbojkcej yt prt wnsadeoy nfx axcxxpospn jc klhxcuvyt ofy mmelyjwavz oqjrkyv mvg oq pzgmnttu vytv, zer emrfnpcreiprmgkvnp yashhi eeb gpuxxjbwsrs yhgrxrzvgw.
接着的内容是对于第一段内容加解密的图片详细演示:
结尾
恩尼格玛机在密码发展的历史长卷中只是普通的一页,但却使得我也受阻重重,纸上得来终觉浅,绝知此事要躬行,课本上的密码看上去易,但唯有实践才能理解它们的威力。


