Thread: What works???

    #1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11

    What works???


    Okay hi all

    I made a encoder that encodes letters and symbols into {'a':'1|'}
    like that.

    You see the encoder and the file in the uploaded stuff.

    Okay well...

    I have a file like this.

    test.txt:
    Code:
    I don't want to be hit.
    ==

    test.dle:
    Code:
    40|  | 4|  15| 14| '| 20|  | 23| 1|  14| 20|  | 20| 15|  | 2|  5|   | 8|  10| 20| .|
    How can I convert the (1|...) back into letters.
    I read the file(that works) then I try to convert, butit does not work with the for loop, because there are several characters. With if statements it does not work either because that checks the whole file for 1| and the 2| and returns a True or False and does not return a letter in the same position.

    If you look at the encoder you will understand.
    Attached Files
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    1) Why are you writing your own encoder instead of using an already written one?

    2) If you are writing one, why all the "a| " stuff instead of using string.translate()?

    3) This is just character substitution, and therefore no more secure than rot13, why not use that?

    Code:
    text = "abcde"
    text.encode("rot13")
    4) To actually answer your question ( ), split the text by "|" since all characters have that in them, trim the whitespace, add the "|" back on and run it through your encoder backwards. Which you've made very difficult by the cumbersome coding...

    Turn the map into a dictionary:

    Code:
    import os
    
    encode = raw_input("Which file would you like to encode to DLE?")       # Ask file
    
    if not os.isfile(encode + ".txt"):
        print "There is no file called ", encode + ".txt"
        break
    
    map = {
        'a':"1|  ", 'b':"2|  ", 'c':"3|  ", 'd':"4|  ", 'e':"5|  ", 'f':"6|  ", 'g':"7|  ", 'h':"8|  ",
        'j':"9|  ", 'i':"10| ", 'k':"11| ", 'l':"12| ", 'm':"13| ", 'n':"14| ", 'o':"15| ", 'p':"16| ",
        'q':"17| ", 'r':"18| ", 's':"19| ", 't':"20| ", 'u':"21| ", 'v':"22| ",'w':"23| ", 'x':"24| ", 
        'y':"25| ", 'z':"26| ", 'A':"31| ", 'B':"32| ", 'C':"33| ", 'D':"34| ", 'E':"35| ", 'F':"36| ",
        'G':"37| ", 'H':"38| ", 'J':"39| ", 'I':"40| ", 'K':"41| ", 'L':"42| ", 'M':"43| ", 'N':"44| ", 
        'O':"45| ", 'P':"46| ", 'Q':"47| ", 'R':"48| ", 'S':"49| ", 'T':"50| ", 'U':"51| ", 'V':"52| ",
        'W':"53| ", 'X':"54| ", 'Y':"55| ", 'Z':"56| ", ' ':" | ", ',':",| ", '.':".| ", ':':":| ", ';':";| ", '\n':"\| ",
        '<':"<| ", '>':">| ", '-':"-| ", '_':"_| ", '?':"?| ", '$':"$| ", '}':"}| ", '{':"{| ", '!':"!| ", ']':"]| ",
        '[':"[| ", '^':"^| ", "'":"'| ", '"':'"| ', '+':"+| ", '@':"@| ", '*':"*| ", '#':"#| ", '%':"%| ",
        '&':"&| ", '/':"/| ", '(':"(| ", ')':")| ", '=':"=| ", '':"| ", '`':"`| ", '~':"~| ", '|':"|| ",
        '':"| ", '':"| ", '':"| ", '':"| ", '':"| ", '':"| ", '':"| ", '':"| ", '':"| ",
        '':"| ", '':"| ", '':"| ", '':"| ", '':"| " 
    }
    
        codedfile = open(encode + ".dle", "w")
    
        for line in file(encode + ".txt"):
            cipherline = []
            for char in line:
                cipherline.append(map[char])
            
            otherfile.write(''.join(cipherline) + '\n')
    
        opened.close()
        otherfile.close()
    Then decryption is a matter of swapping keys/values in the dictionary.

    Code:
    decode_map = dict([(y, x) for (x, y) in map.iteritems()])
    would do that.
    Last edited by sfb; March 27th, 2005 at 08:52 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    Thanks !!!

IMN logo majestic logo threadwatch logo seochat tools logo