0

Polyalphabetic Cipher

Now I’m working on programming a Polyalphabetic cipher. (I moved into a cryptography phase) The big issue is the actual shifting part, as if the length of the message doesn’t evenly divide into groups that are the length of the shift word. For example, if the message was “FREE PIZZA IN THE CAFETERIA”, and the shift word was “COOKIE”, then the shifts would be “COOK IECOO KI ECO OKIECOOKI”. (Cutting off at the end) https://sololearn.com/compiler-playground/ckDWZ76AjDT9/?ref=app

17th May 2024, 9:35 PM
Annihilate
Annihilate - avatar
10 odpowiedzi
+ 3
CONTINUE (2/2) To break it down: lst[i] = alphabet[lst[i]] We already know variable lst is a string "COOK IECOO KI ECO OKIECOOKI", and "i" is an integer. What is the value of "alphabet[lst[i]]"? lst[i] returns a string (character), and "alphabet[string]" is not possible. (END)
19th May 2024, 2:23 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 5
i have done and posted a similar task 2 years ago. the iteration of the `key` variable is done with an *infinite iterator* by using the `cycle()` function from the python `itertools`. if the iterator reaches the end of the `key` variable it does not exhaust, but starts again the next iteration from the beginning. see the code how it works: https://sololearn.com/compiler-playground/cBj3eS3i13DB/?ref=app
19th May 2024, 7:10 PM
Lothar
Lothar - avatar
+ 2
This maybe the method you are thinking of... 1. determine the length of the message 2. multiply the key, so the length is equal to or longer than the message In your example "FREE PIZZA IN THE CAFETERIA", the length is 27, and the key is "COOKIE" (length = 6). To "extend" the key, we can multiple the key by (27 // 6) + 1 = 5 Key become "COOKIECOOKIECOOKIECOOKIECOOKIE" (length = 30) 3. Create an empty string to store the final key 4. Loop the message for each character, take the first letter in the "temp key", put it into the "final key" string, then delete the first character from the "temp key". 4a. If the message character you are looping is a space, add a space to your "final key" Then you get your "final key" COOK IECOO KI ECO OKIECOOKI An alternative but advance method, use the itertools library. There is a "cycle" in that library. Basically it is a generator, which can keep outputting in a loop. https://sololearn.com/compiler-playground/chWh5ULks81Y/?ref=app
18th May 2024, 1:47 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Annihilate, you should start to learn how to read the error message. I run the program with the following inputs FREE PIZZA IN THE CAFETERIA COOKIE And I got this: Traceback (most recent call last): File "./Playground/file0.py", line 61, in <module> main() File "./Playground/file0.py", line 51, in main enc_lst = num_to_let(shifted_lst) File "./Playground/file0.py", line 41, in num_to_let lst[i] = alphabet[lst[i]] TypeError: list indices must be integers or slices, not str It says when running line 61 (which is main()), which encounter an error in line 51, where it encounter an error in line 41 (which is lst[i] = alphabet[lst[i]]) The error is a TypeError, list indices must be integers or slices, not str. That is the same issue you faced in the previous post. (1/2)
19th May 2024, 2:21 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
It would be helpful if you give us the input and output. If I'm not mistaken... Input: FREE PIZZA IN CAFETERIA Output: HSFO XMBNO SV XJS QKNIVSFSI And we have a lesson in here too. Community > By The Community > Cryptography > Vigenere cipher https://www.sololearn.com/learn/10366/?ref=app
18th May 2024, 1:29 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Annihilate your idea of not basing the encryption on ascii table but on custom list is interesting, so I modified the codes I posted to do something similar. I just joined the list into a string but it is functionally the same. I also extended the list to include uppercase, lowercase, numerals and whitespace. You can add additional characters if you want. https://sololearn.com/compiler-playground/cmDoTywr1ZyJ/?ref=app
19th May 2024, 4:11 PM
Bob_Li
Bob_Li - avatar
+ 1
Wong Hei Ming Got it, thanks!
19th May 2024, 4:54 PM
Annihilate
Annihilate - avatar
0
Wong Hei Ming #this is what I have so far: import math def shift(lst, word): lst_len=len(lst) word_len=len(word) shifts=math.ceil(lst_len/word_len) temp_key=word*shifts final_key="" for i in range(lst_len): if lst[i]==" ": final_key+=" " else: final_key+=temp_key[0] temp_key=list(temp_key) temp_key.pop(0) temp_key="".join(temp_key) return final_key #this will be changed by the time I figure out the actual shifting part
18th May 2024, 6:04 PM
Annihilate
Annihilate - avatar