-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.py
54 lines (47 loc) · 1.59 KB
/
crypto.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
'''
Copyright 2015 Singapore Management University
This Source Code Form is subject to the terms of the
Mozilla Public License, v. 2.0. If a copy of the MPL was
not distributed with this file, You can obtain one at
http://mozilla.org/MPL/2.0/.
'''
"""Data encryption
@created: Nguyen Thi Ngoc
"""
"""
Functions for encryption and decryption of data
"""
import base64
from Crypto.Cipher import AES
from appengine_config import _ConfigDefaults
class Crypto:
#Encrypt plain text to cipher text using AES
@staticmethod
def encrypt(plainText):
BLOCK_SIZE = 32
PADDING = '{'
try:
if(plainText != ""):
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
encodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
cipherText = encodeAES(AES.new(_ConfigDefaults.ksketch_CRYPTO_KEY), plainText.encode('utf8'))
return cipherText
else:
return plainText
except:
print plainText
#Decrypt cipher text to plain text
@staticmethod
def decrypt(cipherText):
PADDING = '{'
try:
if(cipherText != ""):
if " " in cipherText:
cipherText = cipherText.replace(" ", "+")
decodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
plainText = decodeAES(AES.new(_ConfigDefaults.ksketch_CRYPTO_KEY), cipherText)
return plainText.decode('utf8')
else:
return cipherText
except:
print cipherText