Kkit.encryption

This module provides functions to encrypt and decrypt strings simply.

Example:

from Kkit import encryption
from cryptography.fernet import Fernet

# there are two ways to generate key:
key = Fernet.generate_key()           # generate key by Fernet
key = encryption.gen_key("your pin")  # generate key from a string

string_to_be_encrypted = "string_to_be_encrypted"
encrypted_string = encryption.encrypt_string(string_to_be_encrypted, key)
decrypted_string = decryption.encrypt_string(encrypted_string, key)
# decrypted_string will equal to string_to_be_encrypted
 1"""
 2This module provides functions to encrypt and decrypt strings simply.
 3
 4Example:
 5
 6```python
 7from Kkit import encryption
 8from cryptography.fernet import Fernet
 9
10# there are two ways to generate key:
11key = Fernet.generate_key()           # generate key by Fernet
12key = encryption.gen_key("your pin")  # generate key from a string
13
14string_to_be_encrypted = "string_to_be_encrypted"
15encrypted_string = encryption.encrypt_string(string_to_be_encrypted, key)
16decrypted_string = decryption.encrypt_string(encrypted_string, key)
17# decrypted_string will equal to string_to_be_encrypted
18```
19"""
20
21import hashlib
22import base64
23from cryptography.fernet import Fernet
24
25def gen_key(pin):
26    """
27    Generate a key that used in encryption and decryption from a string pin.
28
29    Parameters
30    ----------
31    pin : str
32        A string pin to generate a key.
33
34    Returns
35    -------
36    bytes
37        A key that can be used in encryption and decryption.
38    """
39    bytes_key = pin.encode('utf-8')
40    hash_object = hashlib.sha256(bytes_key)
41    hex_dig = hash_object.hexdigest()
42    key_bytes = bytes.fromhex(hex_dig)[:32]
43    key = base64.urlsafe_b64encode(key_bytes)
44    return key
45
46def encrypt_string(string, key, encoding="utf-8"):
47    """
48    Encrypt a string with a key.
49
50    Parameters
51    ----------
52    string : str
53        A string to be encrypted.
54
55    key : bytes
56        A key that used to encrypt the string.
57
58    encoding : str
59        The encoding of the string. Default is 'utf-8'.
60
61    Returns
62    -------
63    str
64        The encrypted string.
65    """
66    cipher_suite = Fernet(key)
67    return cipher_suite.encrypt(string.encode(encoding)).decode(encoding)
68
69def decrypt_string(string, key, encoding="utf-8"):
70    """
71    Decrypt a string with a key.
72
73    Parameters
74    ----------
75    string: str
76        A string to be decrypted.
77
78    key: bytes
79        A key that used to decrypt the string.
80        
81    encoding: str
82        The encoding of the string. Default is 'utf-8'.
83
84    Returns
85    -------
86    str
87        The decrypted string.
88    """
89    cipher_suite = Fernet(key)
90    return cipher_suite.decrypt(string.encode(encoding)).decode(encoding)
def gen_key(pin):
26def gen_key(pin):
27    """
28    Generate a key that used in encryption and decryption from a string pin.
29
30    Parameters
31    ----------
32    pin : str
33        A string pin to generate a key.
34
35    Returns
36    -------
37    bytes
38        A key that can be used in encryption and decryption.
39    """
40    bytes_key = pin.encode('utf-8')
41    hash_object = hashlib.sha256(bytes_key)
42    hex_dig = hash_object.hexdigest()
43    key_bytes = bytes.fromhex(hex_dig)[:32]
44    key = base64.urlsafe_b64encode(key_bytes)
45    return key

Generate a key that used in encryption and decryption from a string pin.

Parameters

pin : str A string pin to generate a key.

Returns

bytes A key that can be used in encryption and decryption.

def encrypt_string(string, key, encoding='utf-8'):
47def encrypt_string(string, key, encoding="utf-8"):
48    """
49    Encrypt a string with a key.
50
51    Parameters
52    ----------
53    string : str
54        A string to be encrypted.
55
56    key : bytes
57        A key that used to encrypt the string.
58
59    encoding : str
60        The encoding of the string. Default is 'utf-8'.
61
62    Returns
63    -------
64    str
65        The encrypted string.
66    """
67    cipher_suite = Fernet(key)
68    return cipher_suite.encrypt(string.encode(encoding)).decode(encoding)

Encrypt a string with a key.

Parameters

string : str A string to be encrypted.

key : bytes A key that used to encrypt the string.

encoding : str The encoding of the string. Default is 'utf-8'.

Returns

str The encrypted string.

def decrypt_string(string, key, encoding='utf-8'):
70def decrypt_string(string, key, encoding="utf-8"):
71    """
72    Decrypt a string with a key.
73
74    Parameters
75    ----------
76    string: str
77        A string to be decrypted.
78
79    key: bytes
80        A key that used to decrypt the string.
81        
82    encoding: str
83        The encoding of the string. Default is 'utf-8'.
84
85    Returns
86    -------
87    str
88        The decrypted string.
89    """
90    cipher_suite = Fernet(key)
91    return cipher_suite.decrypt(string.encode(encoding)).decode(encoding)

Decrypt a string with a key.

Parameters

string: str A string to be decrypted.

key: bytes A key that used to decrypt the string.

encoding: str The encoding of the string. Default is 'utf-8'.

Returns

str The decrypted string.