fork download
  1. // Coursera Cryptography I by Dan Boneh
  2. // Programming assignment 1
  3. //
  4. // Many Time Pad
  5. // Let us see what goes wrong when a stream cipher key is used more than once.
  6. // Below are eleven hex-encoded ciphertexts that are the result of encrypting eleven plaintexts with a stream cipher, all with the same stream cipher key.
  7. // Your goal is to decrypt the last ciphertext, and submit the secret message within it as solution.
  8. //
  9. // Hint: XOR the ciphertexts together, and consider what happens when a space is XORed with a character in [a-zA-Z].
  10.  
  11.  
  12. #include <string>
  13. #include <iostream>
  14.  
  15. using namespace std;
  16.  
  17. const string ciphertext[11]= {
  18. "fc5e552ae0a379bd2ab3d9f5b8e4a4bcc84bc699ad5852c85a1a317698f54f413c93851c09e943185e60117cff40233fe763fc8d81424789da42d7b5c875ad0e23526922698d230d38cc86d54f3659caeecb19ff9d2adbb7bee8b21862d9e8e40bb01ad810c4125e8bccee6a02defe2925b8cd55c95b9b3fdeb8c3c85a47566632342cde15c0bdc88242f57ea20a12984bed7aa78e015752291e7b0ee48328c5ad897c8cfc823539705d381dff4e1fd4c648514a933b75321e1bf49c2b656a4e817897beda162ce76f31e4e359088c9a25e4863cf8a1544b7549bf92438471bcc3a018910ef48bbd23212096f8cd610a7366d65b21fb59019ab776e62cfb8852a8673cdeea4a9a27b8b0dba1bb2f3d777fce879d80d0f6bbb8f2e724d65224cbd5250a29b05e7ae4d5d4d366f6e81a60603e598a66e10c0eb552478e2e360ec3d77bc6dda213334bc58572de45d976aedaf677584ad1a67f323fff85924af37dc9e50d07731195ee68a78a39b54f513f4cb55de54be2ca07840080a1d4f33a3b52ce5988683ded56e743ac9c9223df6e4ce426e37a8370250e5ed6d0d337ae0f76c19ad6e7110bb6a52a6da66946e81a1ad96ec7dcc2e5aff614f130d6f542b74e0ca331a1a303b97608da3a5c8bf137bb9d8ad38d5585720c2233738ab80c158f2fd8b7c74f12af206cd90a5a97a7f3c24aa78cec7dccda325d063c4d9dd571cfae7e55a374b6d7b5c94cf6ecae9e1675ba2b68d07543792e19b31b429d9fa2407d5a4db1bf656291e931f975b2d9ff65cbf0d25884068b4bb36246202118d79119f3b7b972b4",
  19. "eb44497ae3a47abe28ea89eaa3efa3f9c95995dabe0146d5531b2a749df14218758eca1c46af470855741161f9056d20f96ff48fd5465c9c9e54dfbfd930bd4736436f3f25812c1e2e9f96cf456647caf9dc19f89e2b94e3b5ecfe0475d2b8e01de616c417c012568dcbab250d8ae52e66b0da5eca03d431c2add6d24002477c39663c804cd9a7dd8951f568a10613c547997ba089534344251f6d51e4832dc1e2887fc2fc997c39794e7b1ab65707d9cb484c4cd66f78395b17efc16e4e685dc46495ecc7033dee3d21abef4618c29235f68738f2ab071f680ca6865c9334b2c9a31c8e47a087e9622b30d3eedf32073c32cd1334a9551e98a966a824b89a5fb27c37c0ea5c9732bbf5dfbfeb3e34602c9d979d86c7e1fab2fde168c11967e3dd3d167bff1d58f2c9c9ce64b8ed136a7a3044d92ff35e1ffd4408dd2e3006ddd87b9e92b05b205a869e6cde17c777bc96e63f5f0f81a96c2221ff81845fbf6bdcb14c43711195f366b4cf72ac49527754a84cb74fa1dd0dd700d4b4c9a76e364f9e5d9e787daa25f44cae8e9270d07500b02df470962420054cd3cece2dfd4668808597e75400b8a12b7af43d5ae55b04956bcbc1dfe7b9eb5af96282e94fbf4e0ea868fbeb2fed3000c43a478bec3aa3de8994c365cd6049313d6e9aa70b50957c91ad8f4f46ec267dcc5e568bb3f0d35ba3d8f163c88e624a1f3a469ad33c83b56f1cac79f7dae69d4cb8fde6884277a720799977597e671db71c5e9e85f107335278babb216b93f321f968b18afb24d2f6c354d00d984aa86a5a203b16d2974be2a8a177f47a80b09eee3f60603e8f1f27bfd2fbecf924d60c459557421ed59f75c386f709760e8d2e8b",
  20. "e8490c32f5ba70f23eaf9cebeae0f7b5c951d299a81145c2490f2b7c82f71b5172dd9e0040fc0c0a426b4465b1012f3fe07abdb6c94250c8d7549a90c233ac587066683371c43a0d6b8f93d30a6359cbeedd4aed93369fe3b0fffd0530c2f0e40af553d20dd2034a9bcbee6a028db12676fbcb44d85bd40bcfb0d6ce40021761253421c31890badc9e46b966b30c14c24bce7aaf89015358210e6a11bdcc11d8eb833ac5fbc4705678502249ac551ad0921b4d54d63d7b3e5d13f7836e68654ac87a82bec0123bee6f30eeeb5a5d928123f6973be3aa434b7044ab954fd67da586a71fc241b883a06e2d3796e9c47307735ed01d34a95f02d5a533b53dbe9842ba7f79d2b84c9328abf5d7b4a53e7c6a6ace9f9296d6f6a8fdb3ca2693086ce394240761b44f3be8d1d7de2580e91b72672d56d935e31604f9405add6d3c1bc3d77fd793b31f724b8d906e9b29c77fbe96fb241a5b83a463382cee8a984ef170dbfd0d1773548be173a7cf20fb41487b1bbe59f90ea0db48d11d90a5d4f46e3145da168b733cfa13f343b4cb8865967709b03be86d8322321f4cd1d39d30e603258293d9f65413a3f7216ef40a5dee08179065d7dcc5e7b2ec56b643cde84bfa0f0ba93192a402b96409c93d08c2f27bbe979c9ed8729e7248703f6edf821d5187328ca2810e32e07474ce455a9dafe78758bb8af163cc8862461d7f419d863c83aa791ca971e6d1b49d42f6fdae990d27bd206e82384a7f2802f20d448699a27a701349bcaa687488f372b869b38acc61ddf2d400c5459a4db4615d733a09d4914be8f8827efb6fcca099a71121792f8b036bbfecedecf12bd51700851f424a96bd79c780fd163f149b20ccaf4a493a9d514b3704db6865bb58d148a5f1104c177c59d1fc98732efa970d8d6232cc8b7d96bc21d0699f5990cabb6ebb910165a46f4dcaf2a6ee57176411e6a65f50b560b0aa17d9ef580a2d",
  21. "f64a0c23fbb935b628ac90ebafa1b4b6c84cd6d0a30d45cf590f2b358cea1b4c7498ca1a46e0584d5f621170e7053f29e166f48fc6035080db499ab9dd30bb1e2459693c62c4241b6b8f9dd359755ec0fedc57fc812bd5e399e3b21c78d3b8ee0df816c444c901518c98ee634c87fe3a25bfda4ad0419178cfb6ddd3400e587d257a2adf1f90a8dacc57f162f10216d719dc7cab8e52164f264d6a15ad823ac2adce6ac9fa823169651c3a1ebe4812dbd71b4b04dc293d32481fe4966e7f6142cf70d1f7c6532ee42121e8e75b08919d23f68172e4ef4f026044ab945ed672bed4a345ce02a08aac6d6830d9f3df711a3c67ca1534fa455187a162b324a99e58fb6036deaf4a9a2fa2f79abebe3e2f6c688bd29c8482f0b5bde0e621dc0977e8d1380029a5523be2d599db72b7fe1a33663f1bd92fee5e1cfd484bc66d3a02c0de3eca95b309371f8c823ada45d976a9daf677555a85b6642f2aab8b9a0bfc6bd4e24e0a730195ee62a0d97cfb6148704fb55de50eb5df1184079be0cae875350adf42db6827aa1ff20fb984c16adb610bf920e329843528054a9fd3cf2bfe166085dcdefb450cf7a3266df42a57ee0f118b2acdc98bf6a9fa5ae565ccb307830110ed66babe0afd300dc7265bcea138b5909c9ec46e986442352573dfa611528e28d8a2d84f1faf7c65d145579feaecd356ab8aa57fc1932c481a7606cef2358af94549ae3ce1d1b38547b8eae183166ebb306ad06c432d2217bb1c5fd29ef75d601348b7fe6e61dcf93dac75f7c9f56acaf0d31bd1168440ae7e1c201c1f9c8051fef8a57afb7589bdcae23d327933ce0631eddea8b8f565dc014c9d574a50c29f38c79cfd444c08862c82a152057bd553433a06883d64b11dd753b4be0f44566659d7b1cf7535b6a04e886833dc8b73d59a3b8363851682d6ba6fe8964e6dad6743d6b5e0f34c122101a9a65f19af6fa6a34a90df551c23c8561a4abdd86dd6b5df825cc3efbccdadcb156bc0057d5ac1284b745b690d6586d4d87e8176635498589f3fd7ec041cfc47e2102a5e894148db905581102896289bc752ef36fec3b312a9fe4cb0aea18dacecfb2cc37422c48cc03af81868165da68611e6dd3f78ad4dbf191e3d189e0a80b1e01151927566d6b7f58255ee768a08dcf79641eede7f53e6584e3f9faae2ccd29ab40c43ee0ec22b0ba8faea9e778bdcf977416378107d2126de928fdad87ce4541f343d33512a883afc56ac0794bf2bfed23af13cb61c2a0285d446835742b77c34f4cd2164204daae1963bb434d087fbb8a342000130150c4a366c5509629a8a9a1919aa76470b7005d83af464ce8e3be3a7e866e2eeea84556798ef4bec731fcd6373d37d297f",
  22. "eb444928f1ec74a028ea8ee0a6edf7b2c850c2d7ec1e59d35f192b3584f71b567d899f1a4caf40045b611150fd052e24e761ee95c0574d8b9e48c8fccc27a811394561266c8b230927cc94d2587552dca58f70f7d22c93a2a2ade10d7ec5fda10df816c40181094cc8d6e8250a91e32c60fbcb44d85bd43bc3acdfc403055228357523c009d4e9c58545fc27b70c13d50e97328fdd47444f3a08705da18b3a91e2943acda899357c721c3308ac1a01d0c0111848dc383d3a5b0ef78d21676048817692eac10524ff367cabdb5a19878166e48225e5a057196e4dba820a957bbfc2a7188b4dba91e523212796eacd79162032cc0b71e85815d5a07ab53db79a52a8333fc6a652d22ba9e4dbb3a42635662c88879d81d6fab5bde0a53cdb1d70a6d723127bb05e6fe5c2d0c060f6ed5f7f602f5e9721a01d0ef94d068e043743d2c83ed89ca45b334cc58572de45de76afd3fc23534e9de56b243dab969958ea69d3ff4a433c1293ec6bf3c737af41447057b45bb74fa1ca01d21a80b986e2623759ca45d72127e213a149bf849b66d82009f729a6669470320e48db97de2bfb0a61c19ed2b5520cb9a4276cb13b57e45b159563d4ca85a288f95aef7fd7bd57af1a45be74b0af15b9790f882808c6e838a891cf8acc7788370c3f206591f41e5a947c99e3dc4609fd2024c843549de6a3d356abd8f66ecc9e62420826088fd62d8ab8641cb572f7d2b28c51fdedae881a73b0376191744074694f9a005c9787e75b3f5a45a6fe766889ec36f96fb6dcff24d5fcc90084118240fd7d5d743017c89d58f7f8a179ba7c89bb87ee3221793ece103afb9beca5e935d60159d1144b5fc4917bc791ea0d6c098163d1e0510f7bd14f483e46db4963ba4fc65dbeec1a04177b49c0f9986925bfa01dc82738ca9d23df8130d074841c9ed7f464f4970324a02a4dc8a2a5fd4a1b2a10ecfe1713ae7db9ee44d2ee1d1a6cf5501d0eafc57ddef19e9213c9feb4c7f6",
  23. "fe0c4f3ff8a035bf2cb3d9e7afa1b4b6c84cdcdda90a53c51c1d347c9bfc1b4c759186485de7494d536c5478f8032c3cb56ff38581534c91cd4ed9bdc775aa0f315f673776c42406388596d80a625fcaabcc5cf59e789dacbae1fd1f30c1fded15b010de05d3015c9cddf56c169bf56f68becb4ddb409831cff9c3c1570f40692f676fcd02d4e9ca894ff527bc0208d81fd87ba08e015e4f2d08710eb08d2ed8fec83af8e08f707a79512b05ba421ec1cb485742933b75321e17f9832b687c47c464d1f7c65339e32a72e8eb5811c29228e1d221ffaa4e192745a0934f8475b2d2a7038c51f48fa86d2135d3eed861533261990c39e8425182a133a52cb7970b977a3fd6e41ebb28ece4d2b4eb2b3e7669809196c2cdf5fab2fda52ddd086df2cd6b0461b85e73a0c3d6d760f6fc1a7c793552d925e11207b55247db217543c7d37b9e9eb3173e1f808972d207c76da896e032574e83ae6c2923eec48c59f074dfe3590a7907c8a053bbcf21be00566d54ad5de55aabdb1b841699a5d4e07f7e4bcd169a2173e919ef5ca89a9466d86309b021e0299238244b4ed0dacd28eb1e6c958597fa5743b8a52969ba2048e10f1d966482cec5e6e1f90ff873d6f448b440459979bcb846fa7f0cd8254dd3a128bf8acf92cb218b624233226990ba0b158f2fd8b4c74f12af23619c49568da6e7875daf94e92bc593244a47",
  24. "e8490c32f5ba70f23eaf9cebeaf5bfabc94ad2d1a30d428148143d3585f0484c738f934846e90c1e536d547be5093e3db57af580d5034d9c9e44d5a9c731e9093f4520377d9421092282d2dc446f43c7e2c15eb9913795a0a4e8e60d30d7faee0ce453d916c807568698e8634c92f82960f79f41d85b803ddef593c14d03177d387d39c91ec3ac87cc6ef663b4110f960ed462a78f4855412c4d6d1ead8933d2e8c675c2e493706a754e3a1dbc5212c6921c5041932068235b08b69c3b796f4ac272d1f1ce5339e32a72fffc4118c28123e49e3ce3b6094b5344bb940a817cb0d2ee1f814bb18cbd6a3b27c5bdcd60167362cb1a32fd5f129caa74e624a8db44b57f2093ab1e8133aefadfb2bf232a602c8d9d9d91d6e1afb0e7ec27dd5c6be094261c6db45168a0dfd7df25b7ea0b767b79569729f4160ee7015fc739310cc6cf3ecc98b7183a568b963acf0ad978a9d2e1775b4188e56e2421e8888958f672dfb1480d785ac6d46fb68a34b44c4a704cb84ae40eadd848cb1d91e0cbe87e3b469e57896473eb04e65aa4858623d7670df920f57dc624290e0dd9d6d430e6466a87dcd1fa5d0fb8a02b7aa7695de65b159765d6c7cef0e1f61ef97c82fc49be4e11a564a6eb15ec7309882808dbf334b99b9c8e8d629f724d243373dfb116518a398bb08f4702e038779c5d508ca2a3d356abd8e364c5962d580c2d5bcec93bcfbc775fa83cfbd1a28c4feba9ef9f0727b62a61847142782603be160b8683fb407d1d0ca6b1216291f03eb67ef7cef362dff6c811ca11ca46b26344693b1ad59a5ebbb5b062f27488bacaeb352b687b9e1e38f6cfe1affb299a135581074c4cc2d077c1d4f908735d8774caa54c492dd8555a3e0cdb706ebe53d01ba5f15f4b586648d1fed43a33b9ad0b8a7335c9873096873083658d0b94cdf47ef4c50d2aa27c49caa6e0e8501f6415e8bb4318e167b3aa02dfe7511674fe51074aa5d138d5bf9ac15ac9f4b983b1c041708f44764ddc335a261a3c2d7e86c39d379d30375f9214813fd5f44d03f406ed006750814146db9a4390176790389cc74fff7df0908a57a4eb02a6aae281adece97ece2d73d484c326f0046f4c57b38601ebda2236ab57b356167c54b316d7a4a11004906e66c0b1bc804dbe60800fd6f1df5ae9dd6853f8430b23d6a5e3cbded5a35a4ef9058d2c00fcf6f5d27b82cdfe6048267543293d72c5968fdd8a3dfc420834303a1829802cf343ac2c8feb7ff8d86ea72dac0a2650929500d8574bb03f13f58a6c43250e9cfad73da7279186a5abab540d44633f014f71784d4727bf91975c3cbb7a1a43490c992db868dcdc23f2e2ad7be4ebef885577ddbb57e86b53d0657b806d7a37e2c4cd65197d84c560a4aa2b6f7e5a67f757c14d4688a277511c1e1cafc161ab5789ef6997636ee439027df1",
  25. "e8444934b4bb70f23daf8be6afe8a1bc865e95cba91c16d2480e39628ffc494a65dd8b064daf4b1f55615f35fd052c26f07db1c1d6460480df51dffcc03ba6103c54643560c4220e6b989ad8473654c0e6df56ea973cdbacb0ade1077dd3ece910fe149613c81457c8dcee73098ce22a25a9da48d74a872b8cb8ddc40306176f24712ac202d5badacc52ec66bd0a15df0eca3ceedd755e4f350a765da5cc2ec8e08475c0a8863972731c2f01ba1a00dac00c1856d62b3d384c5ae2872b2b675ecc7594ec884261ab2c33e5ae4618928123f6973be3ef42027344ab9506d660b9c3ee1b8d50b0c2bb662c73d2f2c961533d7dcd5b39e84014d5b07ba739fb894ebf7d3cc0b91e8333adfcd3a5b26a286d69ce919c8cd1f0b3bce6f668d8126bf1d82e176eb41d7fefd5ca9a6db7fa1a3d29796e9633a01d0afb014bc1202916c7de3ed18ff60f3a568b9a3acc0cda71fbd7b220555d95e562396fe5919149fa769afd4408795494e563ff8a38ae53523f5aae18f041adda48c500d4b2c3e3743b59cd16906f3cfd1ae44baa8ec160d76e4cf22ba67c9535254b59d097de2be316709599c5b5500db3f73a60bd2759ac5b168c7e82d8c2f6a9f00fe230c9f348ad070baa31bda411b9640e88385dcaed32ae9f9b94db64816e0c3938749aa608478328d8a28f5d1fe2366bd00a5591a1e6874cab9ca92bd095370f0a3e46c9d27d84b7794be06bfedfb2c94aeca9e79e4276a02463996c4d792e19b70352d285ea407d1145bcb921669eef27ad29f78acd6cdce1df54c516ca51b5681272301dd2914ae8f8a463fb7785bd93a733262d349b0374f4d5e7bbf620de0745d105464fc3996ad687b80a705d816ed6a54c1929d8524f2f01947327ff54d71bbbeb0c5c17614f83e3dd7e2ebfb71dc42728c78b73c090278920881c91ccba63ef8c012bec654a98a5a8fd4c5a2553febd4514e164bce10190f9581d23f6461504b999389a9e918413cefabb83bacb5d7ed940384dda7b5e37403c157f84d59d2a86756358835c962898b80c0df80ee4496750890d52949740c20028967b9dc357f938bf90b157a3e20dbca2b18fe1fee065c13c73d288dd6ee105694657b2ca0ca3d23862a14ba604156718b60d8ef7a1341996742381beefc152bb60cf1cd0fd9150f4c23118e144192193a9edddded0a40952bc1ede694efffaedd67d90dcb062437f3c59673a63d88a98d6de3dff4a1e7a722e5d278934fc04ff08c1a96ef9d329f13abb083619d7de55c3",
  26. "eb44497af8a561b73fab8df0b8e4f7b6c01fd6cbb50842ce5b0e396585e01b507d8eca0909ec591f596b4466b1082423e161ef988f03778ddd55dfbfd279e9083611633d70963e0d67cc9adc593656c3fcce40ead22897a2afe8f6487196fbe417e401d70881125084ddab250e8be56f70b5cb45d50f8030c9f9f5c951144328017b3dc008909ec89e0fb96ebc130ec41fd87cbadd4553562501710da98933c5fec67bdcf88f316b73587b00b11a07c7db064c04da213d361e17f99d2b2b6659817b94eddb5339e22237e7f7141b83802eec9d3bb7ae490f2758a6820a907db4caaa4c8f4da287ad232e3cc4eacd6017737bd75b3cfc5519d5b07ba36da89a46be332ed2b31e9335ecffceb9ae387c767c8b919a83cefaa0b6f7a52cda0f67efc4271a67b44e35",
  27. "fe4a583fe6ec61ba28eabfecb8f2a3f9f150c7d5a85861c04e50787d82ee5e4e798fc6485de7450357771177f4072c3eb57af2c1c24b4586d94294fcfe06e926225c7972648a2948058d84c40a7945c8eac150e3932c92acb8febe4867d9eaea10fe149601cf14569addeb7c4c97ff6f76bedc5edc5bd878cebcd4c14d47436776792ec70990afdc8247f86ab40d15d7079973aa8b405843251e3e14aacc3ec3f4966ec3ef9831697e4575499b4f05dcdc0f1850db2a3d235613e49b276e7a0bc07995bece1c3fff2637f8ae555d849631a59034e4a6444b774dbe82588534b5cfaa4c8352a487a871683ad8bdd87a16737dc91e3fa95a1881a161a739ae894efb7237d7ea4d9730a9e2dbbdeb3e2e606d9a9b8087d1b3b5bdb3f120d65c77f3d621166aa51d6ce5c2dc9a75a3ee137a7a31529d6aa01c1ee1015cc628790fd2cf6adb8ff60c374d80d17cda17da71bec4b236544bd1a36c393be3818e0bfd61d2f843073c008ee527a0de33af4506705dfd4cff4be2df1ad05dd482dfa76e364f9e53956573e510a15ba58ec174d7724ce426e329922220055ed6c3d42be04672808f97f65e0ea7bb2b7cb16712d71200912acdc1cea2aff00ef772cef807bf1606a861a1a209f73c41dc214d8bf12eb892869e8d6d8463492237748aa61d158e3d9ce3cb4703eb7a24e842588ceae6df5dab88f162c6946258082c08adca3c9abd731c9374f7d0a8864deba9fe8c1262a76b2fa470492d0400bf025e9c98e148671343bcfe556f99ef20a027b8ccba57dcf0c811c71cca76a47e4665380a90d44ef3b1b67eba7a9cb98fe62e25697b871f74ebd3edecd820d60c00a20e504ad39d38e791fb0c71148b61cee074062ecf484f374892732bee049702ffbe5f69517c59d1b1cc7225fa970b876832cbce04d987399420bb188585b164ff800a65a57e0ccfb3b3bc5c1f271fe8a14419a761b0ee4890fb520a70f2411813ead5619abc969247ccf0b08d"
  28. };
  29.  
  30. //Since this is a stream cipher, we can decrypt characters one by one.
  31. unsigned char GetCtChar(int ctNumber, int pos)
  32. {
  33. //Just converting some part of these strings to char.
  34. string ct;
  35. unsigned char ctChar;
  36. ct = ciphertext[ctNumber].substr(pos * 2, 2);
  37. ctChar = (char)stoul(ct, nullptr, 16);
  38. return ctChar;
  39. }
  40.  
  41. //We are trying to guess a letter at the given position of the key. The assumption is that in one of the messages there would be a space on that position.
  42. unsigned char GetKeyLetter(int pos)
  43. {
  44. string ct[11];
  45. unsigned char ctChar[11], keyLetter = 255, ctXor[10][10] = {0};
  46. for (int ctNumber = 0; ctNumber < 10; ctNumber++)
  47. {
  48. ctChar[ctNumber] = GetCtChar(ctNumber, pos); //We take a character at the given position from every string.
  49. }
  50. for (int i = 0; i < 10; i++)
  51. {
  52. for (int j = 0; j < 10; j++)
  53. {
  54. if (i != j)
  55. {
  56. ctXor[i][j] = ctChar[i] ^ ctChar[j]; //Then xor these characters with one another.
  57. }
  58. }
  59. }
  60. bool valid = true;
  61. for (int i = 0; i < 10; i++) //A character from [a-zA-Z] xored with a space is just the same character in the inverted case.
  62. {
  63. valid = true;
  64. for (int j = 0; j < 10; j++) //So if there would be a space in some message, we would probably get characters from [a-zA-Z] or zeros in all other messages.
  65. {
  66. if (ctXor[i][j] == 0) //There could be two messages with a space in the same position.
  67. continue;
  68. else if (ctXor[i][j] > 96 && ctXor[i][j] < 123) //Range for [a-z] in ASCII.
  69. continue;
  70. else if (ctXor[i][j] > 64 && ctXor[i][j] < 91) //Range for [A-Z] in ASCII.
  71. continue;
  72. else
  73. {
  74. valid = false;
  75. break;
  76. }
  77. }
  78. if (valid) //If a space in some message convert other messages to [a-zA-Z].
  79. keyLetter = ctChar[i] ^ (int)' '; //We can use that message to get a letter of the key.
  80. }
  81. return keyLetter; //Otherwise return 255 as a default.
  82. }
  83.  
  84. int main()
  85. {
  86. string msg[10];
  87. unsigned char msgChar, keyLetter;
  88. //We can safely truncate all ciphertexts to the length of the target message.
  89. cout<<"Decrypting...";
  90. for (int pos = 0; pos < (int)(ciphertext[10].length() / 2); pos++) //Then decrypt letter by letter.
  91. {
  92. keyLetter = GetKeyLetter(pos); //Get a letter of the key
  93. for (int ctNumber = 0; ctNumber < 10; ctNumber++)
  94. {
  95. if (keyLetter == 255) //If the key was not found
  96. {
  97. msg[ctNumber].append(1, '_'); //Replace that character with a '_'
  98. continue;
  99. } //If we found a key
  100. msgChar = GetCtChar(ctNumber, pos) ^ keyLetter; //decrypt
  101. msg[ctNumber].append(1, msgChar); //and reconstruct the original message.
  102. }
  103. }
  104. for (int i = 0; i < 10; i++)
  105. {
  106. cout<<msg[i]<<endl; //Although some characters will be replaced with '_', we can easily get original messages from semantics.
  107. }
  108. return 0;
  109. }
Success #stdin #stdout 0.01s 5276KB
stdin
1
stdout
Decrypting...