Last active
October 19, 2021 10:55
-
-
Save Girgitt/7cbfe8e6ffdcf7eba333c348cdcd1642 to your computer and use it in GitHub Desktop.
simple AES CTR mode encryption for text data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import codecs | |
from cryptography.hazmat.backends import default_backend | |
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes | |
class Encryption(object): | |
def __init__(self, key='aKeyNobodyWIllEverUse', nonce='PleaseMakeMeRandomEachTime'): | |
key = str(key) | |
while len(key) < 32: | |
key += key | |
key = bytearray(key[:32], 'utf8') | |
nonce = str(nonce) | |
while len(nonce) < 16: | |
nonce += nonce | |
nonce = bytearray(nonce[:16], 'utf8') | |
backend = default_backend() | |
self._cipher = Cipher(algorithms.AES(key), modes.CTR(nonce), backend=backend) | |
self._encryptor = None | |
self._encrypted_payload = None | |
self.init_encryption() | |
self._decryptor = None | |
self._decrypted_payload = None | |
self.init_decryption() | |
def init_encryption(self): | |
self._encryptor = self._cipher.encryptor() | |
self._encrypted_payload = None | |
def update_payload_to_encrypt(self, payload): | |
if self._encryptor: | |
self._encrypted_payload = self._encryptor.update(payload) | |
@property | |
def encrypted_payload(self): | |
if self._encrypted_payload: | |
return self._encrypted_payload | |
return '' | |
def init_decryption(self): | |
self._decryptor = self._cipher.decryptor() | |
self._decrypted_payload = None | |
def update_payload_to_decrypt(self, payload): | |
if self._decryptor: | |
self._decrypted_payload = self._decryptor.update(payload) | |
@property | |
def decrypted_payload(self): | |
if self._decrypted_payload: | |
return self._decrypted_payload | |
return '' | |
encryptor = Encryption('some key', 'some nonce') | |
decryptor = Encryption('some key', 'some nonce') | |
def get_encrypted_line(input_line): | |
plaintext = input_line | |
words_lengths = [len(item) for item in plaintext.split(" ")] | |
plaintext_joined = plaintext.replace(" ", "") | |
encryptor.init_encryption() | |
encryptor.update_payload_to_encrypt(bytearray(plaintext_joined, 'utf8')) | |
cipher_as_text = ''.join([hex(item).lstrip('0x').zfill(2) for item in encryptor.encrypted_payload]) | |
split_encrypted_in = [] | |
for word_len in words_lengths: | |
split_encrypted_in.append(cipher_as_text[:2 * word_len]) | |
cipher_as_text = cipher_as_text[2 * word_len:] | |
split_encrypted = " ".join(split_encrypted_in) | |
return split_encrypted | |
def get_decrypted_line(input_line): | |
decryptor.init_decryption() | |
plaintext_words_lengths = [int(len(item) / 2) for item in input_line.split(" ")] | |
#print("plaintext_words_lengths: %s" % plaintext_words_lengths) | |
joined_encrypted = input_line.replace(" ", "") | |
binary_encrypted = bytearray.fromhex(joined_encrypted) | |
#print("binary_encrypted: %s", binary_encrypted) | |
decryptor.update_payload_to_decrypt(binary_encrypted) | |
plaintext_joined = decryptor.decrypted_payload.decode('utf8') | |
#print("plaintext_joined: %s" % plaintext_joined) | |
plaintext_words = [] | |
for word_len in plaintext_words_lengths: | |
plaintext_words.append(plaintext_joined[:word_len]) | |
plaintext_joined = plaintext_joined[word_len:] | |
decrypted_plaintext = ' '.join(plaintext_words) | |
return decrypted_plaintext | |
input_text = 'something to mask in a reversible way' | |
encrypted = get_encrypted_line(input_text) | |
print("input: %s" % input_text) | |
print("encrypted: %s" % encrypted) | |
decrypted = get_decrypted_line(encrypted) | |
print("decrypted: %s" % decrypted) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for a painless installation of cryptgraphy module: