APPENDICES
APPENDICES
Importing Liberaries
from PIL import Image
import numpy as np
import os
from matplotlib.pyplot import imshow
import matplotlib.pyplot as plt
import cv2
import random
def getImageMatrix_gray(imageName):
im = Image.open(imageName).convert('LA')
pix = im.load()
image_size = im.size
image_matrix = []
40
for width in range(int(image_size[0])):
row = []
for height in range(int(image_size[1])):
row.append((pix[width,height]))
image_matrix.append(row)
return image_matrix, image_size[0], image_size[1]
for i in range(1,4):
s=0
for j in range(1,5):
s += G[i-1][j-1] * (10**(-j))
g.append(s)
R = (R*s) % 1
L = (R + key_list[12]/256) % 1
S_x = round(((g[0]+g[1]+g[2])*(10**4) + L *(10**4)) % 256)
V1 = sum(key_list)
V2 = key_list[0]
for i in range(1,13):
V2 = V2 ^ key_list[i]
V = V2/V1
print(key_list)
print(g)
41
L_y = (V+key_list[12]/256) % 1
S_y = round((V+V2+L_y*10**4) % 256)
C1_0 = S_x
C2_0 = S_y
C = round((L*L_y*10**4) % 256)
C_r = round((L*L_y*10**4) % 256)
C_g = round((L*L_y*10**4) % 256)
C_b = round((L*L_y*10**4) % 256)
x = 4*(S_x)*(1-S_x)
y = 4*(S_y)*(1-S_y)
42
else:
C = ((key_list[4]+C1) % N) ^ ((key_list[5]+C2) % N) ^
((key_list[6]+imageMatrix[i][j]) % N) ^ ((C + key_list[7]) % N)
row.append(C)
pix = im.load()
for x in range(dimensionX):
for y in range(dimensionY):
pix[x, y] = LogisticEncryptionIm[x][y]
im.save(imageName.split('.')[0] + "_LogisticEnc.png", "PNG")
43
for i in range(1,4):
s=0
for j in range(1,5):
s += G[i-1][j-1] * (10**(-j))
g.append(s)
R = (R*s) % 1
L_x = (R + key_list[12]/256) % 1
S_x = round(((g[0]+g[1]+g[2])*(10**4) + L_x *(10**4)) % 256)
V1 = sum(key_list)
V2 = key_list[0]
for i in range(1,13):
V2 = V2 ^ key_list[i]
V = V2/V1
L_y = (V+key_list[12]/256) % 1
S_y = round((V+V2+L_y*10**4) % 256)
C1_0 = S_x
C2_0 = S_y
C = round((L_x*L_y*10**4) % 256)
I_prev = C
I_prev_r = C
I_prev_g = C
I_prev_b = C
I=C
I_r = C
I_g = C
I_b = C
x_prev = 4*(S_x)*(1-S_x)
y_prev = 4*(L_x)*(1-S_y)
x = x_prev
y = y_prev
44
imageMatrix,dimensionX, dimensionY, color = getImageMatrix(imageName)
henonDecryptedImage = []
for i in range(dimensionX):
row = []
for j in range(dimensionY):
while x <0.8 and x > 0.2 :
x = 3.99*x*(1-x)
while y <0.8 and y > 0.2 :
y = 3.99*y*(1-y)
x_round = round((x*(10**4))%256)
y_round = round((y*(10**4))%256)
C1 = x_round ^ ((key_list[0]+x_round) % N) ^ ((C1_0 + key_list[1])%N)
C2 = x_round ^ ((key_list[2]+y_round) % N) ^ ((C2_0 + key_list[3])%N)
if color:
I_r = ((((key_list[4]+C1) % N) ^ ((key_list[5]+C2) % N) ^ ((I_prev_r +
key_list[7]) % N) ^ imageMatrix[i][j][0]) + N-key_list[6])%N
I_g = ((((key_list[4]+C1) % N) ^ ((key_list[5]+C2) % N) ^ ((I_prev_g +
key_list[7]) % N) ^ imageMatrix[i][j][1]) + N-key_list[6])%N
I_b = ((((key_list[4]+C1) % N) ^ ((key_list[5]+C2) % N) ^ ((I_prev_b +
key_list[7]) % N) ^ imageMatrix[i][j][2]) + N-key_list[6])%N
I_prev_r = imageMatrix[i][j][0]
I_prev_g = imageMatrix[i][j][1]
I_prev_b = imageMatrix[i][j][2]
row.append((I_r,I_g,I_b))
x = (x + imageMatrix[i][j][0]/256 + key_list[8]/256 + key_list[9]/256) % 1
y = (x + imageMatrix[i][j][0]/256 + key_list[8]/256 + key_list[9]/256) % 1
else:
I = ((((key_list[4]+C1) % N) ^ ((key_list[5]+C2) % N) ^
((I_prev+key_list[7]) % N) ^ imageMatrix[i][j]) + N-key_list[6])%N
I_prev = imageMatrix[i][j]
row.append(I)
x = (x + imageMatrix[i][j]/256 + key_list[8]/256 + key_list[9]/256) % 1
45
y = (x + imageMatrix[i][j]/256 + key_list[8]/256 + key_list[9]/256) % 1
for ki in range(12):
key_list[ki] = (key_list[ki] + key_list[12]) % 256
key_list[12] = key_list[12] ^ key_list[ki]
henonDecryptedImage.append(row)
if color:
im = Image.new("RGB", (dimensionX, dimensionY))
else:
im = Image.new("L", (dimensionX, dimensionY)) # L is for Black and white
pixels
pix = im.load()
for x in range(dimensionX):
for y in range(dimensionY):
pix[x, y] = henonDecryptedImage[x][y]
im.save(imageName.split('_')[0] + "_LogisticDec.png", "PNG")
def uaci(img1,img2):
h,w=img1.shape
value=0
for y in range(h):
for x in range(w):
value+=(abs(int(img1[x,y])-int(img2[x,y])))
value=value*100/(w*h*255)
return value
def sumofpixelval(h,w,img1,img2):
m=np.empty([w,h])
for y in range(h):
for x in range(w):
if img1[x,y]==img2[x,y]:
m[x,y]=0
else:
m[x,y]=1
46
psum=0
for y in range(h):
for x in range(w):
psum+=m[x,y]
return psum
def npcr(img1,img2):
h,w=img1.shape
npcrv=((sumofpixelval(h,w,img1,img2)/(h*w))*100)
return npcrv
Encrypted Image
LogisticEncryption("lena.bmp", "abcdefghijklm")
im = Image.open("lena_LogisticEnc.png", 'r')
imshow(np.asarray(im), cmap='gray')
Decrypted Image
LogisticDecryption("lena_LogisticEnc.png","abcdefghijklm")
im = Image.open("lena_LogisticDec.png", 'r')
imshow(np.asarray(im),cmap='gray')
Histogram Analysis
image = "korea"
ext = ".jpg"
img = cv2.imread(image + ext,1)
pil_im = Image.open(image + ext, 'r')
imshow(np.asarray(pil_im))
47
plt.figure(figsize=(14,6))
histogram_blue = cv2.calcHist([img],[0],None,[256],[0,256])
plt.plot(histogram_blue, color='blue')
histogram_green = cv2.calcHist([img],[1],None,[256],[0,256])
plt.plot(histogram_green, color='green')
histogram_red = cv2.calcHist([img],[2],None,[256],[0,256])
plt.plot(histogram_red, color='red')
plt.title('Intensity Histogram - Original Image', fontsize=20)
plt.xlabel('pixel values', fontsize=16)
plt.ylabel('pixel count', fontsize=16)
plt.show()
image = "korea_LogisticEnc"
ext = ".png"
img = cv2.imread(image + ext,1)
pil_im = Image.open(image + ext, 'r')
imshow(np.asarray(pil_im))
plt.figure(figsize=(14,6))
histogram_blue = cv2.calcHist([img],[0],None,[256],[0,256])
plt.plot(histogram_blue, color='blue')
histogram_green = cv2.calcHist([img],[1],None,[256],[0,256])
plt.plot(histogram_green, color='green')
histogram_red = cv2.calcHist([img],[2],None,[256],[0,256])
plt.plot(histogram_red, color='red')
plt.title('Intensity Histogram - Logistic Encrypted Image', fontsize=20)
plt.xlabel('pixel values', fontsize=16)
plt.ylabel('pixel count', fontsize=16)
plt.show()
48
Pixel Correlation
image = "korea"
ext = ".jpg"
ImageMatrix,image_xsize,image_ysize = getImageMatrix_gray(image+ext)
samples_x = []
samples_y = []
for i in range(1024):
x = random.randint(0,image_xsize-2)
y = random.randint(0,image_ysize-1)
samples_x.append(ImageMatrix[x][y])
samples_y.append(ImageMatrix[x+1][y])
plt.figure(figsize=(10,8))
plt.scatter(samples_x,samples_y,s=2)
plt.title('Adjacent Pixel Autocorrelation - Original Image', fontsize=20)
plt.show()
image = "korea_LogisticEnc"
ext = ".png"
ImageMatrix,image_xsize,image_ysize = getImageMatrix_gray(image+ext)
samples_x = []
samples_y = []
for i in range(1024):
x = random.randint(0,image_xsize-2)
y = random.randint(0,image_ysize-1)
samples_x.append(ImageMatrix[x][y])
samples_y.append(ImageMatrix[x+1][y])
plt.figure(figsize=(10,8))
plt.scatter(samples_x,samples_y,s=2)
plt.title('Adjacent Pixel Autocorrelation - Encrypt Image', fontsize=20)
plt.show()
49
Key Sensitivity
image = "korea"
ext = ".jpg"
pil_im = Image.open(image + ext, 'r')
imshow(np.asarray(pil_im))
LogisticEncryption(image + ext, "abecdefcekfgh")
im = Image.open(image + "_LogisticEnc.png", 'r')
imshow(np.asarray(im))
LogisticDecryption(image + "_LogisticEnc.png", "abecdefaekfgh")
im = Image.open(image + "_LogisticDec.png", 'r')
imshow(np.asarray(im))
LogisticDecryption(image + "_LogisticEnc.png", "abecdefcekfgh")
im = Image.open(image + "_LogisticDec.png", 'r')
imshow(np.asarray(im))
50
img.save("lena1.png")
LogisticEncryption("lena1.png", "abcdefghijklm")
img3 = Image.open("lena1_LogisticEnc.png", 'r')
imshow(np.asarray(img1), cmap='gray')
print("NPCR:",npcr(np.asarray(img1),np.asarray(img3)))
print("UACI:",uaci(np.asarray(img1),np.asarray(img3)))
51