has12zen commited on
Commit
d29aad7
1 Parent(s): 978b0ce

Add application file

Browse files
Files changed (4) hide show
  1. __pycache__/utils.cpython-39.pyc +0 -0
  2. app.py +25 -0
  3. requirements.txt +1 -0
  4. utils.py +115 -0
__pycache__/utils.cpython-39.pyc ADDED
Binary file (2.26 kB). View file
 
app.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from utils import *
3
+
4
+ with gr.Blocks() as demo:
5
+ gr.Markdown("# Encrypt the secret message into image.")
6
+ with gr.Tab("Encrypt"):
7
+ with gr.Row():
8
+ with gr.Column():
9
+ encrypt_msg = gr.Textbox(lines=1, label="Encrypt Message")
10
+ encrypt_key = gr.Textbox(lines=1, label="Encrypt Key")
11
+ encrypt_image = gr.Image()
12
+ encrypt_output = gr.Image()
13
+ encrypt_button = gr.Button("Encrypt")
14
+ with gr.Tab("Decrypt"):
15
+ with gr.Row():
16
+ with gr.Column():
17
+ decrypt_key = gr.Textbox(lines=1, label="Decrypt Key")
18
+ decrypt_image = gr.Image()
19
+ decrypt_output = gr.Textbox(lines=1, label="Decrypt Message")
20
+ decrypt_button = gr.Button("Decrypt")
21
+
22
+ encrypt_button.click(encrypt, inputs=[encrypt_msg, encrypt_key, encrypt_image], outputs=[encrypt_output])
23
+ decrypt_button.click(decrypt, inputs=[decrypt_key, decrypt_image], outputs=[decrypt_output])
24
+
25
+ demo.launch();
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ pillow
utils.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+
3
+ def encrypt(text, secret_key, img):
4
+ image = Image.fromarray(img)
5
+ pixels = list(image.getdata())
6
+ binary_text = ''
7
+ for char in text:
8
+ binary_text+=format(ord(char), '08b')
9
+ binary_secret = ''
10
+ for char in secret_key:
11
+ binary_secret+=format(ord(char), '08b')
12
+ # print(binary_text)
13
+ binary_text = binary_demorf(binary_text, binary_secret)
14
+ binary_text = binary_text + binary_secret
15
+ # print(binary_text)
16
+ i = 0
17
+ print("number of bits we can store (binary_text + binary_secret): ",3*len(pixels))
18
+ new_pixels = []
19
+ for rgb in pixels:
20
+ tcr = []
21
+ for clr in rgb:
22
+ if i < len(binary_text):
23
+ if binary_text[i] == '0':
24
+ if clr % 2 != 0:
25
+ clr -= 1
26
+ else:
27
+ if clr % 2 == 0:
28
+ clr += 1
29
+ i += 1
30
+ tcr += [clr]
31
+ new_pixels += [tuple(tcr)]
32
+ new_image = Image.new(image.mode, image.size)
33
+ new_image.putdata(new_pixels)
34
+ return new_image
35
+
36
+ def decrypt(secret_key, img):
37
+ image = Image.fromarray(img)
38
+ pixels = list(image.getdata())
39
+ binary_secret = ""
40
+ for(char) in secret_key:
41
+ binary_secret += format(ord(char), '08b')
42
+ binary_text = ""
43
+ for rgb in pixels:
44
+ for(clr) in rgb:
45
+ if clr % 2 == 0:
46
+ binary_text += '0'
47
+ else:
48
+ binary_text += '1'
49
+
50
+ # print(binary_secret)
51
+ text_pos = binary_text.split(binary_secret)
52
+ if len(text_pos) == 1:
53
+ return "No text found"
54
+ else :
55
+ bt = text_pos[0];
56
+ # print(bt)
57
+ bt = binary_morf(bt, binary_secret)
58
+ # print(bt)
59
+ text = ""
60
+ for i in range(0, len(bt), 8):
61
+ text += chr(int(bt[i:i+8], 2))
62
+ return text
63
+
64
+ def binary_demorf(binary_text,binary_secret):
65
+ if len(binary_text) < len(binary_secret):
66
+ return binary_text
67
+ text = ""
68
+ cidx =0
69
+ idx = 0
70
+ while idx < len(binary_text):
71
+ ch = binary_text[idx]
72
+ if cidx < len(binary_secret):
73
+ if binary_secret[cidx] == ch:
74
+ if cidx == len(binary_secret)-1:
75
+ cidx = 0
76
+ if ch == '0':
77
+ text += '1'
78
+ else:
79
+ text += '0'
80
+ else:
81
+ text += ch
82
+ cidx += 1
83
+ idx += 1
84
+ else:
85
+ if(binary_secret[0]==ch):
86
+ cidx=1
87
+ else:
88
+ cidx=0
89
+ text += ch
90
+ idx+=1
91
+ else:
92
+ text += ch
93
+ idx+=1
94
+ return text
95
+
96
+ def binary_morf(binary_text,binary_secret):
97
+ if len(binary_text) < len(binary_secret):
98
+ return binary_text
99
+ t = binary_secret[:-1] # secret without the last digit
100
+ l = binary_text.split(t) # split text with that
101
+ # print(l)
102
+ result = ''
103
+ i = 0;
104
+ while i < len(l):
105
+ cr = ""
106
+ if(i!=0):
107
+ cr = l[i][1:]
108
+ else:
109
+ cr = l[i]
110
+ if i != len(l)-1:
111
+ result += cr + binary_secret;
112
+ else:
113
+ result += cr
114
+ i+=1
115
+ return result