@SureshPatil/

SteelblueSturdyDisplaymanager

Python

No description

fork
loading
Files
  • main.py
  • Disco.bmp
  • DiscoModified.bmp
  • Lego.bmp
  • logo1.bmp
  • logo2.bmp
  • Mystery1.bmp
  • Mystery2.bmp
  • NotMystery1.bmp
  • NotMystery2.bmp
  • SC.bmp
  • SCModified.bmp
  • SCModified2.bmp
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from PIL import Image

def multiply_2x1(matrix1,matrix2, m):
  newmatrix = [(matrix1[0][0]*matrix2[0] + matrix1[0][1]*matrix2[1])%m, (matrix1[1][0]*matrix2[0] + matrix1[1][1]*matrix2[1])%m]
  return newmatrix

def encryptImage(im, shift):
  for j in range(0, im.size[0]):
    for i in range(0, im.size[1]):
      im.putpixel((j, i), (im.getpixel((j, i)) + shift) % 256)
  return im

def determinant(matrix):
  return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]

def mod_inverse(a, m):
  for x in range(1,m):
    if((a*x)%m == 1):
      return x
  #print("No modular inverse found!")
  return -1

def inverseOfMatrix(matrix, m):
  det = determinant(matrix)%m
  modInvDet = mod_inverse(det, m)
  if modInvDet == -1:
    return -1
  newMatrix = [[(matrix[1][1]*modInvDet)%m, (-modInvDet*matrix[0][1])%m], [(-modInvDet*matrix[1][0])%m, (matrix[0][0]*modInvDet)%m]]
  return newMatrix

def hillEncryptImage(im, matrix, m):
  invMatrix = inverseOfMatrix(matrix, m)
  if invMatrix == -1:
    return 'Error: Matrix not Invertible!'
  for j in range(0, im.size[1]):
    for i in range(0, im.size[0], 2):
      ogMatrix = [im.getpixel((i, j)), im.getpixel((i+1, j))]
      cipherMatrix = multiply_2x1(matrix, ogMatrix, m)
      im.putpixel((i, j), cipherMatrix[0])
      im.putpixel((i+1, j), cipherMatrix[1])
  return im

def hillDecryptImage(im, matrix, m):
  invMatrix = inverseOfMatrix(matrix, m)
  if invMatrix == -1:
    return 'Error: Matrix not Invertible!'
  for j in range(0, im.size[1]):
    for i in range(0, im.size[0], 2):
      cipherMatrix = [im.getpixel((i, j)), im.getpixel((i+1, j))]
      ogMatrix = multiply_2x1(invMatrix, cipherMatrix, m)
      im.putpixel((i, j), ogMatrix[0])
      im.putpixel((i+1, j), ogMatrix[1])
  return im

def hillEncryptImageAug(im, matrix, vector, m):
  invMatrix = inverseOfMatrix(matrix, m)
  if invMatrix == -1:
    return 'Error: Matrix not Invertible!'
  for j in range(0, im.size[1]):
    boolean = True
    for i in range(0, im.size[0], 2):
      ogMatrix = [im.getpixel((i, j)), im.getpixel((i+1, j))]
      cipherMatrix = multiply_2x1(matrix, ogMatrix, m)
      im.putpixel((i, j), cipherMatrix[0])
      im.putpixel((i+1, j), cipherMatrix[1])
      if boolean:
        matrix = [[(matrix[0][0]*vector[0])%m, (matrix[0][1]*vector[1])%m],[matrix[1][0], matrix[1][1]]]
        boolean = False
      else:
        matrix = [[matrix[0][0], matrix[0][1]],[(matrix[1][0]*vector[0])%m, (matrix[1][1]*vector[1])%m]]  
        boolean = True     
  return im

def hillDecryptImageAug(im, matrix, vector, m):
  invMatrix = inverseOfMatrix(matrix, m)
  if invMatrix == -1:
    return 'Error: Matrix not Invertible!'
  for j in range(0, im.size[1]):
    boolean = True
    for i in range(0, im.size[0], 2):
      cipherMatrix = [im.getpixel((i, j)), im.getpixel((i+1, j))]
      ogMatrix = multiply_2x1(inverseOfMatrix(matrix, m), cipherMatrix, m)
      im.putpixel((i, j), ogMatrix[0])
      im.putpixel((i+1, j), ogMatrix[1])
      if boolean:
        matrix = [[(matrix[0][0]*vector[0])%m, (matrix[0][1]*vector[1])%m],[matrix[1][0], matrix[1][1]]]
        boolean = False
      else:
        matrix = [[matrix[0][0], matrix[0][1]],[(matrix[1][0]*vector[0])%m, (matrix[1][1]*vector[1])%m]]  
        boolean = True         
  return im      




# im = Image.open("Disco.bmp")    
# im = encryptImage(im, 53)
# im.save("DiscoModified.bmp")

# im = Image.open("SC.bmp")
# im = hillEncryptImage(im, [[2, 5], [3, 20]], 256)
# im.save("SCModified.bmp")

# im = Image.open("Mystery1.bmp")
# im = hillDecryptImage(im, [[2, 5], [3, 20]], 256)
# im.save("NotMystery1.bmp")

# im = Image.open("SC.bmp")
# im = hillEncryptImageAug(im, [[2, 5], [3, 20]], [3, 5], 256)
# im.save("SCModified2.bmp")

# im = Image.open("Mystery2.bmp")
# im = hillDecryptImageAug(im, [[2, 5], [3, 20]], [101, 141], 256)
# im.save("NotMystery2.bmp")

im = Image.open("logo2.bmp")
im = hillEncryptImageAug(im, [[2, 5], [3, 20]], [101, 141], 256)
im.save("notLogo2.bmp")

# print(im.size)
# print(im.getpixel((499,341)))   
# im.putpixel((499,341),0)       
# im.save("DiscoModified.bmp")

# im = Image.open("Disco.bmp").convert("L") 
# im.save("DiscoModified.bmp")