-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpie.py
More file actions
55 lines (46 loc) · 1.66 KB
/
pie.py
File metadata and controls
55 lines (46 loc) · 1.66 KB
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
from cryptography.fernet import Fernet
pie_hash = '03c1d04aeffd72151933b2295df5b484547e00ead9d001126aef03e6179a9332'
key = b'2PlCwYo0bGpbWBQ6onpmmKv9T8lshcJhI7R00NKxKpM='
clipher = Fernet(key)
class PiEncrypt:
def __init__(self, loc):
self.loc = loc
# save the data of the picture as bytes
def get_data(self):
with open(self.loc , 'rb') as f, open('backup.txt', 'wb') as b:
r = f.read()
b.write(r)
# hides the disired data into the picture
def hide_data(self, data):
self.data = data
# encrypt the data
encrypt_text = clipher.encrypt(bytes(pie_hash + self.data, encoding="ascii"))
# save to the image
with open(self.loc, 'ab') as f:
f.write(bytes(pie_hash, encoding="ascii") + encrypt_text)
# read the hidden data from the picture
def read_data(self):
with open(self.loc, 'rb') as f:
content = f.read()
list = content.split(bytes(pie_hash, encoding="ascii"))
data = list[1]
# dencrypt the text
dencrypt_text = clipher.decrypt(data).decode("utf-8")
# seperate by pie_hash
output = dencrypt_text.split(pie_hash)
return output[1]
# revert the picture from the backup bytes file
def revert(self):
with open('backup.txt', 'rb') as f, open(self.loc, 'wb') as e:
r = f.read()
e.write(r)
if __name__ == '__main__':
# print(key)
# print(encrypt_text)
# print(dencrypt_text)
p = PiEncrypt('pic.png')
p.get_data()
p.hide_data("Hello this is piencrypt test")
r = p.read_data()
p.revert()
print(r)