当前位置:   article > 正文

现代密码学—密码学基本编码实践 16进制向base64编码转换 等长16进制字符串异或 找出密钥并破解密文 python

现代密码学—密码学基本编码实践 16进制向base64编码转换 等长16进制字符串异或 找出密钥并破解密文 python

实验地点:E楼III区503 实验时间:11.17
一、实验室名称:攻防实验室
二、实验项目名称:密码学基本编码实践
三、实验学时:6学时
四、实验原理:
Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,它是一种基于64个可打印字符来表示二进制数据的方法,可用于在HTTP环境下传递较长的标识信息。
Base64要求把每三个8Bit的字节转换为四个6Bit的字节(38 = 46 = 24),然后把6Bit再添两位高位0,组成四个8Bit的字节,也就是说,转换后的字符串理论上将要比原来的长1/3。
五、实验目的:
1、学习密码学的基本编码方法,包括利用python库函数进行编码、解码,异或编码等;
2、理解明文、密文、密钥等概念,并对简单的密文进行破解。
六、实验内容:
1.Convert hex to base64 (编程将十六进制的字符串转换成base64编码):
编程将如下的十六进制字符串:

49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d
  • 1

转换成如下的base64编码的字符串:

SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t
  • 1

注意:之后的实验会用到本题的程序。
2.Fixed XOR (将两个等长的十六进制字符串进行异或)。
编程实现两个等长的十六进制字符串的异或,如果你的程序能正确运行,就可以将如下的前面两个字符串异或成第三个字符串:

1c0111001f010100061a024b53535009181c
  • 1

… after hex decoding, and when XOR’d against:
686974207468652062756c6c277320657965

… should produce:

746865206b696420646f6e277420706c6179
  • 1

3.Single-byte XOR cipher (一个字符串被单字符加密,请找出该单字符)
如下是一个被单字符异或后得到的十六进制的字符串,请找出该单字符(即密钥),并解密该密文。

1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736
  • 1

提示:利用英语字母频率,穷举法。
4.如下密文中包含一个含60个字符的字符串是利用单字符异或加密得到的,请找出该字符串。
密文如下:

0e3647e8592d35514a081243582536ed3de6734059001e3f535ce6271032
334b041de124f73c18011a50e608097ac308ecee501337ec3e100854201d
40e127f51c10031d0133590b1e490f3514e05a54143d08222c2a4071e351
45440b171d5c1b21342e021c3a0eee7373215c4024f0eb733cf006e2040c
22015e420b07ef21164d5935e82338452f42282c1836e42536284c450de3
043b452e0268e7eb005a080b360f0642e6e342005217ef04a42f3e43113d
581e0829214202063d70030845e5301f5a5212ed0818e22f120b211b171b
ea0b342957394717132307133f143a1357e9ed1f5023034147465c052616
0c300b355c2051373a051851ee154a023723414c023a08171e1b4f17595e
550c3e13e80246320b0bec09362542243be42d1d5d060e203e1a0c66ef48
e159464a582a6a0c50471310084f6b1703221d2e7a54502b2b205c433afa
ec58ea200e3005090e1725005739eda7342aed311001383fff7c58ef1f11
01305424231c0d2c41f105057f74510d335440332f1038ec17275f5814e1
05f12f380720ea2b19e24a07e53c142128354e2827f25a08fb401c3126a6
0d17272f53063954163d050a541b1f1144305ae37d4932431b1f33140b1b
0b4f070f071fe92c200e1fa05e4b272e50201b5d493110e429482c100730
100a3148080f227fe60a132f0c10174fe3f63d1a5d38eb414ca8e82f2b05
0a19e83c58400a023b13234572e6e4272bf67434331631e63b5e0f00175c
54520c2ceb45530e0f78111d0b0707e01e4bf43b0606073854324421e6f9
09e7585353ee4a34190de1354e481c373a1b2b0a136127383e271212191f
0f060d09fb4f2d5024022c5ff6463c390c2b5f1a5532071a31f33503fcea
371d39121605584f48217235ee1e0602445c162e4942254c071954321d29
4a0900e63e5f161e15554045f3594c2a6a77e4e52711602beaf53ae53bed
29011616565d2a372a605bee39eced31183fe068185c3b445b391fe53232
e4102337000303452a1e2f2b29493f54ed5a037b3e08311b625cfd005009
2d560d4b0618203249312a310d5f541f295c3f0f25235c2b20037d1600f3
2c245155e8253708391a7ceb0d05005c3e080f3f0f0e5a16583b111f4448
493804044d262eec3759594f212d562420105d6a39e70a0f3957f347070c
e72d1d1f103807590f4339575e00381074485d2d580249f744052605e11d
e131570ae95307143a71131729552d001057a4540a1f425b190b572dee34
2c1655342f02581c202b0a5c17a358291e1506f325550f05365e165c1c5f
e318164df80b043e5406296e5359271d152f552e155a43eda81f23231d1c
001de0413e174e18192c061e4b3d1b5626f90e3e1429544a20ee150d0c20
32e902193219033c58191302441a5c1b584825ea140c290927aaea53e23c
3a36363a732e32ea3f0e430508204b332c382a19292d5b291122e123446a
1804115614031f5f571f2b143c5d3c1b257a4b37350f18445a3e08341c3d
21f2fb250b2e55151e77253a3f0e5f4b2030370a4155e720e73914e35a4a
510a55583a3c491221397c123a2b14a8305b3b09e71b241d0e51202e1a32
1b51202f4917232b512a141d6812f03c455df05e5a1c2cee14390b3b593a
5f5731e5203116ee131a4a4b24112cef5d0822f035e6547d3a0014462f26
0028fb522104f771501a555d3f581e30e9ec3e49e3e63123432f07794145
1459f6312f000e5a1373e346e40f211e1b0b0e17000f391f170552150500
7e301e18325717e3412e022f087be30e5641080151357714e0e0eee15e11
533258e9360f513b083aa51d2824222f40200a470537ecec392d31070b38
07e32c180dfa56496a461627542115132a4c284050495b23e2245b093159
2d3c230a1e5a300f6c3e26ed0d1709434950fd6f1e121335054129e4e4ec
ef22fa2112311b11584ce43434f46f521a215433f9514fe33d313a3e0838
34e7f336270c08010f2f544f0f1c1e235c0222644c2632efec061de2115f
121a42395d4c560d213b0c0a26a7e4f4382718153d5e511158a10b2c021e
e05d414dfa40222f0c382a03235f4d0d04372d4b7855105e26e44f2e0555
7f3a4f1351f85b0344223e1177e14707190c0e311f4ca633f5f3e9352372
01424d5d1a322a0d381717130e181d07240c2c19ecee750b1a37085d014c
16012c5de55a0314a8260e2759e439123ca0c81c321d454e4e0ee14f4c1d
0b1415512f38580e4e2a227def242643183c224f0ea146443403022fe9fd
43eb2b1078322a02192d5b5e0c360d584d0b5e2c13072912ee32f03f4155
002a52553e08361b0be0074b573e201c164c093a5c0f0159333b59770d5b
38e63c1c5244301a5a01f26930321256143e1ae05e1120a9eaf20a192d58
7d54140a152ef4035f09083ded531ee04df55848020656a1342e502649eb
0c211dfe101702015516341136252f3f06f73247133113f5642d083a3417
015e3d51433f3c003e5e28030b1d413eee186824504b241e0f0d32373e2b
2d465040ec130c5c0e2704aa17010c40095207223669110f22f45ea155f7
14552e2b341e5ce0195351066a23e3283e0ee935444b255a1c5c3cef7614
372b453d5a357c05142be65b3c17f92d2b134853390a312bf92a531b513d
5658265f4c0ce4440a20322f591a413034292b312206a01be6453a512d21
1c585c19f31f785324f8583d1ee02620342b10a236263f105011ee5b0e14
0f522b550818591a752e5fea0e033322ee5e280a4a1b244f5a2b35341255
39093c1ced331b264127173f1312e2455fa33b31012c1f4d073c553f5d5e
18f82d5d07e2430b3b3c1b5b49effb0313173f5d4a2e5c134555ff6b1d1a
550a20234202726341190311295254f4064205aa515ae0145a23071c4e18
3f2047024e3ce4555a1b39fa145455012c3afb0f2d11134846182e3c575b
e3e456571937762828065443153b51152e262f09c937024405284f236432
012f580c3536ec5c021574541d5c41123a4e661d5f0f5f344a083e3a5e4c
4216252d01eb0a2a4623621b48360d312c29f33e380650447617124b3e71
54141e59323606390204e95f1206520e5c084510034d30171c5e744f335d
1e30061401600b342e171059526d1949431a3f412f56594c183711ea4837
3131254f11e76f550e1e4d26f1391f44363b151c31281ff45259351da0e6
5def250d0f3505385f22e9f4112633005d272d092e0138275851f943e90e
0939165718303b445210095c16390cf04f19450e06f4545c0a0c320e3e23
1e0b0b1f573f3d0fe05d43090fa8482242300819313142325b1f4b19365b
0d3b2a5d271e463d2203765245065d5d684a051e5815265b52f3171d3004
6af423303817a43324394af15a5c482e3b16f5a46f1e0b5c1201214b5fe4
4030544f3f51151e436e04203a5e3b287ee303490a43fb3b28042f36504e
1a2d5a03fc0e2c04384046242e2b5e1548101825eb2f285f1a210f022141
122355e90122281deeed3ba05636003826525d5551572d07030d4935201f
2a3c484a15410d3b16375d4665271b5c4ce7ee37083d3e512b45204f17f6
03222801255c2c211a7aeb1e042b4e38e8f1293143203139fb202c325f2b
06542a28041956350e292bf3fe5c32133a2a171b3a3e4e4e3101381529e3
4a5209ef24e5f3225e503b143d0e5747323fe7ee3d5b1b5110395619e65a
1fee0a3945563d2b5703701817584b5f5b54702522f5031b561929ea2d1e
e7271935100e3c31211b23113a3a5524e02241181a251d521ff52f3c5a76
144a0efee02f0f5f1d353a1c112e1909234f032953ec591e0a58e55d2cf4
efee0cf00d0955500210015311467543544708eb590d113d30443d080c1e
1a562c1f7e2b0030094f051c03e30f4d501a0fe22a2817edfc5e470c3843
1c3df1135321a8e9241a5607f8305d571aa546001e3254555a11511924
eb1d3f54ec0fea341a097c502ff1111524e24f5b553e49e8576b5b0e1e33
72413e2f5329e332ec563b5e65185efefd2c3b4e5f0b5133246d214a401d
352a0ae632183d200a162e5346110552131514e0553e51003e220d47424b
1d005c58135f3c1b53300c3b49263928f55625454f3be259361ded1f0834
2d2457524a1e1204255934174d442a1a7d130f350a123c4a075f5be73e30
0c0518582d131f39575925e0231833370c482b270e183810415d5aec1900
453b181df1572735380b0446097f00111f1425070b2e1958102ceb592928
010a4a2d0b0926082d2f1525562d1d070a7a08152f5b4438a4150b132e20
2b395d0d5d015d41335d21250de33e3d42152d3f557d1e44e4ee22255d2d
4a1b5c272d0d1c45072639362e402dee2853e51311262b17aa72eb390410
e7015f0215352030574b4108e44d0e1a204418e62325ff7f34052f234b2d
1d563c13202346071d39e34055402b0b392c27f552222d3deb3843ee2c16
29332a521f3c1b0811e33e1a25520e323e75e01c17473f55071226120d3d
210b35ee1a0a5335222e35033905170c4f3104eb032d425058367d5a2bf2
1e553809415efb1c460f2f0ffafaec491e4d4e49510452e8245a366a4106
e1f92cee0e10142514e7ec13155c412fe901092f1f0fa738280c5eee5e04
3526291e0b2a5f486a3051041f4c16372f5402e6f70b31a03525190b161a
260e5e1f0c2e4d7528ef11552fefe247201e4752085c1da903563c162a4b
2a14ff2e3265e604075e523b24455c364a7f284f3a43051d52152f1119e8
5f02e55a4b1300063640ef10151002565f0b0c010033a1cbef5d3634484a
1b121c585b495a5e033a09037f2d1754072c2d49084055172a3c220bed4f
1613400e1632435c0018482aa55b363d26290ae4405ded280f2b0c271536
4011250ce02119464a1de43113170356342c272d1d3355555e5706245e0a
16272d5e545953002e10020875e223010719555410f91ce518420e382456
0d4037320345f945241a1d090a545a310142442131464f4d10562ae4f05a
07ee4d4ae12e571e313c1636313134233e495459e548317708563c2c1b2f
e75803294b36565225552c3406304f0201e43323291b5e0e2159025c2f25
5e63194411490c44494232237e1b323108573d3f391d1f3537e4165a2b35
51000a3a264c503b5852072a5636f04f5cea58a42838f5fca876415c3521
3c14130be511275932055a30aa2d03470c51060009f210543002585f5713
10f0370c5823115200e5015d083e2f1a5df91d68065c1b03f0080855e529
02ec00f1462d034123151ba6fc07eb3d5e54e85a3f3ee532fb41791a060b
0c29274232f93efb3d465544e45e491b042ced245100e3f05c14134c254b
5741235f051e080401a8013c065627e8ee5432205114243d54320e133f2d
4a4d181635411f5d084e31ed230c16506d5125415e060e4dcd0e5f3708e3
2d531c3e22065a5eee07310c145305131800063e4a20094b2006ea131240
e7335c1c4308160be6aa551a0f5a58243e0b10ee470047683c345e1c5b0c
5434505ee22a18110d20342e4b53062c4d79042a0a02422e225b2523e95a
3252212407115c07e15eee06391d0519e9271b641330011f383410281f0e
2cee2b355233292b595d1c69592f483b54584f7154fd4928560752e333a1
17272b272f110df5e91c560a39104510240b5c4b0c1c570871e422351927
c32550ec3f132c0c2458503ae5241d3c0d7911480a073826315620403615
16e11c270d2b010650145de2290b0beb1e120a3a354b2104064f3b533c4e
505746313d4d2e3455290a281ee81d50007e1148252528025237715a342a
1c0a13163e404e40242142061d34185421160220fa031f7a423a08f2e01a
101d303802f51b0c08ef461259315b553823e622a12d565509e23c624139
0a3d1309e4384c0eed383846545a035a41ee1771513b090a031e15f45159
2d4944092a1965542507003b23195758403e175a0a450c5c38114de21141
eb100fe63a031c4b35eb591845e428441c0d5b0037131f5c160a31243619
c155ef0d19143e24392507a202581a25491b135c27571d5c5b35250f0bef
0e1d510556485e39557e044e2cf10457523016473f500b1e36370c17591c
7e5a19250a5e152b46f5130a094cef08e84704ef10197324464b0114017a
3b56f126390008343d3c400232ed201667211f0b1a1413080202530b08e2
4912321b61c90a0cf6ef0a0a0c0f17fa62eb385e2616194526701aff5fe6
2c57114b0400152d4f2aeb18ed41386c2e3a023a281d1a311eefe750ebab
3a4353282114593b3e36446d2c5e1e582e335337022930331f211604576a
295f3bfae9271ae8065a3b4417545c3e5b0df11a53351c78530915392d2e
074a122ee01b17131e4e124e2322a9560ce4120e37582b24e1036fe93f30
3c08290121090ef72f25e4f220323444532d3fe71f34553c7b2726131009
12e84a3308590357a719e74c4f2133690a20031a0b045af63551325b1219
0e3d4fe03f56523cf40f29e4353455120e3a4f2f26f6a30a2b3e0c5b085a
57f3315c33e41c0f523426232d0651395c1525274e314d0219163b5f181f
53471622182739e9e25b473d74e1e7023d095a3134e62d1366563004120e
230a06431935391d5e0b5543223a3bed2b4358f555401e1b3b5c36470d11
22100330e03b4812e6120f163b1ef6abebe6f602545ef9a459e33d334c2a
463405faa655563a43532cfe154bec32fe3345eb2c2700340811213e5006
14241340112b2916017c270a0652732ee8121132385a6c020c040e2be15b
251119225c573b105d5c0a371c3d421ef23e22377fee334e0228561b2d15
2e4c2e373b434b0d0b1b340c300e4b195614130ea03c234c292e14530c46
0d2c3f08560ee32e5a5b6413355215384442563e69ec294a0eef561e3053
193c100c0b24231c012273e10d2e12552723586120020b02e45632265e5f
2c175a11553d4b0b16025e2534180964245b125e5d6e595d1d2a0710580b
213a175ff30855e4001b305000263f5a5c3c5100163cee00114e3518f33a
10ed33e65b003012e7131e161d5e2e270b4645f358394118330f5a5b241b
33e80130f45708395457573406422a3b0d03e6e5053d0d2d151c083337a2
551be2082b1563c4ec2247140400124d4b6508041b5a472256093aea1847
7b5a4215415d544115415d5015455447414c155c46155f4058455c5b523f
0864eb4935144c501103a71851370719301bec57093a0929ea3f18060e55
2d395e57143359e80efffb13330633ea19e323077b4814571e5a3de73a1f
52e73c1d53330846243c422d3e1b374b5209543903e3195c041c251b7c04
2f3c2c28273a12520b482f18340d565d1fe84735474f4a012e1a13502523
23340f39064e306a08194d544647522e1443041d5ee81f5a18415e34a45f
475a392637565757730a0c4a517b2821040e1709e028071558021f164c54
100b2135190505264254005618f51152136125370eef27383e45350118ed
3947452914e0223f1d040943313c193f295b221e573e1b5723391d090d1f
2c33141859392b04155e3d4e393b322526ee3e581d1b3d6817374d0c085b
c2ea5821200f1b755b2d13130f04e26625ea3a5b1e37144d3e473c24030d
ee15025d2019f757305e3f010e2a453a205f1919391e1a04e86d1a350119
1a5beb4946180fe0002a031a050b41e5164c58795021e1e45c59e2495c20
1121394f1e381c3647005b7326250514272b55250a49183be5454ba518eb
1ee55936102a465d5004371f2e382f1d03144f170d2b0eed042ee341eb19
ec1014ef3ff1272c3408220a41163708140b2e340e505c560c1e4cf82704
274b341a454a27a0263408292e362c201c0401462049523b2d55e5132d54
e259032c444b091e2e4920023f1a7ce40908255228e36f0f2424394b3c48
34130cf8223f23084813e745e006531a1e464b005e0e1ee405413fe22b4e
4af201080c0928420c2d491f6e5121e451223b070dee54244b3efc470a0e
771c161f795df81c22101408465ae7ef0c0604733ee03a20560c1512f217
2f3a142c4155073a200f04166c565634020a59ea04244ff7413c4bc10858
240d4752e5fa5a4e1ce255505602e55d4c575e2b59f52b4e0c0a0b464019
21341927f3380232396707232ae424ea123f5b371d4f65e2471dfbede611
e10e1c3b1d4d28085c091f135b585709332c56134e4844552f45eb41172a
3f1b5a343f034832193b153c482f1705392f021f5f0953290c4c43312b36
3810161aea7001fb5d502b285945255d4ef80131572d2c2e59730e2c3035
4d59052e1f2242403d440a13263e1d2dea0612125e16033b180834030829
022917180d07474c295f793e42274b0e1e16581036225c1211e41e04042f
ec2b41054f2a5f56065e5e0e1f56e13e0a702e1b2f2137020e363a2ae2a4
53085a3b34e75a1caa2e5d031f261f5f044350312f37455d493f131f3746
0c295f1724e90b001a4e015d27091a0b3256302c303d51a05956e6331531
e42b315ce21f0def38144d20242845fa3f3b3b0ce8f4fb2d31ed1d54134b
2957023141335d35372813263b46581af6535a16404d0b4ff12a207648ec
e4421e301de25c43010c504e0f562f2018421ce137443b41134b5f542047
0c5600294e085c1d3622292c480d261213e05c1334385108c145f3090612
062d2e02267404241f4966e6e010052d3224e72856100b1d22f65a30e863
324950394700e11a01201a0564525706f1013f353319076b4c0d015a2e24
2a1be80e2013571522483b1e20321a4e03285d211a444d113924e8f41a1f
27193ae2302208e73010eaa1292001045737013e10e4745aed2c105b25fb
1b135d46eaef103e1d330a14337a2a4302441c1631ed07e7100c743a0e35
1a0957115c293b1c0de853245b5b18e2e12d28421b3230245d7b4a55f355
e7360e2b3846202a2926fa495e3302ed064d127a17343a1f11032b40e8f5
06e8f90a3118381c5414157d1434050210363e30500511a00a3d56e10438
30021931f7193e25a0540ef52658350929380974fb035b1a5d2c042959c7
151b0c24052d0e56025404390e5a3909edec0d03070f040cff710825363e
2a2328120b2203320810134a0c0a0ef30b25460bec011c1e26e913575a51
e12d0948ed3c511416151d1c54082b3e385d14f838510bec4e4b5f585321
1559305c3a49192a010f04ec11001a3d5a5621e5535358353206521f013f
172c2c155a3a322009505c290516a2c4e4405a1e0a1e353b6e1a5a4e2f09
552c34e2432b0df1132b130841000d4007232339a2092a593f142b0a0117
0931432e452d3aea1d02587d3a3e56ed2a3050e2f9363df366331e421947
0250094823545b20163f1d0a36a92228ed25564d1a304deae8035c32370d
4314380e264e2359e6a412504a424328e84434ff30236649353315344a00
25e33540550d3c15135b0eed451cfd1812eaf2063f085d6e214d121c342f
37513b2d0a4e3e5211372a3a01334c5d51030c46463e3756290c0d0e1222
132f175e4c4af1120138e1f2085a3804471f5824555d083de6123f533123
0de11936062d3d2f12193e135f38ff5e1a531d1426523746004e2c063a27
49241aee1802311611a50de9592009e936270108214a0c4213a01f09545f
02e14d2babee204a5c4337135821360d021b7831305963ee0737072f0deb
1512371119050c0c1142245a004f033650481830230a1925085c1a172726
3be62f230a4b50526ec9345100252aa729eafa59221b3fa517304e500a15
5e57f231333c3d0c470a47551733511031362a3bed0f334a3f3136104230
eb24015d051a151f245905061a37ea273d2239fe02463a5e314d565f0457
23025f415d290a594e3b5940313347a11c5e41531ff15a385a183829780a
51e0035f2deb3b163eabe8550e2e0414491f573b5419234a28183044e112
1d54e8390b26585f3aef5f14206672240c4a5e5d31e01b4d406e351401fa
e555173e242c753b275d4ee50b2f26501402a71b1b5733ec19ee34284aed
2ee8f023401c09383b084d623ef324ee5a33065a6d5e365b092c5d0d4501
3f4e024d4b161e144d5e3b140d1e2944465b491d265603a705373c231240
544f0d4ea6091e00e62d3e130d4f005139f339001a3b480c221b730be75e
5f1f4f3e0a0dec3b5128e32960e42d0fee02275528154b10e65c36555a2e
ea3e311b5b0f5f220b1f1b2914f12111f41213e06232224df5ec0114470d
51203f1e01e5563851284013514a565e53125223052f47100e5011100201
3f5bee2305217838582be55958a00245265b0308ec56525b5c114c2d5407
e6e74818e53602160e45372029eb4de72754ec3f49290d2f5901014c0e7f
08e715e612380a5c1908285a1222073a023c562907384e4f470444483f34
1110382b5225343ba6092133483e2d683e1e280227084a1e405e3a341513
415f240f0c53e3f7196e2252fb0105347f345e531f535a344bf439220916
5722e7f7fa2f4c2e057e2a025e2dec31413439aa12265f5a3458f81a4b15
135839401856f337a72fec475a060de239a650163a55392a5b303f051415
56090f18023a2b16e2364407050d48e1541408281d3aa3e84c5b264c1f33
1725f9540aec5e10ed293e4e5a5a2d2125f053251a55395d1c2044022231
292d523ff86a180620075f325e02566659f30423525a053a01f0087f4b3b
17fe493808f25309251e1325596ce32b42311e5d0c2f58652640582a4b17
67381a5afb7128150a0043e45b173d2111155c49092d2635370a3a201826
e62d021d36e03b205d5f1f295c094608342a412122583f3bfc34190be62c
393a055f59060d454a235326e844243a30285c14e316272524f4f0444f51
352c3c5b2b5845244f55494940194721f80b120f07392b7c2c5a0508111e
2f1219430151e60f11150b101e295736361b1e053e4d08f83f230e2c383a
ef5b1d492610e834330f5cf3a2485d324f2822084f41111f582957191b19
1e3e223704fe1d2e1f592753e5550f15170b231b4234e945301f5605a670
300d322759ea0337015c662a0e073809543f2741104835512d0624551751
373727ef1f41084d0b5c0c0137283b1337026aea1c5ae115064ffa183402
09152b11e1233e5a0e302a521c5a33181e180026463744a82c024b4bf04e
1df61df1263fee59135c13400950153d3c5c59183b020b1d2d2c492f4968
e2000c405a01ede30c4c082e2537443c120f38fc57c43651423e5c3beb1d
1922182420191b293e163d58020b005f454a0621051a38e80b090a463ee9
39513f2d47042c0fe5134419ec48490f150f323a5ee7a7e0201e193a5e1b
2037200a2b1013567b35fb4a0f322c2f49435d091920521c302b413f5f35
775d1a345b483b35a02a4c3e17ee3a3d5a5b57153613264f23041922432f
35125b3e0a1d2257eb002a26455e1a2f042e1545e92f0b3408032c4f3551
2d4c392321300a18ed4f3e2c314d20500052aa3917e55d0d29500754282e
381b2e263758f63c474a1c23110c2d5f1c220412e91043580656080c0427
081ce1e5350b6a3535f0e6592e5b543432340e38f008e0324102e45a3f25
30040c181615362e4d1016160a4a5c006eeb1d2422355a3f1028ff192a07
53f6354d4b5d121974245c14f0225713331f2e381810101428571725e432
1a2c06372d5b1419742150042d25003c2650512834ef16e51d183f0f0508
3d191107251100ee2e4125405a44174f061e0e1e5959e606530e06ed245e
3f592d47512dec5922500e460e1de7183b4c3c2e583942255a0c5d4d2305
3438001e482a002d56113a1fe13bed542d3508e22f4e22221431121c1539
ed445a5d28415073eb18022ef836274d573a48090f2a663058194901405d
215b143954fc313c1e28584b51e729ef31013b232bfb4c52e2322a2d4557
5244102e1c3d304450ee01761924e62ff2173305e15809102b2125284dfc
171a3f010f3639056f2be71c2047581de32e05a20833e1221b0e25362459
2958280de238084f5a1c292e005be71f3b311e1f415809383d3862260238
361f56ecee120156375862eb3627185c2519545149e2e50b1f3b0c4e3352
e6115f440634e4005d273611e41c5d383c3814537b3d23362b084024345b
10370656372e0236eb4f3303e216505f0e465228383729394faa2f205f34
2e125b2f2c1d0f1f170e0c51331f0c06291610345c0603791f33253f0e0c
1c2b080526133aeb3e23571d4cfa1e48057a2a010a490a50391b09514f2e
59383ae11237e5450029162d2e1d3e09221a160e42ea06ea0ca7c7ecf4ea
3d3024f34d5c07464bea3b185e110d3a10395d3b2632343cf30ca2e6065a
262f111c0e15441a4825111b185f1e5756243206125f4603e97e79582d27
2d5801ee2654113e2da00b58e9260d643c10423e1d1f42093b0d0f7d5102
3649211f210456051e290f1b4c584d0749220c280b2a50531f262901503e
52053e3e152b5b2b4415580fec57ef5c08e5ed43cc2d2e5b40355d0d2017
6d3917263f030c4b55f0025d501e57504a122729293c4c5819680d3001ed
1e313323324e5e177b171cf70c371541395c0e2b7726e42505483014362e
1910e4f7253f0a012057e03b1e3b4201362b224ff60e0b3a1d115b043957
200c1e0b242e5e3b4755f61e3be05c040908f1234358e55562711d2efa0f
0737e0160b1d13132044080d2325f1f0ee2f00354f2106471131020a5d0b
3f21060de62c052a17576e2ce729242b3e3621300627f01e52580a480050
1b381a11351f4f5d22040c3c4b3e7d263714e8e61a571d107a34260a4a51
edf52314e111207c0b23eb482f441d211f306137152407040e08530a783e
3c054e2d4e2905275e640220f74f1a193f54e1ed5b4e2a290eab27a55147
33522817335316ea2f3df957e25e02030601514f09f74c2fedee102d3114
5d05231d03313826164156110c44e4111f4658005e115e300f413b430300
380bf53a4331f74627492c133fe8eb3141ee39040def040c1a0ae914e3ed
5b00f0211f0a091e05582e22f05a5d262e0ce352251d25100b102b11e339
36053935f051f959093252411e2d5af81f360c0fa15d0b373b1d26323b77
501424184202206215e05944505c4817514540445b0207025de05b050932
0a5a114515536f553a352c513f0b12f700345fa51d5efb28222676e559ea
561b0557403f5f534a574638411e2d3b3c133f79555c333215e6f5f9e7ec
6658f7210218110f00062752e305f21601442c5310162445ed4d175630f3
0e2154253c4a22f02e1b0933351314071b521513235031250c18120024a1
e03555453d1e31775f37331823164c341c09e310463438481019fb0b12fa
37eee654410e4007501f2c0e42faf50125075b2b46164f165a1003097f08
2a5332145851553926523965582e5b2f530d5d1e292046344feaed461517
583d2b06251f551d2f5451110911e6034147481a05166e1f241a5817015b
1f2d3f5c310c315402200010e24135592435f71b4640540a041012ee1b3f
5b2010060e2f5a4d045e0b36192f79181b0732183b4a261038340032f434
3a5557340be6f5315c35112912393503320f54065f0e275a3b5853352008
1c595d183539220eec123478535337110424f90a355af44c267be848173f
41053f5cef5f6f56e4f5410a5407281600200b2649460a2e3a3c38492a0c
4c071a57e9356ee415103c5c53e254063f2019340969e30a2e381d5b2555
32042f46431d2c44607934ed180c1028136a5f2b26092e3b2c4e2930585a
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327

