├── DecryptedImg1.png ├── DecryptedImg2.png ├── Decryption.ipynb ├── Encryption.ipynb ├── Project Report.pdf ├── README.md ├── ToBeSent for Decryption ├── P.png ├── P2.png ├── R.png ├── R2.png ├── cipher.txt └── cipher2.txt ├── keys └── mykey.txt └── pics ├── test1.jpg ├── test2.jpg └── test3.jpg /DecryptedImg1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riyag283/Image-encryption/01aa0ee645925d42bc0df7cb53d152dde409e17f/DecryptedImg1.png -------------------------------------------------------------------------------- /DecryptedImg2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riyag283/Image-encryption/01aa0ee645925d42bc0df7cb53d152dde409e17f/DecryptedImg2.png -------------------------------------------------------------------------------- /Decryption.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 29, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "f = open(\"ToBeSent for Decryption/cipher2.txt\",'r',encoding='utf-8')\n", 10 | "cipher = f.read()" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "import base64" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 3, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "import numpy as np\n", 29 | "import cv2" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 30, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "P = cv2.imread('ToBeSent for Decryption/P.png')\n", 39 | "R = cv2.imread('ToBeSent for Decryption/R.png')" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 31, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "h = np.shape(P)[0]\n", 49 | "w = np.shape(P)[1]" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 6, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "CK = np.ones((h,w,1), dtype = 'uint8')" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 7, 64 | "metadata": {}, 65 | "outputs": [], 66 | "source": [ 67 | "for i in range(h):\n", 68 | " for j in range(w):\n", 69 | " ck = P[i][j][0] ^ R[i][j][0]\n", 70 | " CK[i][j][0] = ck" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 8, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "K1 = []\n", 80 | "for i in range(len(CK)):\n", 81 | " K1.append(0)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 9, 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [ 90 | "for i in range(len(CK)):\n", 91 | " count = 0\n", 92 | " for j in range(len(CK[i])):\n", 93 | " if CK[i][j][0] == 0:\n", 94 | " count += 1\n", 95 | " K1[i] = chr(count)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 10, 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [ 104 | "K1 = \"\".join(K1)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 11, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/plain": [ 115 | "'Hello, World. ThisIsMyKey.'" 116 | ] 117 | }, 118 | "execution_count": 11, 119 | "metadata": {}, 120 | "output_type": "execute_result" 121 | } 122 | ], 123 | "source": [ 124 | "K1" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 12, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "The hexadecimal equivalent of SHA256 is : \n", 137 | "fd9c19f47a75d66361a93af14fb98d9419794da15d9244dffaf2b4835d57efdf\n" 138 | ] 139 | } 140 | ], 141 | "source": [ 142 | "import hashlib \n", 143 | "SK1 = hashlib.sha256(K1.encode()) \n", 144 | "\n", 145 | "print(\"The hexadecimal equivalent of SHA256 is : \") \n", 146 | "print(SK1.hexdigest()) " 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 13, 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [ 155 | "# AES 256 in OFB mode:\n", 156 | "from Crypto.Cipher import AES\n", 157 | "from Crypto.Random import new as Random\n", 158 | "from hashlib import sha256\n", 159 | "from base64 import b64encode,b64decode\n", 160 | "\n", 161 | "class AESCipher:\n", 162 | " def __init__(self,data,key):\n", 163 | " self.block_size = 16\n", 164 | " self.data = data\n", 165 | " self.key = sha256(key.encode()).digest()[:32]\n", 166 | " self.pad = lambda s: s + (self.block_size - len(s) % self.block_size) * chr (self.block_size - len(s) % self.block_size)\n", 167 | " self.unpad = lambda s: s[:-ord(s[len(s) - 1:])]\n", 168 | "\n", 169 | " def decrypt(self):\n", 170 | " cipher_text = b64decode(self.data.encode())\n", 171 | " iv = cipher_text[:self.block_size]\n", 172 | " cipher = AES.new(self.key,AES.MODE_OFB,iv)\n", 173 | " return self.unpad(cipher.decrypt(cipher_text[self.block_size:])).decode()" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 14, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "import pandas as pd" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 15, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [ 191 | "xdf = pd.DataFrame(columns = ['1','2'])\n", 192 | "a = []\n", 193 | "b = []\n", 194 | "for i in P:\n", 195 | " k = 0\n", 196 | " n1 = []\n", 197 | " n2 = []\n", 198 | " for j in i:\n", 199 | " if k%2==0:\n", 200 | " n1.append(np.sum(j))\n", 201 | " else:\n", 202 | " n2.append(np.sum(j))\n", 203 | " k += 1 \n", 204 | " a.append(sum(n1))\n", 205 | " b.append(sum(n2))\n", 206 | "xdf['1'] = a\n", 207 | "xdf['2'] = b" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 16, 213 | "metadata": {}, 214 | "outputs": [], 215 | "source": [ 216 | "ydf = pd.DataFrame(columns = ['1','2'])\n", 217 | "a = []\n", 218 | "b = []\n", 219 | "for i in R:\n", 220 | " k = 0\n", 221 | " n1 = []\n", 222 | " n2 = []\n", 223 | " for j in i:\n", 224 | " if k%2==0:\n", 225 | " n1.append(np.sum(j))\n", 226 | " else:\n", 227 | " n2.append(np.sum(j))\n", 228 | " k += 1 \n", 229 | " a.append(sum(n1))\n", 230 | " b.append(sum(n2))\n", 231 | "ydf['1'] = a\n", 232 | "ydf['2'] = b" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 17, 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [ 241 | "from sklearn.linear_model import LinearRegression" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 18, 247 | "metadata": {}, 248 | "outputs": [ 249 | { 250 | "data": { 251 | "text/plain": [ 252 | "LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)" 253 | ] 254 | }, 255 | "execution_count": 18, 256 | "metadata": {}, 257 | "output_type": "execute_result" 258 | } 259 | ], 260 | "source": [ 261 | "LRmodel = LinearRegression()\n", 262 | "LRmodel.fit(xdf,ydf)" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": 19, 268 | "metadata": {}, 269 | "outputs": [], 270 | "source": [ 271 | "zdf = pd.DataFrame(columns = ['1','2'])\n", 272 | "a = []\n", 273 | "b = []\n", 274 | "for i in CK:\n", 275 | " k = 0\n", 276 | " n1 = []\n", 277 | " n2 = []\n", 278 | " for j in i:\n", 279 | " if k%2==0:\n", 280 | " n1.append(np.sum(j))\n", 281 | " else:\n", 282 | " n2.append(np.sum(j))\n", 283 | " k += 1 \n", 284 | " a.append(sum(n1))\n", 285 | " b.append(sum(n2))\n", 286 | "zdf['1'] = a\n", 287 | "zdf['2'] = b" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 20, 293 | "metadata": {}, 294 | "outputs": [], 295 | "source": [ 296 | "predict = LRmodel.predict([[sum(zdf['1']),sum(zdf['2'])]])" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 21, 302 | "metadata": {}, 303 | "outputs": [], 304 | "source": [ 305 | "x = round(predict[0][0])%26\n", 306 | "y = round(predict[0][1])%26" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 22, 312 | "metadata": {}, 313 | "outputs": [], 314 | "source": [ 315 | "cipher = cipher.split(' ')" 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": 32, 321 | "metadata": {}, 322 | "outputs": [], 323 | "source": [ 324 | "txt = []\n", 325 | "for each in cipher:\n", 326 | " try:\n", 327 | " ch = ord(each) - x + y\n", 328 | " txt.append(int(ch))\n", 329 | " except:\n", 330 | " print(each)" 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": 33, 336 | "metadata": {}, 337 | "outputs": [], 338 | "source": [ 339 | "text = \"\"\n", 340 | "for t in txt:\n", 341 | " text += chr(t)" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": 35, 347 | "metadata": {}, 348 | "outputs": [], 349 | "source": [ 350 | "de = AESCipher(text,SK1.hexdigest()).decrypt()" 351 | ] 352 | }, 353 | { 354 | "cell_type": "code", 355 | "execution_count": 36, 356 | "metadata": {}, 357 | "outputs": [], 358 | "source": [ 359 | "de = de.encode(\"utf-8\")" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 37, 365 | "metadata": {}, 366 | "outputs": [], 367 | "source": [ 368 | "with open(\"DecryptedImg2.png\", \"wb\") as fh:\n", 369 | " fh.write(base64.decodebytes(de))" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": null, 375 | "metadata": {}, 376 | "outputs": [], 377 | "source": [] 378 | } 379 | ], 380 | "metadata": { 381 | "kernelspec": { 382 | "display_name": "Python 3", 383 | "language": "python", 384 | "name": "python3" 385 | }, 386 | "language_info": { 387 | "codemirror_mode": { 388 | "name": "ipython", 389 | "version": 3 390 | }, 391 | "file_extension": ".py", 392 | "mimetype": "text/x-python", 393 | "name": "python", 394 | "nbconvert_exporter": "python", 395 | "pygments_lexer": "ipython3", 396 | "version": "3.7.3" 397 | } 398 | }, 399 | "nbformat": 4, 400 | "nbformat_minor": 4 401 | } 402 | -------------------------------------------------------------------------------- /Project Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riyag283/Image-encryption/01aa0ee645925d42bc0df7cb53d152dde409e17f/Project Report.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Image-encryption 2 | Image Encryption Algorithm implementation using AES and Visual Cryptography 3 | 4 | Refer to the paper: "A Novel Image Encryption Algorithm using AES and Visual Cryptography" published in 2016 by IEEE. (2016 2nd International Conference on Next Generation Computing Technologies) 5 | -------------------------------------------------------------------------------- /ToBeSent for Decryption/P.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riyag283/Image-encryption/01aa0ee645925d42bc0df7cb53d152dde409e17f/ToBeSent for Decryption/P.png -------------------------------------------------------------------------------- /ToBeSent for Decryption/P2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riyag283/Image-encryption/01aa0ee645925d42bc0df7cb53d152dde409e17f/ToBeSent for Decryption/P2.png -------------------------------------------------------------------------------- /ToBeSent for Decryption/R.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riyag283/Image-encryption/01aa0ee645925d42bc0df7cb53d152dde409e17f/ToBeSent for Decryption/R.png -------------------------------------------------------------------------------- /ToBeSent for Decryption/R2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riyag283/Image-encryption/01aa0ee645925d42bc0df7cb53d152dde409e17f/ToBeSent for Decryption/R2.png -------------------------------------------------------------------------------- /keys/mykey.txt: -------------------------------------------------------------------------------- 1 | Hello, World. ThisIsMyKey. -------------------------------------------------------------------------------- /pics/test1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riyag283/Image-encryption/01aa0ee645925d42bc0df7cb53d152dde409e17f/pics/test1.jpg -------------------------------------------------------------------------------- /pics/test2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riyag283/Image-encryption/01aa0ee645925d42bc0df7cb53d152dde409e17f/pics/test2.jpg -------------------------------------------------------------------------------- /pics/test3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riyag283/Image-encryption/01aa0ee645925d42bc0df7cb53d152dde409e17f/pics/test3.jpg --------------------------------------------------------------------------------