-
Notifications
You must be signed in to change notification settings - Fork 0
/
pic.py
117 lines (81 loc) · 3.29 KB
/
pic.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
import os #The OS module in Python provides functions for interacting with the operating system.
from PIL import Image #PIL is the python image library which needs to be preinstalled before you can run this code
import random
import feistel
chr_ascii={} #dictionary containing characters as keys and ascii values as values
ascii_chr={} #dictionary containing ascii values as keys and characters as values
for i in range(0,256):
chr_ascii[chr(i)]=i
ascii_chr[i]=chr(i)
def encrypt():
img=input("\nEnter the name of the image you want the message to be encoded in, with the extension\n\n")
path=os.path.abspath(img) #this gives the absolute path of the given file
image=Image.open(path)
height=image.height
width=image.width
password=input("\n\nEnter security key\n\n")
user=input("\n\nEnter the message you wanna encode\n\n")
#Feistel encryption begins
message = feistel.text_process(user)
length = len(message)
k1 = feistel.key_gen(length, length)
k2 = feistel.key_gen(length/2, length)
text = feistel.feistel_encrypt(message, k1, k2) #Encrypted text which will be hidden in image
#Image steganography from here onwards
p=0
j=0
z=0
text_length=len(text)
image.putpixel((0,0),(text_length//255, text_length%255,0))
random.seed(password)
x = random.sample(range(1,width+1), text_length)
y = random.sample(range(1, height+1), text_length)
coor = list(zip(x,y))
for i in range(text_length):
RGB=image.getpixel(coor[i])
rgb_mutable=list(RGB)
rgb_mutable[z]=chr_ascii[text[p]]^chr_ascii[password[j]]
rgb_tuple=tuple(rgb_mutable)
image.putpixel(coor[i],rgb_tuple)
z=(z+1)%3
j=(j+1)%len(password)
p+=1
image.show()
image.save(img[:-4]+"_encrypt.png")
print("\n\nData hiding in image is successful\n\n")
def decrypt():
img=input("\nEnter the name of the image you want the message to be decoded from, with the extension\n\n")
path=os.path.abspath(img) #this gives the absolute path of the given file
image=Image.open(path)
height=image.height
width=image.width
j1=0
z1=0
password=input("\n\nEnter your key\n\n")
message=''
raw=list(image.getpixel((0,0)))
length = raw[0]*255 + raw[1]
random.seed(password)
x = random.sample(range(1,width+1), length)
y = random.sample(range(1, height+1), length)
coor = list(zip(x,y))
for i in range(length):
RGB2=image.getpixel(coor[i])
coded=RGB2[z1]
message+=ascii_chr[coded^chr_ascii[password[j1]]]
z1=(z1+1)%3
j1=(j1+1)%len(password)
#Feistel decryption begins
message = feistel.text_process(message)
length = len(message)
k1 = feistel.key_gen(length, length)
k2 = feistel.key_gen(length/2, length)
ans = feistel.feistel_decrypt(message, k1, k2)
print("\n\nEncrypted text was\n\n", ans)
user = input("Type 'encrypt' if you want encryption \nType 'decrypt' if you want decryption\n" )
if user == 'encrypt':
encrypt()
elif user == 'decrypt':
decrypt()
else:
print("Invalid entry")