1+
2+ from cryptography .fernet import Fernet
13pie_hash = '03c1d04aeffd72151933b2295df5b484547e00ead9d001126aef03e6179a9332'
24
5+ key = b'2PlCwYo0bGpbWBQ6onpmmKv9T8lshcJhI7R00NKxKpM='
6+ clipher = Fernet (key )
7+
38class PiEncrypt :
49
510 def __init__ (self , loc ):
@@ -14,16 +19,23 @@ def get_data(self):
1419 # hides the disired data into the picture
1520 def hide_data (self , data ):
1621 self .data = data
22+ # encrypt the data
23+ encrypt_text = clipher .encrypt (bytes (pie_hash + self .data , encoding = "ascii" ))
24+ # save to the image
1725 with open (self .loc , 'ab' ) as f :
18- f .write (bytes (pie_hash + self . data , encoding = "ascii" ))
26+ f .write (bytes (pie_hash , encoding = "ascii" ) + encrypt_text )
1927
2028 # read the hidden data from the picture
2129 def read_data (self ):
2230 with open (self .loc , 'rb' ) as f :
2331 content = f .read ()
2432 list = content .split (bytes (pie_hash , encoding = "ascii" ))
25- # print(f.read())
26- return list [1 ].decode ("utf-8" )
33+ data = list [1 ]
34+ # dencrypt the text
35+ dencrypt_text = clipher .decrypt (data ).decode ("utf-8" )
36+ # seperate by pie_hash
37+ output = dencrypt_text .split (pie_hash )
38+ return output [1 ]
2739
2840 # revert the picture from the backup bytes file
2941 def revert (self ):
@@ -32,10 +44,12 @@ def revert(self):
3244 e .write (r )
3345
3446if __name__ == '__main__' :
35-
36- p = PiEncrypt ('test.png' )
37- # p.get_data()
38- p .revert ()
39- p .hide_data ("Hello my name is Sid" )
47+ # print(key)
48+ # print(encrypt_text)
49+ # print(dencrypt_text)
50+ p = PiEncrypt ('pic.png' )
51+ p .get_data ()
52+ p .hide_data ("Hello this is piencrypt test" )
4053 r = p .read_data ()
54+ p .revert ()
4155 print (r )
0 commit comments