-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
218 lines (177 loc) Β· 8.7 KB
/
app.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# -*- coding: utf-8 -*-
from flask import Flask, render_template, request, flash, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename
import os
from stegano import lsb
from ModifiedPlayfair import ModifiedPlayfair
import chardet
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
import codecs
app = Flask(__name__)
UPLOAD_FOLDER = 'OriginalImages/'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
################# Utility Functions #######################
@app.route('/')
def index():
return render_template('index.html')
@app.route('/senderurl')
def sender():
return render_template('sender.html')
@app.route('/receiverurl')
def receiver():
return render_template('receiver.html')
def generate_keys():
# key length must be a multiple of 256 and >= 1024
key = RSA.generate(1024)
publickey = key.publickey()
return key.exportKey(), publickey.exportKey() #returns privetkey and public key
@app.route('/generate_receiver_keys')
def generate_receiver_keys():
######### RSA started #############
privatekey, publickey = generate_keys()
print("Public Key: ", publickey)
print("\n\nPrivate Key: ", privatekey)
return render_template('display_recvr_keys.html', privatekey=privatekey, publickey=publickey)
@app.route('/receiverside_decrypt')
def receiver_decrypt():
return render_template('receiver_decrypt.html')
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
###########################################################
@app.route('/accept_sender_data', methods = ['GET', 'POST'])
def accept_sender_data():
if request.method == 'POST':
print('line 55')
############## SENDER SIDE CODE ###############
f = request.files['uploaded_image']
if f and allowed_file(f.filename):
filename = secure_filename(f.filename)
f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
else:
return 'File format not supported!!'
####### Photo uploded ######
print('line 58')
plaintext = request.form['plaintext']
print('Plaintext : ',plaintext)
playfairkey = str(request.form['playfairkey'])
print('Playfairkey : ',playfairkey)
receiver_publickey = request.form['receiver_publickey']
#print('Receiver publickey : ',receiver_publickey)
### All data collected from front end ########
print('line 75')
objModifiedPlayfair = ModifiedPlayfair() #creating obj of modified playfair class
objModifiedPlayfair.getKey(playfairkey) # passing playfair key to that class
objModifiedPlayfair.getPlainText(plaintext) # passing plaintext to that class
objModifiedPlayfair.preparePairs()
objModifiedPlayfair.generateKeyTable()
objModifiedPlayfair.encrypt()
cipher_from_playfair = objModifiedPlayfair.getCipherText() ####### 1 (string)
######### Playfiar completed ############
print('line 84')
########## RSA Started ################
# encrypt() : Requires plaintext in bytes form only.
print(type(receiver_publickey))
print(receiver_publickey.encode('utf-8').replace(b'\\n', b'\n'))
pobj = RSA.importKey(receiver_publickey.encode('utf-8').replace(b'\\n', b'\n'))
pobj = PKCS1_OAEP.new(pobj)
print('RSA Cipher of playfairkey : ',pobj.encrypt(playfairkey.encode('utf-8'))) #print bytes in string form
rsacipher_for_playfairKey = pobj.encrypt(playfairkey.encode('utf-8')) #### 2 (rsacipher_for_playfairKey -> unique type che bytes)
########## RSA completed ############
print("RSA Cipher: ", rsacipher_for_playfairKey)
print("\nRSA Cipher Length: ", len(rsacipher_for_playfairKey))
print('line 100')
########## R transposition Started ####
playfair_rsa_combined_cipher = cipher_from_playfair + "||||" + str(rsacipher_for_playfairKey) # bytes to string
ptl = list(playfair_rsa_combined_cipher)
fl = []
bl = []
# print("Length : ", len(ptl))
for i in range(0,len(ptl)):
if i%2 == 0:
# print("i: ", i)
fl.append(playfair_rsa_combined_cipher[i])
else:
# print("i: ", i)
bl.insert(0,playfair_rsa_combined_cipher[i])
templist = fl + bl
ciphertext = ''.join(templist)
ciphertext = ciphertext[::-1]
print('Combined (RSA + Playfair) Cipher Text:', ciphertext)
########## R transposition completed ###########
print('line 122')
########### stegnography Encryption ###############
imgname = os.path.join(app.config['UPLOAD_FOLDER'], filename)
secret = lsb.hide( imgname, ciphertext)
imgextension = filename.split('.')[1]
# Downloading final encrypted image
print('line 137')
uploads = os.path.join(app.root_path, app.config['UPLOAD_FOLDER'])
secret.save('EncryptedImages/'+'EncryptedImg'+'.'+imgextension)
return send_from_directory(directory= "EncryptedImages/", filename='EncryptedImg'+'.'+imgextension)
#print('Encrypted image successfully downloaded at sender end!')
#return render_template('index.html')
@app.route('/accept_receiver_data', methods = ['GET', 'POST'])
def accept_receiver_data():
filename = ''
if request.method == 'POST':
#Reading the encrypted image
f = request.files['encrypted_image']
if f and allowed_file(f.filename):
filename = secure_filename(f.filename)
f.save(os.path.join('EncryptedImages/', filename))
print('Encrypted image successfully uploaded!')
else:
return 'File format not supported!!'
receiver_privatekey = request.form['receiver_privatekey']
print('receiver_privatekey:',receiver_privatekey)
print('Line 161')
############# Decryption Process Started ##################
imgextension = filename.split('.')[1]
img_message = lsb.reveal("EncryptedImages//EncryptedImg"+'.'+imgextension)
print('Retrived msg from image :',img_message)
print('Line 163')
# decrypting rohit transposition #
ciphertext = img_message
ciphertext = ciphertext[::-1]
startptr = 0
endptr = len(ciphertext) - 1
decryptedtext = ""
while startptr <= endptr:
if startptr != endptr:
decryptedtext = decryptedtext + ciphertext[startptr] + ciphertext[endptr]
else:
decryptedtext = decryptedtext + ciphertext[startptr]
startptr = startptr + 1
endptr = endptr - 1
print("\n\nDecrypted Text after Rohit-Transposition : ", decryptedtext)
print('Line 181')
# splitting that combined cipher into rsacipher & playfair cipher
cfpf, rsacfpf = decryptedtext.split("||||") # Using it to separate rsacipher & playfair cipher
print("Playfair Cipher: ", cfpf) #str
print("\n\n\nRSA Cipher: ", rsacfpf)
print("Type of csafpf", type(rsacfpf)) #str
print('Line 188')
# Getting receiver's private key
pobj = RSA.importKey(receiver_privatekey.encode('utf-8').replace(b'\\n', b'\n'))
pobj = PKCS1_OAEP.new(pobj)
print('Line 193')
# Getting Playfair's Key
rsacipher_for_playfairKey = pobj.decrypt(codecs.escape_decode(rsacfpf[2:len(rsacfpf) - 1])[0])
print('PLayfair key:',rsacipher_for_playfairKey.decode('utf-8'))
playfairkey = rsacipher_for_playfairKey.decode('utf-8') #string
print('Line 202')
# Using this playfair key to decrypt the playfair cipher
objModifiedPlayfair = ModifiedPlayfair() #creating obj of modified playfair class
objModifiedPlayfair.getKey(playfairkey) # passing playfair key to that class
objModifiedPlayfair.setCipherText(cfpf) # passing ciphertext to that class (cfpf==cipher for playfair)
objModifiedPlayfair.generateKeyTable()
decrypted_playfair_plaintext = objModifiedPlayfair.decrypt()
print('Line 207')
print('Final Decrypted playfair plaintext :',decrypted_playfair_plaintext)
return render_template('Decrypted_msg.html', decrypted_playfair_plaintext = decrypted_playfair_plaintext)
#############################################################
if __name__ == '__main__':
app.run(debug = True)