summaryrefslogtreecommitdiff
path: root/sbhs_server/helpers/simple_encrypt.py
blob: 09f8aba5fb28d3e8870c6693c49382e416dc2157 (plain)
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
import base64

def encrypt(cleartext):
    """ Function to encrypt the text which is send over the e-mail to verify the user.
    """
    string = cleartext

    string = string[::-1]

    for i in xrange(3):
        string = base64.b32encode(string)
        string = string[::-1]
        string = string.lower()

    padding = string.count("=")
    string = string.replace("=", "")
    return str(padding) + "." + string



def decrypt(ciphertext):
    """ Function to decrypt the ciphertext
    """
    data = ciphertext.split(".")
    padding = int(data[0])
    cipher = data[1]

    for i in xrange(padding):
        cipher = "=" + cipher

    string = cipher

    for i in xrange(3):
        string = string.upper()
        string = string[::-1]
        string = base64.b32decode(string)

    return string[::-1]