2

i was going through one of the python recipes in active state and found the following code. I've come up with the following 4 questions. Will be very grateful for any guidance and explanations.

Q::Im unable to exactly figure out as to why is a "key-random seed" taken from the user at the first place in line "k=long(sys.argv[2])" ? especially since, this value 'k' doesnt seemed to be used in the later part of the code ?

Secondly, in the part of the code for encryption and decryption, what is the exact implication of the statement "bytearray[i]-random.randint(0,255)%256" ? does this mean that the unicode values of each character is shifted or displaced during encryption and re-shifted back to its original value when decrypted ?

Thirdly, does the "bytearray= map(ord, f1.read())" compute the unicode point values of every character in the file ?

Lastly, Since random numbers are used in the encryption and decryption, what factor guarantees that the decryption of the encrypted file will be accurate ? has the seed value "k" got anything to do with this ?

Below is the code that i'm studying.

Will greatly appreciate your guidance,Thanks in advance

# encdec.py

import sys
import random

if len(sys.argv) != 5:
    print "Usage: encdec.py e/d longintkey [path]filename1 [path]filename2"
    sys.exit()

k = long(sys.argv[2]) # key
random.seed(k)

f1 = open( sys.argv[3], "rb")
bytearr = map (ord, f1.read () )
f2 = open( sys.argv[4], "wb" )

if sys.argv[1] == "e": # encryption

    for i in range(len(bytearr)):
        byt = (bytearr[i] + random.randint(0, 255)) % 256
        f2.write(chr(byt))


if sys.argv[1] == "d": # decryption

    for i in range(len(bytearr)):
        byt = ((bytearr[i] - random.randint(0, 255)) + 256 ) % 256
        f2.write(chr(byt))

f1.close()
f2.close()

1 Answer 1

6

random.seed(k) will set the random number generator to some state, that it will deliver the same list of random numbers for the same k.

import random
print [random.randint(0,255) for i in xrange(5)]

returns different numbers each time I run the script.

import random
random.seed(2)
print [random.randint(0,255) for i in xrange(5)]

returns [244, 242, 14, 21, 213] each time I run the script.

That's why if you seed the random numbers generator with a specific value of k, it will give you a very specific list of random numbers which can be used to encode the given file and then decode it.

Second question - yes. It works with one byte at a time, so 256 different values.

Third question - yes, it computes the integer ordinal of a one-character string each time. It is the same like bytearr = [ ord(c) for c in f1.read() ] - read a character at a time and create a list of their values (0 - 255).

Lastly - See above.

And just a pythonic hint for the two loops... if you iterate over an array, you can use

for c in bytearr:

and then use c instead of

for i in range(len(bytearr)):

and using bytearr[i].

So, for example:

for c in bytearr:
    byt = (c + random.randint(0, 255)) % 256
    f2.write(chr(byt))

Your question: when i open the decrypted file using pico, why is that im seeing the strange characters ? shouldn't they be just some random alphabets and numbers ?

No. Look here. There are 2*26=52 letters, 10 digits, some more printable characters and some not printable characters (newline, tab,...). You encode/decode on the 'decimal' value (0-255) and your pico shows you the characters in the 'char' column.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for speed! Only thing I'd add is that unicode has nothing to do with it: the file's opened in binary mode and processed as such.
Thanks eumiro ! that solves most of my doubts. Im still unclear about what does the "bytearr = map (ord, f1.read () )" actually do ?
Thanks @DSM !One more little thing..when i open the decrypted file using pico, why is that im seeing the strange characters ? shouldn't they be just some random alphabets and numbers ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.