I developed an image encryption using chaotic logistic map but unable to get original image after decryption

Viewed 134

While decrypting the encrypted image i am not getting an decrypted image instead a image with red colour is shown.Here i generated the key using chaotic function and then performed XOR on the original image matrix and the key matrix to get the cipher matrix and then i replaced the original image matrix pixels with cipher matrix and i obtained a cipher image.But when i am applying XOR on the cipher image matrix and the key matrix i getting the original pixel matrix but when i am printing the pixel values using im.getpixels function i am only getting the red pixels values and for green and blue it is showing 0 here are some original image rgb values [(1, 1, 3), (1, 1, 3), (1, 1, 3), (1, 1, 3), (1, 1, 3), (1, 1, 3), (0, 1, 5), (0, 1, 5), (0, 1, 5), (0, 1, 5), (0, 3, 6), (1, 5, 8), (5, 8, 13), (7, 10, 15), (8, 11, 16), (8, 11, 16), (14, 15, 19), (20, 23, 28), (24, 28, 37), (30, 38, 49), ........] and the some decrypted image rgb values are [(1, 0, 0), (1, 0, 0), (1, 0, 0), (1, 0, 0), (1, 0, 0), (1, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (1, 0, 0), (5, 0, 0), (7, 0, 0), (8, 0, 0), (8, 0, 0), (14, 0, 0), (20, 0, 0), (24, 0, 0), (30, 0, 0), (40, 0, 0), (39, 0, 0), (47, 0, 0), (65, 0, 0),......................].How can i get the g,b values also please help below is the code written for encryption.

    from PIL import Image
`import os
import random
def val_tuple(x):
    return x[0]

def getImageMatrix_tuple(image):#it takes the image having tuples as pixels
    im = Image.open(image)  # Can be many different formats.
    pix = im.load()
    image_size = im.size #Get the width and height of the image for iterating over
    print("Image Size : ",image_size)
    image_matrix = []
    for width in range(int(image_size[0])):
        row = []
        for height in range(int(image_size[1])):
            try:
                row.append((binarySequence((val_tuple(pix[width,height])))))
            except:
                row=[(binarySequence((val_tuple(pix[width,height]))))]
        try:
            image_matrix.append(row)
        except:
            image_matrix = [row]
    return image_matrix  #return binary sequence of pixel values of an image

def getImageMatrix_list(image):#it takes the image as lists
    im = Image.open(image)  # Can be many different formats.
    pix = im.load()
    image_size = im.size #Get the width and height of the image for iterating over
    print("Image Size : ",image_size)
    image_matrix = []
    for width in range(int(image_size[0])):
        row = []
        for height in range(int(image_size[1])):
            try:
                row.append((binarySequence((pix[width,height]))))
            except:
                row=[(binarySequence((pix[width,height])))]
        try:
            image_matrix.append(row)
        except:
            image_matrix = [row]
    return image_matrix  #return binary sequence of pixel values of an image

def getChaoticSequence(image):#it takes image as input
    im = Image.open(image)  # Can be many different formats.
    image_size = im.size #Get the width and hight of the image for iterating over
    print("Image Size : ",image_size)
    image_matrix = []
    x=0.2 #seed value
    for width in range(int(image_size[0])):
        row = []
        for height in range(int(image_size[1])):
            try:
                xn = (x*(1-x)*random.uniform(3.5,4))
                x = xn
                row.append(genKEY(binarySequence(int(xn*255)),RightShiftRegister(binarySequence(int(xn*255)))))
            except:
                row=[genKEY(binarySequence(int(xn*255)),RightShiftRegister(binarySequence(int(xn*255))))]
        try:
            image_matrix.append(row)
        except:
            image_matrix = [row]
    return image_matrix  #returns the key of image in binary

def getCipher_im(image_mat,key_mat):
    cipher=[]
    for i in range(im_width):
        for j in range(im_height):
            try:
                cipher.append(DeciOfBin(genKEY(image_mat[i][j],key_mat[i][j])))
            except:
                cipher = [DeciOfBin(genKEY(image_mat[i][j],key_mat[i][j]))]
    return cipher # returns cipher in decimal
    
def RightShiftRegister(binary):
    take = [1,0,1,0,1,0,1,0]
    count=0
    for i in range(len(binary)):
        if take[i]==1 and binary[i]==1:
            count = count +1# even parity
    if(count%2!=0):#odd number # XOR operation
        appendbit=1
    else:
        appendbit=0
    for i in range(6,-1,-1):
        binary[i+1] = binary[i]
    binary[0]=appendbit
    return binary  #Returns right shifted bits by appending XOR bit infront
    
def binarySequence(n):#it takes decimal values
    counter=8
    bitseq=[]
    while(True):
        q=n/2
        r=n%2
        n=q
        counter = counter-1
        try:
            bitseq.append(int(r))
        except:
            bitseq = [r]
        if(counter<=0):
            break
    try:
        bitseq.reverse()
    except:
        bitseq= []
    return bitseq # Converts the decimal value of pixels in binary

def genKEY(list1,list2):#it takes two lists for XOR
    key=[]
    for i in range(len(list1)):
        try:
            key.append(list1[i] ^ list2[i])
        except:
            key = [list1[i] ^ list2[i]]
    return key    # Returns Key in Binary

def DeciOfBin(binary):
    decimal = 0
    for bit in binary:
        decimal = decimal * 2 + int(bit)
    return decimal
###############################################GLOBAL Variables######################################
file_name="frame0.jpg"
cipher_file_name="frame0-cipher.jpg" #file_name+
im = Image.open(file_name)  # Can be many different formats.
size = im.size
im_width=size[0]
im_height=size[1]
#####################################################################################################
def LogMapEncrypt(size,cipher_mat):#it takes cipher matrix as input
    r=0
    j=0
    c=0
    im = Image.new("L", (size[0],size[1]))
    pix = im.load()
    for i in range(0,im_width*im_height):
        if c<im_height:
            pix[r,c]=cipher_mat[j]
            j=j+1
            c=c+1
        else:
            r=r+1
            c=0
            pix[r,c]=cipher_mat[j]
            j=j+1
    im.save(cipher_file_name,"BMP")
    absPath = os.path.abspath(cipher_file_name)
    return absPath #generates thr path of encrypted image

    
##########################################################################################################


def main():
    im = Image.open(file_name)
    pix=im.load()             #load its pixels
    if type(pix[0,0]) is tuple:     #check for tuple or not
        im=(getImageMatrix_tuple(file_name))
    else:
        im=(getImageMatrix_list(file_name))
        

    
    im_key=getChaoticSequence(file_name)

    print(LogMapEncrypt(size,getCipher_im(im,im_key)))

    
if __name__ == "__main__":
    main()

And the code for decryption i have written is

def LogMapDecrypt(size,original_mat):#it takes cipher matrix as input
r=0
j=0
c=0
k=0
h=0
im = Image.new("RGB",(size[0],size[1]))
pix = im.load()
for i in range(0,im_width*im_height):
    if c<im_height:
        pix[r,c]=original_mat[j][k]
        j=j+1
        c=c+1
    else:
        r=r+1
        c=0
        pix[r,c]=original_mat[j][k]
        c=c+1
        j=j+1
l=[]
for i in range(size[0]):
    for j in range(size[1]):
        l.append(im.getpixel((i,j)))
print(l)   
im.save(original_file_name,"BMP")
absPath = os.path.abspath(original_file_name)
return absPath

#driver code

im1 = Image.open(cipher_file_name)# open the image file
    pix1=im1.load()             #load its pixels
    if type(pix1[0,0]) is tuple:     #check for tuple or not
        im1=(getImageMatrix_tuple(cipher_file_name))
    else:
        im1=(getImageMatrix_list(cipher_file_name))

WHY AM I NOT GETTING DECRYPTED IMAGE.PLS HELP

0 Answers
Related