七、实验器材(设备、元器件):
学生每人一台PC,安装Windows 7操作系统及VC++\Python开发环境。
八、实验思路,代码及运行结果:
将上述四个题目的解题思路或方法,代码和运行结果分别写在下面。
**1.**解题思路或方法:
第一小问要求我们编程将十六进制的字符串转换成base64编码,这一小问大体分为两个步骤。第一步将十六进制字符串转化为ASCII码,进而得到字母串,即明文信息。第二步则对明文字符串进行base64加密。
先来看第一步,将十六进制字符串转化成ASCII码值得到明文信息。使用以下语句:
m= binascii.unhexlify(hex_string)
其中,binascii是Python中的一个模块,在这里将十六进制字符串转换成了ASCII码值,而unhexlify将十六进制字符串转换为可视化的字符串。如果此时进行print(m),则可得到明文信息如下:
在这里插入图片描述
第二步,将上述的明文信息进行base64编码加密,Base64的编码规则是将3个8位字节(3×8=24位)编码成4个6位的字节(4×6=24位),之后在每个6位字节前面,补充两个0,形成4个8位字节的形式,那么取值范围就变成了0~63。又因为2的6次方等于64,所以每6个位组成一个单元。也就是说,加密时先把字符串中每个字符对应的十进制ASCII码值转换为2进制, 比如a的ascll码为97, 97的二进制是:01100001,然后把每个字符的8位二进制数都连起来,每6位分为一组,最后再把6个二进制码转换为Base64对于的编码。在本次实验中可直接使用Python中的base64模块。

    encodestr=base64.b64encode(m) 
    return str(encodestr,'utf-8')
  • 1
  • 2

