This month I have finished with my Python course in HackBulgaria, thus I have decided to continue with a course, called Algorithms.
The course had a requirement of three tasks, which should be resolved in a reasonable time. The first one is a little too trivial to be presented here, but the other two took me a reasonable time, thus I have decided to write about them. So, let’s start with the second one, which requires the making of an algorithm, that decrypts hidden messages into something meaningful.
The full task is published here.
At first, when I saw it I thought that it would be rather easy to do it, but it was not the case. I started to make the algorithm to decrypt the message and after some hour or so I was ready (a lot of time, indeed, but I was also testing and drinking beer). Also, I did not get from the first time the idea for the alphabet word, I tought that I should go to the ASCII table instead.
Once, I was ready with the encryption, I started to decrypt. It was a little easier, until the moment, when I had the resulting index (“hevrItommseIal vi”) and the indices of the key (“12, 6, 3, 7, 12, 6, 3, 7, 12, 6, 3, 7, 12, 6, 3, 7, 12”). With these two I had to build the sum of the key with the original message. It seemed tough, because there was a remainder in the whole story and I was not sure where to apply the whole number. So, I simply disregarded it (Never do this at work!) 🙂 and I noticed that the results were coming negative in the positions, where the remainder had to be applied. Thus, with one “if” I managed to build up my decryption machine.
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 |
def start_program(myInput, alphabet, encryptionKeyOriginal): encryptionKey = generate_key(len(myInput), encryptionKeyOriginal) myInputIndices = generate_indices(alphabet, myInput) myEncryptionIndices = generate_indices(alphabet, encryptionKey) mySumedIndices = [ x + y for x, y in zip(myInputIndices, myEncryptionIndices)] myRemainderIndices = [x % len(alphabet) for x in mySumedIndices] mapped = map_index_to_alhpa_char(myRemainderIndices, alphabet) message = str(len(alphabet)) + "~" + alphabet + mapped + \ encryptionKeyOriginal + "~" + str(len(encryptionKeyOriginal)) finalResult = swap(message) print(finalResult) def map_index_to_alhpa_char(myRemainderIndices, alphabet): result = [] for x in range(0, len(myRemainderIndices)): position = int(myRemainderIndices[x]) result.append(alphabet[position]) result = "".join(str(x) for x in result) return result def generate_indices(alphabet, inputString): result = [] for x in range(0, len(inputString)): myInt = alphabet.index(inputString[x]) result.append(myInt) return result def generate_key(myInputLen, encryptionKey): counter = 0 producedKey = encryptionKey while myInputLen > len(producedKey): producedKey += producedKey[counter] if counter < len(producedKey): counter += 1 return producedKey def swap(myInput): cut_at = int(len(myInput) / 2) return myInput[(-1) * cut_at:] + myInput[:cut_at] def start_reverse(myMessage): step1 = swap(myMessage) step2 = step1.split("~") lenAlphabet = int(step2[0]) rest = step2[1] lenKey = int(step2[2]) alphabet = rest[:lenAlphabet] key = rest[(-1) * lenKey:] encryptedMessage = rest[lenAlphabet:] encryptedMessage = encryptedMessage[:lenKey * (-1)] keyEncription = generate_key(len(encryptedMessage), key) keyIndexList = generate_indices(alphabet, keyEncription) encryptedMessageList = generate_indices(alphabet, encryptedMessage) aResult = [x - y for x, y in zip(encryptedMessageList, keyIndexList)] for x in range(0, len(aResult)): if aResult[x] < 0: aResult[x] += lenAlphabet aResult = map_index_to_alhpa_char(aResult, alphabet) print(aResult) originalmessage = "TJKUMbUUJTIREuKOXD'HR.sTOVSWU!KSJ(O.sVYtWZTTZVULsNOBdYONXFsvEu(PgWJsRTSVsYKOfDZOJSNVWu(IU!yAaMs?OW.tYaVET.A IQXTMQURJ.HLs'VHa'OTYUSzCQ!SePzsuMTzYQ!SM!NOdOH SuPMa)yA!LsKOPEUM,VAaMs.SuKOkDJEcIIStHACKBULGARIA~1260~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .',!?()rEPNtg,yTYMsJOFOtkZ sd EKsVYtXPIOUMs HK't(PYSROEAsq.JfPyAJ HVRVCUYaZsPITzMQ'UMZTEJXANEBCUYWRI!Os.Su(IkD!OdADLCKNFXDZOJ THRVCQdZMRRUMPNIDtISGTJQSz" # start_program("I love algorithms", "Ilocveakgrithms ", "hack") # start_program("this is a secret.", "thisaecr .", "rest") # start_program("I love freckles.", " .Ifrckslove", "kv.") # start_program("is this fun or what?", "orwa? thfuisn", "wat?") start_reverse(originalmessage) # Ilocveakgrithms hevrItommseIal vihack |
Enjoy it! 🙂