arnold/book cipher with python

后端 未结 2 1533
被撕碎了的回忆
被撕碎了的回忆 2021-01-14 04:57

I\'m trying to write a book cipher decoder, and the following is what i got so far.

code = open("code.txt", "r").read() 
my_book = open(&q         


        
2条回答
  •  温柔的废话
    2021-01-14 05:27

    This may be quite a late answer; but better now than never I guess?

    I completed a book cipher implementation, that I would like to say; does exactly what you are asking after.

    • It takes a book file (in my example test run, at the bottom "Shakespeare.txt")
    • and a message (words)
    • finds the index of each words typed in, and gets the same words from that -> but in the book.
    • It prints out the book's-Words-Indexes.

    Give it a look? Hope it helps!

    I worked as crazy on this one. Took me, literally Years to complete this!

    Have a great day!

    I believe a answer with working code, or at least a try in that direction That's why I'm providing this code too; Really hope it helps both you & The future viewers!

    MAJOR EDIT:

    Edit 1: Providing code here, easier for the future viewers; and for you hopefully:

    Main Program:

    Originally from my GitHub: https://github.com/loneicewolf/Book-Cipher-Python

    I shortened it and removed stuff (that wasn't needed in this case) to make it more 'elegant' (& hopefully it became that too)

    # Replace "document1.txt" with whatever your book / document's name is.
    
    BOOK="document1.txt" # This contains your "Word Word Word Word ...." I believed from the very start that you meant, they are not the same - (obviously)
    
    # Read book into "boktxt"
    def GetBookContent(BOOK):
        ReadBook = open(BOOK, "r")
        txtContent_splitted = ReadBook.read();
        ReadBook.close()
        Words=txtContent_splitted
    
        return(txtContent_splitted.split())
    
    
    boktxt = GetBookContent(BOOK)
    
    words=input("input text: ").split()
    print("\nyou entered these words:\n",words)
    
    i=0
    words_len=len(words)
    for word in boktxt:
        while i < words_len:
            print(boktxt.index(words[i]))
            i=i+1
    
    x=0
    klist=input("input key-sequence sep. With spaces: ").split()
    for keys in klist:
            print(boktxt[int(klist[x])])
            x=x+1
    

    TEST ADDED:

    EDIT: I think I could provide a sample run with a book, to show it in action, at least.. Sorry for not providing this sooner: I executed the python script: and I used Shakespeare.txt as my 'book' file.

    input text: King of dragon, lord of gold, queen of time has a secret, which 3 or three, can hold if two of them are dead

    (I added a digit in it too, so it would prove that it works with digits too, if somebody in the future wonders)

    and it outputs your book code:

    27978 130 17479 2974 130 23081 24481 130 726 202 61 64760 278 106853 1204 38417 256 8204 97 6394 130 147 16 17084
    

    For example:

    27978 means the 27978'th word in Shakespeare.txt

    To decrypt it, you feed in the book code and it outputs the plain text! (the words you originally typed in)

    input key-sequence sep. With spaces: 27978 130 17479 2974 130 23081 24481 130 726 202 61 64760 278 106853 1204 38417 256 8204 97 6394 130 147 16 17084

    -> it outputs ->

    King of dragon, lord of gold, queen of time has a secret, which 3 or three, can hold if two of them are dead

    //Wishes William.

提交回复
热议问题