我使用的是Python3的版本,所以字符都为unicode编码,而b64encode函数的参数为byte类型,之前unhexlify得到的字符串是byte类型的,所以无需转码。而得到的encodestr必须进行转码,我在这里使用’utf-8’进行转码。转码后,输出加密后的密文结果。
在这里插入图片描述

运行结果:
在这里插入图片描述
2. 解题思路或方法:
第二个实验要求我们编程实现两个等长的十六进制字符串的异或。
为了实现两个等长的字符串实现一对一的对应异或,使用Python中的zip()函数对字符串进行分组。zip函数进行分组,返回一个列表 (x,y),例如(‘1’,‘6’)(‘c’,‘8’)等。
x^y是按位异或,实现每个zip列表中两个数的异或。
%x 无符号整数(十六进制) 故 %02x是输出两位十六进制,字母小写空缺补零。
核心代码如下:
m_a=binascii.unhexlify(a)
m_b=binascii.unhexlify(b)
result =’’.join("%02x" % (x^y) for x, y in zip(m_a, m_b))
输出最终结果为:
在这里插入图片描述

运行结果:
在这里插入图片描述
3. 解题思路或方法:
第三小题要求在已知被单字符异或后得到的十六进制的字符串的条件下,找出密钥即该单字符并解密该密文。提示中提到了英文字母频率和穷举法。
穷举法给我的启示是,将单字符的取值范围0x00-0xFF这里面总共256种字符(包括不可打印字符在内),都用来作为密钥对密文进行解密,看哪个单字符得到的结果更符合实际语句,从而确定密钥和明文。因为这个被加密过的字符串是被单字符异或得到的,使用这256个字符作为密钥,与加密过的字符串进行异或便可以得到256组明文。
英文字母频率是用来判断语句是否符合常用的语言规律。每个字母在语句中出现都有不同的概率,可以通过这种频率来判断我们得到的256组明文是否符合常理,通过比较判断,找出256组中最合适的字符串作为明文,相应的单字符作为最终的密钥。
英文字母频率如下:
FREQUENCY_TABLE = {
‘a’: 0.08167,‘b’: 0.01492,‘c’: 0.02782,‘d’: 0.04253,‘e’: 0.1270,‘f’: 0.02228,‘g’: 0.02015,‘h’: 0.06094, ‘i’: 0.06966,‘j’: 0.00153,‘k’: 0.00772,‘l’: 0.04025,‘m’: 0.02406,‘n’: 0.06749,‘o’: 0.07507,‘p’: 0.01929,‘q’: 0.00095,‘r’: 0.05987,‘s’: 0.06327,‘t’: 0.09056,‘u’: 0.02758,‘v’: 0.00978,‘w’: 0.02360,‘x’: 0.00150,‘y’: 0.01974,‘z’: 0.00074, ’ ': 0.1918182
}
定义一个子函数score,这个函数的作用是返回分数值,该分数值是输入数据的每个字母的概率之和。如果一个字符串的分数值比较高的话就可以认为这句话在平时中是比较符合逻辑的。所以认为是正确解密得到的字符串。

