()
| 1 | def crypt(): |
| 2 | import OpenImageIO as o |
| 3 | import array |
| 4 | import random |
| 5 | import pickle |
| 6 | |
| 7 | path = raw_input("Enter OIIO plugin path: ") |
| 8 | in_file = raw_input("Enter the name of the file for encryption: ") |
| 9 | out_file = raw_input("Enter the name of the resulting file: ") |
| 10 | key_path = raw_input("Enter the name of the file in which to store the key: ") |
| 11 | |
| 12 | # open the input file |
| 13 | spec = o.ImageSpec() |
| 14 | pic = o.ImageInput.create(in_file, path) |
| 15 | pic.open(in_file, spec) |
| 16 | desc = spec.format |
| 17 | |
| 18 | # Create a couple of arrays where we'll store the data. |
| 19 | # They all need to be the same size, and we'll fill them with dummy data for now. |
| 20 | # We'll read the original pixel values in arr |
| 21 | arr = array.array("B", "\0" * spec.image_bytes()) |
| 22 | # the values we'll write to the encrypted file |
| 23 | new_values = arr[:] |
| 24 | length = range(len(new_values)) |
| 25 | print "Working, please wait..." |
| 26 | pic.read_image(desc, arr) |
| 27 | |
| 28 | # save the state of the random number generator so we can use it |
| 29 | # to decode the image |
| 30 | state = random.getstate() |
| 31 | |
| 32 | # generate random values, add them to the original values. |
| 33 | # Do % 256 so nothing overflows |
| 34 | for i in length: |
| 35 | rand_val = random.randint(0, 255) |
| 36 | new_values[i] = (arr[i] + rand_val) % 256 |
| 37 | |
| 38 | # write new values to the output file, close everything |
| 39 | out = o.ImageOutput.create(out_file, path) |
| 40 | out.open(out_file, spec, False) |
| 41 | out.write_image(desc, new_values) |
| 42 | out.close() |
| 43 | # save the state of the RNG - that's the key for decryption |
| 44 | f = open(key_path, "w") |
| 45 | pickle.dump(state, f) |
| 46 | f.close() |
| 47 | return True |
| 48 | |
| 49 | |
| 50 | def decrypt(): |
nothing calls this directly
no test coverage detected