has12zen commited on
Commit
de48b2d
1 Parent(s): d1836d1

new encryption model

Browse files
Files changed (4) hide show
  1. __pycache__/utils2.cpython-39.pyc +0 -0
  2. app.py +4 -4
  3. requirements.txt +2 -1
  4. utils2.py +155 -0
__pycache__/utils2.cpython-39.pyc ADDED
Binary file (3.14 kB). View file
 
app.py CHANGED
@@ -1,5 +1,5 @@
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.")
@@ -8,18 +8,18 @@ with gr.Blocks() as demo:
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();
 
1
  import gradio as gr
2
+ from utils2 import *
3
 
4
  with gr.Blocks() as demo:
5
  gr.Markdown("# Encrypt the secret message into image.")
 
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(type="pil")
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(type="pil")
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(share=False);
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- pillow
 
 
1
+ pillow
2
+ gradio
utils2.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+
3
+ prime = 19
4
+ p_base = 1
5
+ arr1=[]
6
+ arr2=[]
7
+ gidx =0
8
+ msglog1 = []
9
+ gidxlog1 =[]
10
+ msglog2 = []
11
+ gidxlog2 =[]
12
+ base_log1 = []
13
+ base_log2 = []
14
+ def reverse_slicing(s):
15
+ return s[::-1]
16
+
17
+ def getbinary(msg):
18
+ binary = ""
19
+ for char in msg:
20
+ pc = format(ord(char), '08b')
21
+ c = pc[1:]
22
+ c = reverse_slicing(c)
23
+ binary += c
24
+ return binary
25
+
26
+ def getSkips(char):
27
+ global p_base
28
+ global prime
29
+ p_base = (p_base*prime)%100;
30
+ first = p_base%10+1;
31
+ tmp = int(p_base/10)
32
+ second=tmp%10+1;
33
+ if(char=='0'):
34
+ return first
35
+ return second
36
+
37
+ def iterateSecret(binary_secret):
38
+ global gidx
39
+ t = binary_secret[gidx];
40
+ m = getSkips(t);
41
+ gidx=(gidx+1)%len(binary_secret);
42
+ return m;
43
+
44
+ def unravel(bt):
45
+ text = ""
46
+ for i in range(0, len(bt), 7):
47
+ tc= bt[i:i+7]
48
+ tc = reverse_slicing(tc)
49
+ tc = '0'+tc
50
+ text +=tc;
51
+ final=''
52
+ for i in range(0, len(text), 8):
53
+ final += chr(int(text[i:i+8], 2))
54
+ return final
55
+
56
+
57
+ def encrypt(text,key, image):
58
+ global arr1, gidx,msglog1,gidxlog1,p_base,base_log1
59
+ p_base = 1
60
+ final=''
61
+ pc=''
62
+ for cz in text:
63
+ pc += format(ord(cz), '08b')
64
+ c = pc[1:]
65
+ for i in range(0, len(pc), 8):
66
+ final += chr(int(pc[i:i+8], 2))
67
+ print(final)
68
+ gidx = 0;
69
+ pixels = list(image.getdata())
70
+ binary_msg = getbinary(text)
71
+ binary_secret = getbinary(key)
72
+ mod = len(pixels)
73
+ i=0
74
+ base_log1.clear();
75
+ gidxlog1.clear();
76
+ msglog1.clear();
77
+ arr1.clear()
78
+ base_log1+=[p_base]
79
+ binary_msg+='011111111'
80
+ print(binary_msg)
81
+ skips = 0;
82
+ new_pixels = []
83
+ for rgb in pixels:
84
+ tcr = []
85
+ for clr in rgb:
86
+ if skips == 0:
87
+ skips+=iterateSecret(binary_secret)
88
+ gidxlog1+=[gidx]
89
+ base_log1+=[p_base]
90
+ msglog1+=[binary_secret[gidx]]
91
+ arr1+=[skips]
92
+ if i < len(binary_msg):
93
+ if binary_msg[i]=='0':
94
+ if clr%2!=0:
95
+ clr-=1
96
+ else:
97
+ if clr%2==0:
98
+ clr+=1
99
+ i+=1
100
+ else:
101
+ skips-=1;
102
+ tcr+=[clr]
103
+ new_pixels += [tuple(tcr)]
104
+ new_image = Image.new(image.mode, image.size)
105
+ new_image.putdata(new_pixels)
106
+ return new_image
107
+
108
+ def decrypt(key,image):
109
+ global arr2,gidxlog2,msglog2,p_base,base_log2,gidx
110
+ p_base = 1
111
+ gidx = 0
112
+ pixels = list(image.getdata())
113
+ base_log2.clear()
114
+ msglog2.clear()
115
+ gidxlog2.clear()
116
+ arr2.clear()
117
+ base_log2+=[p_base]
118
+ binary_secret = getbinary(key)
119
+ ones =0;
120
+ skips = 0;
121
+ binary_msg = ""
122
+ flag =0
123
+ for rgb in pixels:
124
+ for clr in rgb:
125
+ if skips ==0:
126
+ cnt = clr%2;
127
+ if cnt ==1:
128
+ ones+=1;
129
+ else:
130
+ ones=0;
131
+ binary_msg += str(cnt);
132
+ if ones ==8:
133
+ flag =1;
134
+ break
135
+ skips+=iterateSecret(binary_secret)
136
+ gidxlog2+=[gidx]
137
+ base_log2+=[p_base]
138
+ msglog2+=[binary_secret[gidx]]
139
+ arr2+=[skips]
140
+ else:
141
+ skips-=1
142
+ if flag ==1:
143
+ break;
144
+ print(binary_msg)
145
+ binary_msg = binary_msg[:-9]
146
+ print(binary_msg)
147
+ txt = unravel(binary_msg)
148
+ print(txt)
149
+ print(arr1[:10],arr2[:10])
150
+ print(msglog1[:10],gidxlog1[:10])
151
+ print(msglog2[:10],gidxlog2[:10]);
152
+ print(base_log1[0:10],base_log2[0:10])
153
+ return txt
154
+
155
+