def score(input_bytes):
    score = 0
    for byte in input_bytes:
        score += FREQUENCY_TABLE.get(chr(byte).lower(), 0)
    #chr() 用一个范围在 range(256)内的(就是0~255)整数作参数,返回一个对应的字符。
return score
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

定义下面这个子函数,功能是求解明文,使用密钥与密文异或来解得明文字符串,将结果返回。

def singlechar_xor(input_bytes, key_value):
    output = b''
    for char in input_bytes:
        output += bytes([char ^ key_value])
    return output
  • 1
  • 2
  • 3
  • 4
  • 5

下面是判断明文得分的部分:
尝试单字符密钥的每个可能字节,用该字节解密密文并计算每个明文的英语分数。得分最高的明文可能是使用正确的key值解密的那个。

def singlechar_xor_brute_force(ciphertext):
    candidates = []
    for key_candidate in range(256):
        plaintext_candidate = singlechar_xor(ciphertext, key_candidate)#明文
        candidate_score = score(plaintext_candidate) #计算得分
        result = {
            'key': key_candidate,
            'score': candidate_score,
            'plaintext': plaintext_candidate
        }
        candidates.append(result)    #往列表的尾部添加新的对象
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

运行结果:
在这里插入图片描述
4. 解题思路或方法:
这一小题和第三小题的思路大致相同,而且可以使用在第三小题中定义的子函数。因为题目中给我们的字符串都是以六十个十六进制字符为一行的,所以可以将数据放在文本文档中,然后把数据一行一行读入。和第三问的区别在于,第三问是固定了密文,只需找到合适的密钥来解密正确的明文,而这一题则不知道密文是哪一个,需要多一层尝试,即每一行都要作为密文进行一次与第三题相似的过程,然后将最后所有的明文字符串进行比较,筛选出最合适的明文和密钥。其他的与第三问一样,例如可以调用第三小题中判断明文得分部分的子函数,来比较哪一行字符串更符合语言逻辑。

运行结果:
在这里插入图片描述
代码:
完整代码请移步资源区

九、总结及心得体会:
之前三次密码写实验我都是用C语言去实现的,这次实验由于涉及base64编码,用Python相对简单,其实我对于Python的掌握并不熟练,仅仅是在大一的暑假理论性的学习了一遍,很少付诸实践去使用。这次实验不仅让我熟悉了密码算法,更是加强了我对Python的熟练度。
十、对本实验过程及方法、手段的改进建议:
利用英文字母频率法暴破是我受到题目的提示,在本次实验中采用的方法,我还在网上看到利用编码的汉明码距的破解方法。大体思路是将二十六个英文字母用八位二进制数来表示,它们之间的汉明码距是非常小的。例如,如果a-z分别代表0-25,那么他们的八位二进制表示也就是00000000-00011001,极端地来说两个字母之间的汉明码距最大也就是4。但是如果与一个单字符异或之后,它们的汉明码距一定会大幅度地变大。所以在判断256组明文是否是真正的明文时,只需算出其汉明码距之和,最小的可以认为是正确的明文。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/226411
推荐阅读
相关标签
  

闽ICP备14008679号