forked from ShareByLink/iqbox-ftp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypt.py
50 lines (35 loc) · 1.1 KB
/
crypt.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
import os
import sys
import base64
from itertools import izip, cycle
sys.path.append(os.path.abspath(__file__))
# Key should be in `key` file
with open('key', 'r') as keyfile:
KEY = keyfile.read()
def encrypt(data):
"""
Encrypts the `data` string using and XOR operation and
returns the result encoded using base64.
:param data: String to be encrypted
"""
encrypted = _do_xor(data)
return base64.b64encode(encrypted)
def decrypt(data):
"""
Decrypts the `data` string using and XOR after decoding
it using base64. The `data` string should have been encrypted
with the `encrypt` function in this module.
:param data: String to be decrypted
"""
decrypted = base64.b64decode(data)
return _do_xor(decrypted)
def _do_xor(data):
"""
Performs a XOR operation between `data` and `KEY`
strings.
Returns the encoded result.
:param data: String to be encoded
"""
# Recipe for xoring two strings.
xored = ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(KEY)))
return xored