Welcome, guest | Sign In | My Account | Store | Cart

File encryption/decryption using stream cipher. It can encrypt/decrypt any type of file.

Python, 33 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# encdec.py
# File encryption/decryption using stream cipher
# FB - 201003066

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()

5 comments

FB36 (author) 11 years, 9 months ago  # | flag

Here is an even shorter version:

import sys
import random

if len(sys.argv) != 4:
    print "Usage: encdec_.py longintkey [path]filename1 [path]filename2"
    sys.exit()  
random.seed(long(sys.argv[1])) # key
f1 = open( sys.argv[2], "rb")
bytearr = map (ord, f1.read () )
f2 = open( sys.argv[3], "wb" )
for i in range(len(bytearr)):
    f2.write(chr(bytearr[i] ^ random.randint(0, 255)))
f1.close()
f2.close()
Robert Kern 11 years, 8 months ago  # | flag

One time pads must be generated via a real random process, not a pseudorandom number generator. The way you use the PRNG here would be classified as a stream cipher, not a one time pad. However, the PRNG used by Python has linearities that can be used to determine the state of the generator. It is clearly documented as not being suitable for cryptographic use. This approach is vulnerable to many attacks and should not be considered at all secure.

FB36 (author) 11 years, 8 months ago  # | flag

All true. What I really meant was the way, how enc/dec of each byte is done, is like one-time pad. True implementation of one-time would require asking user a random string/file that is the same size as the file to enc/dec, which is quite impractical. Of course I would not want anyone to use this code for serious encryption needs also. Thanks.

FB36 (author) 11 years, 8 months ago  # | flag

Actually I think the encryption strength can easily be increased (exponentially!) by encrypting the same file multiple times using a different key everytime. (For decryption, the keys must be applied in reverse order of course.) (Also it surely would be better to use a new random number generator algorithm that is specifically designed for cryptography.)

esaul 11 years, 7 months ago  # | flag

I found that your code must be changed a little in Python 3.1.2, just so:

import sys
import random

if len(sys.argv) != 4:
    print("Usage: encdec2.py longintkey [path]filename1 [path]filename2")
    sys.exit()  
bytw = bytearray(1)
random.seed(int(sys.argv[1])) # key
f1 = open( sys.argv[2], "rb")
bytearr = f1.read ()
f2 = open( sys.argv[3], "wb" )
for i in range(len(bytearr)):
    bytw[0] = bytearr[i] ^ random.randint(0, 255)
    f2.write(bytw)
f1.close()
f2.close()