├── .gitignore ├── Dataset ├── 10.jpg ├── 11.jpg ├── 12.jpg ├── 13.jpg ├── 14.jpg ├── 15.jpg ├── 16.jpg ├── 17.jpg ├── 18.jpg └── 19.jpg ├── README.md └── RNA_DNA.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /Dataset/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mehdim7/RNA-DNA-Image-Encryption/493a6cf1a98292eb5461a2fd273a6c9a39e5560b/Dataset/10.jpg -------------------------------------------------------------------------------- /Dataset/11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mehdim7/RNA-DNA-Image-Encryption/493a6cf1a98292eb5461a2fd273a6c9a39e5560b/Dataset/11.jpg -------------------------------------------------------------------------------- /Dataset/12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mehdim7/RNA-DNA-Image-Encryption/493a6cf1a98292eb5461a2fd273a6c9a39e5560b/Dataset/12.jpg -------------------------------------------------------------------------------- /Dataset/13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mehdim7/RNA-DNA-Image-Encryption/493a6cf1a98292eb5461a2fd273a6c9a39e5560b/Dataset/13.jpg -------------------------------------------------------------------------------- /Dataset/14.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mehdim7/RNA-DNA-Image-Encryption/493a6cf1a98292eb5461a2fd273a6c9a39e5560b/Dataset/14.jpg -------------------------------------------------------------------------------- /Dataset/15.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mehdim7/RNA-DNA-Image-Encryption/493a6cf1a98292eb5461a2fd273a6c9a39e5560b/Dataset/15.jpg -------------------------------------------------------------------------------- /Dataset/16.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mehdim7/RNA-DNA-Image-Encryption/493a6cf1a98292eb5461a2fd273a6c9a39e5560b/Dataset/16.jpg -------------------------------------------------------------------------------- /Dataset/17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mehdim7/RNA-DNA-Image-Encryption/493a6cf1a98292eb5461a2fd273a6c9a39e5560b/Dataset/17.jpg -------------------------------------------------------------------------------- /Dataset/18.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mehdim7/RNA-DNA-Image-Encryption/493a6cf1a98292eb5461a2fd273a6c9a39e5560b/Dataset/18.jpg -------------------------------------------------------------------------------- /Dataset/19.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mehdim7/RNA-DNA-Image-Encryption/493a6cf1a98292eb5461a2fd273a6c9a39e5560b/Dataset/19.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RNA-DNA-Image-Encryption 2 | This project relates to an article written in this regard. Here are title and abstract of this article: 3 | 4 | title: Acid based Medical Image Encryption Signal Processing 5 | 6 | abstract: 7 | Medical Image image encryption is very important to protect from any unauthorized user access. In this paper a two-phase secure image encryption has been proposed using the concepts of Deoxyribonucleic acid (DNA) and Ribonucleic acid (RNA). First, the plain image is encrypted based on DNA rules, DNA XOR operator, and chaotic map function in DNA phase and initial cipher image is created. Next, the cipher image is used as the input of RNA phase so that codons truth table for RNA and secret key are exploited for further image encryption. The proposed method has high resistance against common attacks in the field and achieved entropy of 7.9997. 8 | 9 | for run this project please follow this steps: 10 | 1: download datasets folder 11 | 2: set 'path' variable in RNA_DNA.py file with value that indicates dataset folder. for example if you copy dataset folder in drive 'd:' you should write path='D:/datasets folder name/' 12 | 3: run program 13 | 4: see result in datasets folder and enjoy its! 14 | 15 | An example of the program result for '15.jpg' dataset: 16 | 17 | Image: 15 18 | RNA Entropy: 7.999618419885624 19 | Key Sensivity: 2153 , 99.61 20 | First Pixcel: 21 | NPCR: 0.991498 22 | UACI: 0.328311 23 | Midle Pixcel: 24 | NPCR: 0.997614 25 | UACI: 0.321843 26 | Last Pixcel: 27 | NPCR: 0.995364 28 | UACI: 0.334691 29 | Average: 30 | NPCR: 0.994825 31 | UACI: 0.328282 32 | Correlation Coefficient: 33 | Horizontal: -0.0233 34 | Vertical: -0.0002 35 | Diagonal: -0.0057 36 | Time: 315.564 37 | 38 | thanks 39 | -------------------------------------------------------------------------------- /RNA_DNA.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Image Encryption 3 | Combination RNA and DNA and Logistic map 4 | CopyRight Mehdi Yadollahi January 5, 2019''' 5 | import random 6 | import cv2 7 | import numpy as np 8 | import matplotlib 9 | from matplotlib import pyplot as plt 10 | from numpy import unique 11 | from scipy.stats import entropy as scipy_entropy 12 | import time 13 | 14 | class Gen: 15 | def __init__(self,bases,code): 16 | self.bases=bases 17 | self.code=code 18 | 19 | def getCode(self): 20 | return self.code 21 | 22 | def getBases(self): 23 | return self.bases 24 | 25 | class Codon: 26 | def __init__(self,gen1,gen2,gen3): 27 | self.codon=[gen1,gen2,gen3] 28 | 29 | def getString(self): 30 | s='' 31 | for i in range(3): 32 | s=s+self.codon[i].getBases() 33 | return s 34 | 35 | def getBinary(self): 36 | s = '' 37 | for i in range(3): 38 | s = s + self.codon[i].getCode() 39 | return s 40 | 41 | def getComplement(self,basesTemp): 42 | newCodon=[] 43 | for i in range(3): 44 | if(self.codon[i].bases=='A'): 45 | newCodon.append(basesTemp[3]) 46 | elif (self.codon[i].bases == 'U'): 47 | newCodon.append(basesTemp[0]) 48 | elif (self.codon[i].bases == 'C'): 49 | newCodon.append(basesTemp[2]) 50 | elif (self.codon[i].bases == 'G'): 51 | newCodon.append(basesTemp[1]) 52 | else: 53 | print('Error complementary') 54 | return Codon(newCodon[0],newCodon[1],newCodon[2]) 55 | 56 | def Xor(self,temp): 57 | # print('>',temp.getBinary()) 58 | # print('^',self.getBinary()) 59 | bin3=int(self.getBinary(),2) ^ int(temp.getBinary(),2) 60 | bin3='{0:06b}'.format(bin3) 61 | if len(bin3)!=6: 62 | print('XOR Error') 63 | # print('=',bin3) 64 | 65 | return bin3 66 | 67 | 68 | class CodonRow: 69 | def __init__(self,indexC,randC,codonC): 70 | self.index=indexC 71 | self.rand=randC 72 | self.codons=codonC 73 | 74 | def getRow(self): 75 | return [self.index,self.rand,self.codons] 76 | 77 | def getCodon(self): 78 | return self.codons 79 | 80 | def getIndex(self): 81 | return self.index 82 | 83 | def getRand(self): 84 | return self.rand 85 | 86 | class DNA: 87 | def __init__(self,imgBit,key): 88 | self.DNATableBits={'A':['00','00','11','11','10','01','10','01'], 89 | 'T': ['11', '11', '00', '00', '01', '10', '01', '10'], 90 | 'C': ['10','01','10','01', '00','00','11','11'], 91 | 'G': ['01', '10', '01','10','11', '11', '00', '00']} 92 | 93 | self.DNATable = [ {'00':'A', '11':'T', '10':'C', '01':'G'}, 94 | {'00': 'A', '11': 'T', '01': 'C', '10': 'G'}, 95 | {'11': 'A', '00': 'T', '10': 'C', '01': 'G'}, #2 96 | {'11': 'A', '00': 'T', '01': 'C', '10': 'G'}, 97 | {'10': 'A', '01': 'T', '00': 'C', '11': 'G'}, #4 98 | {'01': 'A', '10': 'T', '00': 'C', '11': 'G'}, 99 | {'10': 'A', '01': 'T', '11': 'C', '00': 'G'}, #6 100 | {'01': 'A', '10': 'T', '11': 'C', '00': 'G'}] 101 | 102 | self.XORDNATable={'A':{'A':'A' , 'T':'T' , 'C':'C', 'G':'G'}, 103 | 'T':{'A':'T','T':'A','C':'G','G':'C'}, 104 | 'C':{'A':'C','T':'G','C':'A','G':'T'}, 105 | 'G':{'A':'G', 'T':'C' , 'C':'T' , 'G':'A'}} 106 | 107 | self.imgBit=imgBit 108 | self.key=key 109 | self.R=3.999 110 | self.x0=self.createKey(key) 111 | print('Key: ',self.key, 'X0: ',self.x0) 112 | 113 | 114 | def createKey(self,key): 115 | a = ord(key[0]) 116 | for i in range(1, 12): 117 | a = a ^ ord(key[i]) 118 | a = a / (2 ** 12) 119 | b = '' 120 | for i in range(12, len(key)): 121 | b += '{0:08b}'.format(ord(key[i])) 122 | b = int(b, 2) / (2 ** 32) 123 | return (a + b) / 2 124 | 125 | def DNACrypt(self): 126 | finalBit = '' 127 | keyCounter = 0 128 | lastPixcel='00000000' 129 | logistic=self.logisticMap() 130 | for i in range(0, len(self.imgBit), 8): 131 | x1 = round(next(logistic)*255) #random.randint(0, 255) #key 132 | x2 = round(next(logistic)*7) 133 | x3 = round(next(logistic)*7) 134 | x4 = round(next(logistic)*7) 135 | x5 = round(next(logistic)*7) 136 | 137 | key8Bit='{0:08b}'.format(x1) 138 | data8Bit=self.imgBit[i:i + 8] 139 | key4DNA=self.get4DNA(key8Bit,x2) # key => x2 140 | data4DNA=self.get4DNA(data8Bit,x3) # data => x3 141 | result4DNA=self.XorDNA(key4DNA,data4DNA) 142 | lastPixcel4DNA=self.get4DNA(lastPixcel,x4) # lastPixcel => x4 143 | result4DNA=self.XorDNA(result4DNA,lastPixcel4DNA) 144 | 145 | lastPixcel=self.get4DNABinary(result4DNA,x5) # result => x5 146 | finalBit += lastPixcel 147 | 148 | return finalBit 149 | 150 | def logisticMap(self): 151 | x=self.x0 152 | while True: 153 | x = self.R * x * (1.0 - x) 154 | yield x 155 | 156 | def get4DNABinary(self,result4DNA,ruleNumber): 157 | cc = '' 158 | for i in range(0, len(result4DNA)): 159 | cc += self.DNATableBits[result4DNA[i]][ruleNumber] 160 | return cc 161 | 162 | def get4DNA(self,bits,ruleNumber): 163 | cc='' 164 | for i in range(0, len(bits), 2): 165 | temp = bits[i:i + 2] 166 | cc +=self.DNATable[ruleNumber][temp] 167 | return cc 168 | 169 | def XorDNA(self,result4DNA,lastPixcel4DNA): 170 | cc='' 171 | for i in range(0, len(result4DNA)): 172 | cc+=self.XORDNATable[result4DNA[i]][lastPixcel4DNA[i]] 173 | return cc 174 | 175 | class RNA: 176 | baseRNACodonTable=[] 177 | def __init__(self,imgBit,keyBit): 178 | self.imgBit=imgBit 179 | self.key=keyBit 180 | # self.key='00110110' 181 | self.gen1= Gen('A','00') 182 | self.gen2 = Gen('C', '01') 183 | self.gen3 = Gen('G', '10') 184 | self.gen4 = Gen('U', '11') 185 | self.Bases=[self.gen1,self.gen2,self.gen3,self.gen4] 186 | self.baseRNACodonTable= self.CreateRNACodonTable() 187 | self.RNACodonTable1= self.CreateRNACodonTable(setRand=True) 188 | self.RNACodonTableC1= self.CreateRNACodonTableC(self.RNACodonTable1) 189 | self.RNACodonTable2= self.CreateRNACodonTable(setRand=True) 190 | self.RNACodonTableC2= self.CreateRNACodonTableC(self.RNACodonTable2) 191 | 192 | # self.ShowCodonTable(self.baseRNACodonTable) 193 | 194 | def CreateRNACodonTable(self,setRand=False): 195 | index=0 196 | rand = 1 197 | if(setRand==True): 198 | rand=random.uniform(0, 1) 199 | 200 | codonTable=[] 201 | for i in range(4): 202 | for j in range(4): 203 | for k in range(4): 204 | newRow=CodonRow(index,rand,Codon(self.Bases[i], self.Bases[j], self.Bases[k])) 205 | codonTable.append(newRow) 206 | index+=1 207 | rand=index 208 | if (setRand == True): 209 | rand=random.uniform(0, 1) 210 | 211 | codonTable.sort(key=lambda x: x.rand, reverse=False) 212 | return codonTable 213 | 214 | def CreateRNACodonTableC(self, codonTable): 215 | codonTableC=[] 216 | for i in range(64): 217 | tt= codonTable[i].getCodon().getComplement(self.Bases) 218 | newRow=CodonRow(codonTable[i].index,codonTable[i].rand,tt) 219 | codonTableC.append(newRow) 220 | return codonTableC 221 | 222 | def ShowCodonTable(self,codonTable): 223 | for i in range(64): 224 | st=str.format("{0},{1},{2},{3},{4}",i,codonTable[i].getIndex(),codonTable[i].getRand() 225 | ,codonTable[i].getCodon().getString() 226 | ,codonTable[i].getCodon().getBinary()) 227 | print(st) 228 | 229 | def getRNACodeCodon(self,tripleCodon,key2Bit): 230 | if(key2Bit=='00'): 231 | table=self.RNACodonTable1 232 | elif(key2Bit=='01'): 233 | table=self.RNACodonTableC1 234 | elif (key2Bit == '10'): 235 | table = self.RNACodonTable2 236 | elif (key2Bit == '11'): 237 | table = self.RNACodonTableC2 238 | else: 239 | print('Error getRNACodeCodon:',key2Bit,tripleCodon) 240 | indexTemp=self.findTripleCodon(tripleCodon) 241 | return indexTemp,table[indexTemp].getCodon() 242 | 243 | def findTripleCodon(self,tripleCodon): 244 | for i in range(64): 245 | if (tripleCodon==self.baseRNACodonTable[i].getCodon().getString()): 246 | return i 247 | print(tripleCodon) 248 | print('Error finding triple Codon') 249 | return -1 250 | 251 | def convertBinaryToBases(self,bits): 252 | cc = '' 253 | for i in range(0, len(bits), 2): 254 | temp = bits[i:i + 2] 255 | cc += list(filter(lambda x: x.getCode() == temp, self.Bases))[0].getBases() 256 | 257 | fix = len(cc) % 3 258 | fix= (3-fix)% 3 259 | p = 'A' * fix 260 | cc += p 261 | return cc 262 | 263 | def getXorCodon(self,tempCodon,sourceCodon): 264 | bin1=tempCodon.Xor(sourceCodon) 265 | tripleCodon=self.convertBinaryToBases(bin1) 266 | tempindex= self.findTripleCodon(tripleCodon) 267 | return self.baseRNACodonTable[tempindex].getCodon() 268 | 269 | def RNACrypt(self): 270 | sourceString=self.convertBinaryToBases(self.imgBit) 271 | finalString='' 272 | finalBit='' 273 | 274 | keyCounter=0 275 | for i in range(0,len(sourceString),3): 276 | startKey=keyCounter%(len(self.key)-1) 277 | keyCounter+=1 278 | tt=self.key[startKey:startKey+2] 279 | index,tempCodon= self.getRNACodeCodon(sourceString[i:i + 3], tt) 280 | finalString+=tempCodon.getString() 281 | finalBit+=tempCodon.getBinary() 282 | 283 | return finalBit 284 | 285 | class ImageCrypt: 286 | def __init__(self,img,pathRNAFinal,key): 287 | self.imgSource=img 288 | self.pathRNAFinal=pathRNAFinal 289 | 290 | self.imgRNAFinal=img.copy() 291 | 292 | finalBit=self.convertImageToBinary(self.imgSource) 293 | 294 | self.dna=DNA(finalBit,key) 295 | finalBit = self.dna.DNACrypt() 296 | 297 | self.rna = RNA(finalBit, self.convertKeyToBinary(key)) 298 | finalBit = self.rna.RNACrypt() 299 | 300 | self.saveRNAFinalImage(finalBit) 301 | 302 | def convertKeyToBinary(self,key): 303 | a = '' 304 | for i in range(0, len(key)): 305 | a += '{0:08b}'.format(ord(key[i])) 306 | return a 307 | 308 | def convertImageToBinary(self,img): 309 | bb = '' 310 | rows, cols = img.shape 311 | for i in range(rows): 312 | for j in range(cols): 313 | bb += '{0:08b}'.format(img[i, j]) 314 | return bb 315 | 316 | def saveRNAFinalImage(self,finalBit): 317 | rows, cols = self.imgRNAFinal.shape 318 | for i in range(rows): 319 | for j in range(cols): 320 | start=i * cols * 8 + j * 8 321 | temp8Bit= finalBit[start:start+8] 322 | self.imgRNAFinal[i,j] = int(temp8Bit,2) 323 | cv2.imwrite(self.pathRNAFinal, self.imgRNAFinal) 324 | 325 | class DataSet: 326 | def __init__(self,imagesDict): 327 | self.images=imagesDict 328 | self.path=path 329 | self.filePass='.bmp' 330 | 331 | def getDataSetPath(self,dataSetName,filepass='.jpg'): 332 | return self.path+self.images[dataSetName]+filepass 333 | 334 | def getDataSetFinalPath(self,dataSetName): 335 | return self.path + self.images[dataSetName] +'RNA'+ self.filePass 336 | 337 | def getDataSetFinalPathKeyChange(self,dataSetName): 338 | return self.path + self.images[dataSetName] +'RNAKeyChange'+ self.filePass 339 | 340 | def getDataSetFinalPathKeyChangeResult(self,dataSetName,count): 341 | return self.path + self.images[dataSetName] +'KeyChange_'+str(count)+ self.filePass 342 | 343 | def getDataSetFinalPathUACI1(self,dataSetName): 344 | return self.path + self.images[dataSetName] +'RNAUACI1'+ self.filePass 345 | 346 | def getDataSetFinalPathUACI2(self,dataSetName): 347 | return self.path + self.images[dataSetName] +'RNAUACI2'+ self.filePass 348 | 349 | def getDataSetFinalPathUACI3(self,dataSetName): 350 | return self.path + self.images[dataSetName] +'RNAUACI3'+ self.filePass 351 | 352 | def getDataSetRNAPlt(self,dataSetName,filepass='.png'): 353 | return self.path + self.images[dataSetName] +'RNAPlt'+ filepass 354 | 355 | def getDataSetHorizontalCorPlt(self,dataSetName,filepass='.png'): 356 | return self.path + self.images[dataSetName] +'HorizontalCorPlt'+ filepass 357 | 358 | def getDataSetVerticalCorPlt(self,dataSetName,filepass='.png'): 359 | return self.path + self.images[dataSetName] +'VerticalCorPlt'+ filepass 360 | 361 | def getDataSetDiagonalCorPlt(self, dataSetName, filepass='.png'): 362 | return self.path + self.images[dataSetName] + 'DiagonalCorPlt' + filepass 363 | 364 | 365 | class AnalyzeImage: 366 | 367 | def __init__(self,dataSetName,key,dataSets,infoFile): 368 | self.dataSets=dataSets 369 | self.dataSetName=dataSetName 370 | self.infoFile=infoFile 371 | self.key=key 372 | self.correlationcCount=8000 373 | 374 | self.imgSource = cv2.imread(self.dataSets.getDataSetPath(dataSetName), cv2.IMREAD_GRAYSCALE) 375 | self.imgRNAFinal = cv2.imread(self.dataSets.getDataSetFinalPath(dataSetName), cv2.IMREAD_GRAYSCALE) 376 | 377 | entropyFinal = self.Entropy(self.imgRNAFinal) 378 | print('RNA Entropy: ', entropyFinal) 379 | self.infoFile.write('\nRNA Entropy: ' + str(entropyFinal)) 380 | 381 | self.key2 = self.getNextKey(self.key) 382 | run = ImageCrypt(self.imgSource, self.dataSets.getDataSetFinalPathKeyChange(dataSetName), self.key2) 383 | 384 | count, countPercent =self.keySensitivityImage(dataSetName) 385 | infoFile.write('\nKey Sensivity: ' + str(count)+' , '+ str(round(countPercent,2))) 386 | self.NPCRUACI(dataSetName, infoFile) 387 | self.Correlation() 388 | 389 | 390 | def Entropy(self,img): 391 | _, counts = unique(img, return_counts=True) 392 | return scipy_entropy(counts, base=2) 393 | 394 | def Correlation(self): 395 | matrixX,matrixY=self.getCorrelation(self.imgSource, 1, 0) 396 | matrixX2,matrixY2=self.getCorrelation(self.imgRNAFinal, 1, 0) 397 | self.infoFile.write('\nCorrelation Coefficient:') 398 | corHorizontal = np.corrcoef(matrixX2, matrixY2) 399 | self.infoFile.write('\nHorizontal: ' + str(round(corHorizontal[1, 0],4))) 400 | 401 | matplotlib.style.use('ggplot') 402 | plt.subplot(122), plt.scatter(matrixX2, matrixY2,c='blue', alpha=0.5), plt.title('Horizontal') 403 | plt.subplot(121), plt.scatter(matrixX, matrixY,c='blue', alpha=0.5), plt.title('ORIGINAL') 404 | plt.savefig(self.dataSets.getDataSetHorizontalCorPlt(self.dataSetName)) 405 | #plt.show() 406 | 407 | matrixX, matrixY = self.getCorrelation(self.imgSource, 0, 1) 408 | matrixX2, matrixY2 = self.getCorrelation(self.imgRNAFinal, 0, 1) 409 | 410 | corVertical = np.corrcoef(matrixX2, matrixY2) 411 | self.infoFile.write('\nVertical: ' + str(round(corVertical[1, 0],4))) 412 | 413 | matplotlib.style.use('ggplot') 414 | plt.subplot(122), plt.scatter(matrixX2, matrixY2,c='blue', alpha=0.5), plt.title('Vertical') 415 | plt.subplot(121), plt.scatter(matrixX, matrixY,c='blue', alpha=0.5), plt.title('ORIGINAL') 416 | plt.savefig(self.dataSets.getDataSetVerticalCorPlt(self.dataSetName)) 417 | 418 | matrixX, matrixY = self.getCorrelation(self.imgSource, 1, 1) 419 | matrixX2, matrixY2 = self.getCorrelation(self.imgRNAFinal, 1, 1) 420 | 421 | corDiagonal = np.corrcoef(matrixX2, matrixY2) 422 | self.infoFile.write('\nDiagonal: ' + str(round(corDiagonal[1,0],4))) 423 | 424 | matplotlib.style.use('ggplot') 425 | plt.subplot(122), plt.scatter(matrixX2, matrixY2,c='blue', alpha=0.5), plt.title('Diagonal') 426 | plt.subplot(121), plt.scatter(matrixX, matrixY,c='blue', alpha=0.5), plt.title('ORIGINAL') 427 | plt.savefig(self.dataSets.getDataSetDiagonalCorPlt(self.dataSetName),dpi=300) 428 | 429 | def getCorrelation(self,img,x,y): # x=1 y=0 horizontal x=0 y=1 vertical x=1 y=1 diagonal 430 | matrixX=[] 431 | matrixY=[] 432 | 433 | rows, cols = img.shape 434 | 435 | maxRow=rows-1 436 | maxCol=cols-1 437 | if x==0 and y==1: 438 | maxCol=cols 439 | if x==1 and y==0: 440 | maxRow=rows 441 | 442 | random.seed(1) 443 | for i in range(self.correlationcCount): 444 | rx=random.randint(0,maxRow-2) 445 | ry=random.randint(0,maxCol-2) 446 | matrixX.append(img[rx,ry]) 447 | matrixY.append(img[rx+x,ry+y]) 448 | 449 | return matrixX,matrixY 450 | 451 | def getNextKey(self, key): 452 | if (len(key) < 16): 453 | print('Error key') 454 | if (ord(key[15]) == 255): 455 | key = key[0:15] + chr(0) 456 | else: 457 | key = key[0:15] + chr(ord(key[15]) + 1) 458 | return key 459 | 460 | def getNextPixcel(self, pixcel): 461 | if (pixcel == 255): 462 | pixcel = 0 463 | else: 464 | pixcel += 1 465 | return pixcel 466 | 467 | def keySensitivityImage(self, dataSetName): 468 | stt=self.dataSets.getDataSetFinalPath(dataSetName) 469 | img1 = cv2.imread(self.dataSets.getDataSetFinalPath(dataSetName), cv2.IMREAD_GRAYSCALE) 470 | img2 = cv2.imread(self.dataSets.getDataSetFinalPathKeyChange(dataSetName), cv2.IMREAD_GRAYSCALE) 471 | sensitivityImage = cv2.imread(self.dataSets.getDataSetFinalPathKeyChange(dataSetName), cv2.IMREAD_GRAYSCALE) 472 | rows, cols = img1.shape 473 | count = 0 474 | for i in range(rows): 475 | for j in range(cols): 476 | if (img1[i, j] == img2[i, j]): 477 | sensitivityImage[i, j] = 255 478 | count += 1 479 | else: 480 | sensitivityImage[i, j] = 0 481 | 482 | cv2.imwrite(self.dataSets.getDataSetFinalPathKeyChangeResult(dataSetName, count), sensitivityImage) 483 | return count,((1-(count/(rows*cols)))*100) 484 | 485 | def NPCRUACI(self, dataSetName, infoFile): 486 | img1 = cv2.imread(self.dataSets.getDataSetPath(dataSetName), cv2.IMREAD_GRAYSCALE) 487 | img1[0, 0] = self.getNextPixcel(img1[0, 0]) 488 | rows, cols = img1.shape 489 | run1 = ImageCrypt(img1, self.dataSets.getDataSetFinalPathUACI1(dataSetName), self.key) 490 | 491 | NPCR1, UACI1 = self.getEachNPCRUACI(self.dataSets.getDataSetFinalPath(dataSetName), 492 | self.dataSets.getDataSetFinalPathUACI1(dataSetName)) 493 | infoFile.write('\nFirst Pixcel: ') 494 | infoFile.write('\nNPCR: ' + str(round(NPCR1,6))) 495 | infoFile.write('\nUACI: ' + str(round(UACI1,6))) 496 | 497 | img2 = cv2.imread(self.dataSets.getDataSetPath(dataSetName), cv2.IMREAD_GRAYSCALE) 498 | img2[int(rows / 2), int(cols / 2)] = self.getNextPixcel(img2[int(rows / 2), int(cols / 2)]) 499 | run2 = ImageCrypt(img2, self.dataSets.getDataSetFinalPathUACI2(dataSetName), self.key) 500 | 501 | NPCR2, UACI2 = self.getEachNPCRUACI(self.dataSets.getDataSetFinalPath(dataSetName), 502 | self.dataSets.getDataSetFinalPathUACI2(dataSetName)) 503 | infoFile.write('\nMidle Pixcel: ') 504 | infoFile.write('\nNPCR: ' + str(round(NPCR2,6))) 505 | infoFile.write('\nUACI: ' + str(round(UACI2,6))) 506 | 507 | img3 = cv2.imread(self.dataSets.getDataSetPath(dataSetName), cv2.IMREAD_GRAYSCALE) 508 | img3[rows - 1, cols - 1] = self.getNextPixcel(img2[rows - 1, cols - 1]) 509 | run3 = ImageCrypt(img3, self.dataSets.getDataSetFinalPathUACI3(dataSetName), self.key) 510 | 511 | NPCR3, UACI3 = self.getEachNPCRUACI(self.dataSets.getDataSetFinalPath(dataSetName), 512 | self.dataSets.getDataSetFinalPathUACI3(dataSetName)) 513 | infoFile.write('\nLast Pixcel: ') 514 | infoFile.write('\nNPCR: ' + str(round(NPCR3,6))) 515 | infoFile.write('\nUACI: ' + str(round(UACI3,6))) 516 | 517 | NPCR4 = (NPCR1 + NPCR2 + NPCR3) / 3 518 | UACI4 = (UACI1 + UACI2 + UACI3) / 3 519 | 520 | infoFile.write('\nAverage: ') 521 | infoFile.write('\nNPCR: ' + str(round(NPCR4,6))) 522 | infoFile.write('\nUACI: ' + str(round(UACI4,6))) 523 | 524 | def getEachNPCRUACI(self, imgPath1, imgPath2): 525 | img1 = cv2.imread(imgPath1, cv2.IMREAD_GRAYSCALE) 526 | img2 = cv2.imread(imgPath2, cv2.IMREAD_GRAYSCALE) 527 | 528 | rows, cols = img1.shape 529 | sumNPCR = 0 530 | sumUACI = 0 531 | for i in range(rows): 532 | for j in range(cols): 533 | sumUACI += abs(int(img1[i, j]) - int(img2[i, j])) 534 | if (img1[i, j] != img2[i, j]): 535 | sumNPCR += 1 536 | NPCR = sumNPCR / (rows * cols) 537 | UACI = sumUACI / (255 * rows * cols) 538 | return NPCR, UACI 539 | 540 | 541 | class ProposedAlgorithm: 542 | def __init__(self,dataSets,key): 543 | self.dataSets=DataSet(dataSets) 544 | self.key=key 545 | for k in dataSets: 546 | print('>>>> ', k, ' <<<<<') 547 | start=time.time() 548 | infoFile = open(self.dataSets.getDataSetPath(k,'.txt'),'w') 549 | infoFile.write('Image: '+k) 550 | #infoFile.write('\r\n'+'Key: ' + self.key) 551 | img1 = cv2.imread(self.dataSets.getDataSetPath(k), cv2.IMREAD_GRAYSCALE) 552 | run=ImageCrypt(img1,self.dataSets.getDataSetFinalPath(k),self.key) 553 | analyze=AnalyzeImage(k,self.key,self.dataSets,infoFile) 554 | #self.showImage(k) 555 | end=time.time() 556 | infoFile.write('\nTime: ' + str(round(end-start,3))) 557 | infoFile.close() 558 | 559 | def showImage(self,dataSetName): 560 | imgSource =cv2.imread(self.dataSets.getDataSetPath(dataSetName), cv2.IMREAD_GRAYSCALE) 561 | imgRNA=cv2.imread(self.dataSets.getDataSetFinalPath(dataSetName), cv2.IMREAD_GRAYSCALE) 562 | imgKeySensitive=cv2.imread(self.dataSets.getDataSetFinalPathKeyChange(dataSetName), cv2.IMREAD_GRAYSCALE) 563 | 564 | plt.subplot(231), plt.imshow(imgSource), plt.title('ORIGINAL') 565 | plt.subplot(232), plt.imshow(imgKeySensitive), plt.title('Key Sensitive') 566 | plt.subplot(233), plt.imshow(imgRNA), plt.title('RNA') 567 | hist1 = cv2.calcHist([imgSource], [0], None, [256], [0, 256]) 568 | hist2 = cv2.calcHist([imgKeySensitive], [0], None, [256], [0, 256]) 569 | hist3 = cv2.calcHist([imgRNA], [0], None, [256], [0, 256]) 570 | 571 | plt.subplot(234), plt.hist(imgSource.ravel(), 256, [0, 256]) 572 | plt.subplot(235), plt.hist(imgKeySensitive.ravel(), 256, [0, 256]) 573 | plt.subplot(236), plt.hist(imgRNA.ravel(), 256, [0, 256]) 574 | 575 | plt.savefig(self.dataSets.getDataSetRNAPlt(dataSetName)) 576 | 577 | 578 | # key for image encryption 579 | key='̼‡ÆN²ÜYTö,—¡Ä' # 128 bit ( 16 char) 580 | # Name of DataSets 581 | dataSets={'10':'10','11':'11','12':'12','13':'13','14':'14','15':'15' 582 | ,'16': '16','17':'17','18':'18','19':'19'} 583 | 584 | 585 | # DataSet Directory Path 586 | path='D:/RNA/' 587 | p=ProposedAlgorithm(dataSets,key) 588 | print('Done!!!') 589 | --------------------------------------------------------------------------------