File size: 5,094 Bytes
de48b2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10ce6b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de48b2d
 
 
 
 
 
 
 
10ce6b2
 
de48b2d
 
10ce6b2
de48b2d
 
 
10ce6b2
de48b2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10ce6b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de48b2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from PIL import Image

prime = 19
p_base = 1
arr1=[]
arr2=[]
gidx =0
msglog1 = []
gidxlog1 =[]
msglog2 = []
gidxlog2 =[]
base_log1 = []
base_log2 = []
def reverse_slicing(s):
    return s[::-1]

def getbinary(msg):
  binary = ""
  for char in msg:
    pc = format(ord(char), '08b')
    c = pc[1:]
    c = reverse_slicing(c)
    binary += c
  return binary

def getSkips(char):
    global p_base
    global prime
    p_base = (p_base*prime)%100;
    first = p_base%10+1;
    tmp = int(p_base/10)
    second=tmp%10+1;
    if(char=='0'):
        return first
    return second

def iterateSecret(binary_secret):
    global gidx
    t = binary_secret[gidx];
    m = getSkips(t);
    gidx=(gidx+1)%len(binary_secret);
    return m;

def unravel(bt):
    text = ""
    for i in range(0, len(bt), 7):
        tc= bt[i:i+7]
        tc = reverse_slicing(tc)
        tc = '0'+tc
        text +=tc;
    final=''
    for i in range(0, len(text), 8):
        final += chr(int(text[i:i+8], 2))
    return final
    

def encrypt(text,key, image):
    global arr1, gidx,msglog1,gidxlog1,p_base,base_log1
    p_base = 1
    final=''
    pc=''
    for cz in text:
        pc += format(ord(cz), '08b')
    c = pc[1:]
    for i in range(0, len(pc), 8):
        final += chr(int(pc[i:i+8], 2))
    print(final)
    gidx = 0;
    pixels = list(image.getdata())
    binary_msg = getbinary(text)
    binary_secret = getbinary(key)
    mod = len(pixels)
    i=0
    base_log1.clear();
    gidxlog1.clear();
    msglog1.clear();
    arr1.clear()
    base_log1+=[p_base]
    binary_msg+='011111111'
    print(binary_msg)
    skips = 0;
    new_pixels = []
    for rgb in pixels:
        if type(rgb) == tuple:
            tcr = []
            for clr in rgb:
                if skips == 0:
                    skips+=iterateSecret(binary_secret)
                    gidxlog1+=[gidx]
                    base_log1+=[p_base]
                    msglog1+=[binary_secret[gidx]]
                    arr1+=[skips]
                    if i < len(binary_msg):
                        if binary_msg[i]=='0':
                            if clr%2!=0:
                                clr-=1
                        else:
                            if clr%2==0:
                                clr+=1
                        i+=1
                else:
                    skips-=1;
                tcr+=[clr]
            new_pixels += [tuple(tcr)]
        elif type(rgb) == int:
            clr = rgb
            if skips == 0:
                skips+=iterateSecret(binary_secret)
                gidxlog1+=[gidx]
                base_log1+=[p_base]
                msglog1+=[binary_secret[gidx]]
                arr1+=[skips]
                if i < len(binary_msg):
                    if binary_msg[i]=='0':
                        if clr%2!=0:
                            clr-=1
                    else:
                        if clr%2==0:
                            clr+=1
                    i+=1
            else:
                skips-=1;
            new_pixels+=[clr]
    new_image = Image.new(image.mode, image.size)
    new_image.putdata(new_pixels)
    return new_image

def decrypt(key,image):
    global arr2,gidxlog2,msglog2,p_base,base_log2,gidx
    p_base = 1
    gidx = 0
    pixels = list(image.getdata())
    base_log2.clear()
    msglog2.clear()
    gidxlog2.clear()
    arr2.clear()
    base_log2+=[p_base]
    binary_secret = getbinary(key)
    ones =0;
    skips = 0;
    binary_msg = ""
    flag =0
    for rgb in pixels:
        if type(rgb) == tuple:  
            for clr in rgb:
                if skips ==0:
                    cnt = clr%2;
                    if cnt ==1:
                        ones+=1;
                    else:
                        ones=0; 
                    binary_msg += str(cnt);
                    if ones ==8:
                        flag =1;
                        break
                    skips+=iterateSecret(binary_secret)
                    gidxlog2+=[gidx]
                    base_log2+=[p_base]
                    msglog2+=[binary_secret[gidx]]
                    arr2+=[skips]
                else:
                    skips-=1
        elif type(rgb) == int:
            clr = rgb
            if skips ==0:
                cnt = clr%2;
                if cnt ==1:
                    ones+=1;
                else:
                    ones=0; 
                binary_msg += str(cnt);
                if ones ==8:
                    flag =1;
                    break
                skips+=iterateSecret(binary_secret)
                gidxlog2+=[gidx]
                base_log2+=[p_base]
                msglog2+=[binary_secret[gidx]]
                arr2+=[skips]
            else:
                skips-=1
        if flag ==1:
            break;
    print(binary_msg)
    binary_msg = binary_msg[:-9]
    print(binary_msg)
    txt = unravel(binary_msg)
    print(txt)
    print(arr1[:10],arr2[:10])
    print(msglog1[:10],gidxlog1[:10])
    print(msglog2[:10],gidxlog2[:10]);
    print(base_log1[0:10],base_log2[0:10])
    return txt