#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Help with encoding


    I made a substitution cipher which generates random dictionary with which the plaintext characters are replaced.

    here's the code :-

    Code:
    import random
    
    alphabets=' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+|:"<>-=[];,.?'
    class HCPyEncoder:    
        def encode(self,plaintext):       
            length=len(alphabets)
            arr=range(0,length)
            random.shuffle(arr)
            pad,cipher="",""
            dictionary={}        
            for i in range(0,length):
                pad=pad+alphabets[arr[i]]
                dictionary[alphabets[i]]=alphabets[arr[i]]
            for i in plaintext:
                if i in dictionary:
                    i=dictionary[i]
                cipher=cipher+i
            return cipher,pad
        
        def decode(self,ciphertext,pad):
            dictionary={}
            plaintext=""
            for i in range(len(pad)):
                dictionary[pad[i]]=alphabets[i]
            
            for i in ciphertext:
                if i in dictionary:
                    i=dictionary[i]
                plaintext=plaintext+i
            return plaintext
    
    encoder=HCPyEncoder()
    ciphertext,pad=encoder.encode("Psycho Coder")
    plaintext=encoder.decode(ciphertext,pad)
    print "Ciphertext : ",ciphertext
    print "Plaintext : ",plaintext
    You can have a look at it here :- http://codepad.org/ZquaqYsi

    But the problem is everytime it generates a new key or pad, so if the secret message is sent to someone then it cannot the recovered.

    So I want to give it a fixed substitution dictionary and not random which will be used for generating every cipher text.

    Please help I am a beginner at python so please help with the code which I want now.

    Thank you
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    Right after importing random, set the random seed.

    random.seed(a=hash('messages to Alice'))

    This way you could have a known secret code on a per-person basis.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Right after importing random, set the random seed.

    random.seed(a=hash('messages to Alice'))

    This way you could have a known secret code on a per-person basis.
    Sir I do not understand will you please explain with code
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    The point is: you can set the random seed. Here I've used the seeds 1 and 2. All the rest is the same.
    Code:
    >>> random.seed(1); a = list(range(5)) ; random.shuffle(a); print(a)
    [1, 4, 2, 3, 0]
    >>> random.seed(2); a = list(range(5)) ; random.shuffle(a); print(a)
    [1, 2, 0, 3, 4]
    >>> random.seed(1); a = list(range(5)) ; random.shuffle(a); print(a)
    [1, 4, 2, 3, 0]
    >>> random.seed(2); a = list(range(5)) ; random.shuffle(a); print(a)
    [1, 2, 0, 3, 4]
    >>> random.seed(2); a = list(range(5)) ; random.shuffle(a); print(a)
    [1, 2, 0, 3, 4]
    >>> random.seed(2); a = list(range(5)) ; random.shuffle(a); print(a)
    [1, 2, 0, 3, 4]
    >>>
    hash merely showed how to convert a memorable string to a seed. And, I used hash explicitly because the result of random.seed seems to depend on the python version and arguments you supply. hash returns an integer.

    >>> hash('messages to Alice')
    6351042976021034634
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo