-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryption.py
More file actions
63 lines (50 loc) · 1.9 KB
/
Encryption.py
File metadata and controls
63 lines (50 loc) · 1.9 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
56
57
58
59
60
61
62
63
from cryptography.fernet import Fernet as en # Package to encrypt data
import base64 # Package to generate key from
class Encrypt:
def __init__(self, message, password):
self.message = message
self.password = password
# GETTING SECTION
def getPassword(self):
return self.password
def getMessage(self):
return self.message
# SETTING SECTION
def setPassword(self, password):
self.password = password
def setMessage(self, message):
self.message = message
# KEY GENERATION SECTION
def keyGenerator (self, n):
# First we have to convert it into str of 32 char
# for getting key
length = len(n)
if (length < 32) :
half = (32-length)//2 + 1
temp = " "
if(32-length > 1):
for i in range(half):
temp = temp + str(i%10)
n = temp[::-1] + n + temp
else:
n = n + temp
n = n[0:32]
n = base64.urlsafe_b64encode(n.encode())
key = n # Storing key
encryptSuit = en(key) # Creating encrypting Suit
return encryptSuit
# ENRYPTION SECTION
def encryptMessage(self):
self.setMessage(self.getMessage().encode()) # Encoding message to bytes
encryptionSuit = self.keyGenerator(self.getPassword())
encryptedText = encryptionSuit.encrypt(self.getMessage()) # Encrypting Text
return str(encryptedText)[2:-1] # Converting to string
# DECRYPTION SECTION
def decryptMessage(self):
self.setMessage(self.getMessage().encode()) # Encoding message to bytes
try:
encryptionSuit = self.keyGenerator(self.getPassword())
decryptedText = encryptionSuit.decrypt(self.getMessage()) # Decrpyting Text
return str(decryptedText)[2:-1] # Converting to string
except:
return "!-)=~"