Last active
July 30, 2017 22:43
-
-
Save back-seat-driver/9a49faabb12b9ce133458908f00d4037 to your computer and use it in GitHub Desktop.
Image_Encryption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
import numpy as np | |
import hashlib | |
import scipy.misc | |
def S_L(string_name_list,LIST): | |
f = open(string_name_list + '.py', 'w') | |
simplejson.dump(LIST,f) | |
f.close() | |
def L_P(SET,n): | |
#Splits an iterable object into n len chunks | |
to_return=[] | |
j=0 | |
k=n | |
while k<len(SET)+1: | |
to_return.append(SET[j:k]) | |
j=k | |
k+=n | |
return(to_return) | |
def FILTER(a_string): | |
to_return='' | |
for i in range(len(a_string)): | |
if a_string[i]!=' ' and a_string[i]!='[' and a_string[i]!=']': | |
to_return+=a_string[i] | |
return(to_return) | |
def RETURN_FILE_LIST(str_name): | |
#Only works with integers will have to change per contents | |
#of particular saved file | |
contents=open(str_name + '.py').read().split(',') | |
to_return=[] | |
for i in range(len(contents)): | |
to_return.append(int(FILTER(contents[i]))) | |
return(to_return) | |
def xo(bit_string_1,bit_string_2): | |
xor_str='' | |
for i in range(len(bit_string_1)): | |
if bit_string_1[i]=='0' and bit_string_2[i]=='0': | |
xor_str+='0' | |
if bit_string_1[i]=='1' and bit_string_2[i]=='1': | |
xor_str+='0' | |
if bit_string_1[i]=='0' and bit_string_2[i]=='1': | |
xor_str+='1' | |
if bit_string_1[i]=='1' and bit_string_2[i]=='0': | |
xor_str+='1' | |
return(xor_str) | |
def to_print(object): | |
for i in range(len(object)): | |
print(object[i]) | |
def bin_n_bit(dec,n): | |
return(str(format(dec,'0'+n+'b'))) | |
def list_concat(list_of_lists): | |
to_return=[] | |
for i in range(len(list_of_lists)): | |
to_return+=list_of_lists[i] | |
return(to_return) | |
def str_concat(list_of_strings): | |
to_return='' | |
for i in range(len(list_of_strings)): | |
to_return+=list_of_strings[i] | |
return(to_return) | |
def array_to_list(a_array): | |
#Function will take a nested numpy array and return and convert | |
#each element of the numpy array to a python list type | |
to_return=[] | |
for i in range(len(a_array)): | |
to_return.append(a_array[i].tolist()) | |
return(to_return) | |
def image_mount(file_string_name): | |
return(Image.open(file_string_name + '.png').convert('L')) | |
def itl(file_string_name): | |
return(list(np.array(Image.open(file_string_name + '.png').convert('L')))) | |
def lti(a_list): | |
return(Image.fromarray(np.array(a_list))) | |
def image_dim(image_list): | |
#Function will take and image and return the dimensions of the photo | |
#in pixel locations. | |
return(len(image_list),len(image_list[0])) | |
def int_list(a_image_str_name): | |
#Function will take a .png image and convert the image to a set | |
#of pixel integers | |
to_return=[] | |
to_iter=list_concat(array_to_list(itl(a_image_str_name))) | |
for i in range(len(to_iter)): | |
to_return.append(to_iter[i]) | |
return(to_return) | |
def int_list_bin(int_list): | |
#Function will take an int list and convert each list value to its | |
#binary representaion. | |
for i in range(len(int_list)): | |
int_list[i]=bin_n_bit(int_list[i],'8') | |
return(int_list) | |
def encryption_image_save(image_str_name): | |
image_list=RETURN_FILE_LIST(image_str_name) | |
image_list_split=L_P(image_list,image_list[-1]) | |
list_of_arrays=[] | |
for i in range(len(image_list_split)): | |
list_of_arrays.append(np.asarray(image_list_split[i])) | |
array_main=np.asarray(list_of_arrays) | |
scipy.misc.toimage(array_main).save(image_str_name + '_encryption' + '.png') | |
def image_encryption(password_str,image_str_name): | |
key_to_build='' | |
int_pic_list=int_list_bin(int_list(image_str_name)) | |
split_len=image_dim(array_to_list(itl(image_str_name)))[-1] | |
while len(key_to_build) < (len(int_pic_list))*2: | |
insert=hashlib.sha512(password_str.encode('utf-8')).hexdigest() | |
#password_str.encode('utf-8') | |
key_to_build+=insert | |
password_str=insert | |
to_xor=L_P(key_to_build,2) | |
for i in range(len(int_pic_list)): | |
int_pic_list[i]=int(xo(int_pic_list[i],bin_n_bit(int(to_xor[i],16),'8')),2) | |
int_pic_list.append(split_len) | |
image_list_split=L_P(int_pic_list,int_pic_list[-1]) | |
list_of_arrays=[] | |
for i in range(len(image_list_split)): | |
list_of_arrays.append(np.asarray(image_list_split[i])) | |
array_main=np.asarray(list_of_arrays) | |
scipy.misc.toimage(array_main).save(image_str_name + '_encryption' + '.png') | |
def image_decryption(password_str,image_str_name): | |
key_to_build='' | |
encryption_list=int_list_bin(int_list(image_str_name)) | |
split_len=image_dim(array_to_list(itl(image_str_name)))[-1] | |
while len(key_to_build) < len(encryption_list)*2: | |
insert=hashlib.sha512(password_str.encode('utf-8')).hexdigest() | |
key_to_build+=insert | |
password_str=insert | |
to_xor=L_P(key_to_build,2) | |
for i in range(len(encryption_list)): | |
encryption_list[i]=int(xo(encryption_list[i],bin_n_bit(int(to_xor[i],16),'8')),2) | |
encryption_list.append(split_len) | |
image_list_split=L_P(encryption_list,encryption_list[-1]) | |
list_of_arrays=[] | |
for i in range(len(image_list_split)): | |
list_of_arrays.append(np.asarray(image_list_split[i])) | |
array_main=np.asarray(list_of_arrays) | |
scipy.misc.toimage(array_main).save(image_str_name + '_decryption' + '.png') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment