|
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 |
|
|
|
|
|
|