Last active
November 19, 2018 13:11
-
-
Save klashxx/d40f39d39e00222ff9846eec99ca71fe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| from Crypto.Cipher import AES | |
| from base64 import b64encode, b64decode | |
| hdr = b'To your eyes only' | |
| plaintext = b'Attack at dawn' | |
| key = b'Sixteen byte key' | |
| cipher = AES.new(key, AES.MODE_CCM) | |
| cipher.update(hdr) | |
| msg = cipher.nonce, hdr, cipher.encrypt(plaintext), cipher.digest() | |
| from Crypto.Cipher import AES | |
| from base64 import b64encode, b64decode | |
| msg = (b'\x1f\xaa@\xfd\xe4\xb3\x82\x15\x18\x95k', b'To your eyes only', b'\xe9\xd5\x11o8\xafJ\xe6w\xab\x8e~\x9f\\', b'\xa9\x04D\x92\x9f\xf1sg\xa6\xfc}\x83\xdf9a\xf2') | |
| nonce, hdr, ciphertext, mac = msg | |
| cipher64 = b64encode(ciphertext).decode('ascii') | |
| print('cipher64, ciphertext -->', cipher64, ciphertext) | |
| key = b'Sixteen byte key' | |
| cipher = AES.new(key, AES.MODE_CCM, nonce) | |
| cipher.update(hdr) | |
| plaintext = cipher.decrypt(b64decode(cipher64)) | |
| print('plain decoded --->', plaintext) | |
| from cryptography.fernet import Fernet | |
| from base64 import b64encode, b64decode | |
| #cipher_key = Fernet.generate_key() | |
| cipher_key = b'fFN0T6t8bHDfPePpdj1IGLS-Jmji4uNoiv_jaO2v1N8=' | |
| cipher_key64 = b64encode(cipher_key).decode('ascii') | |
| cipher_key64 | |
| cipher = Fernet(b64decode(cipher_key64)) | |
| text = b'My super secret message' | |
| encrypted_text = cipher.encrypt(text) | |
| encrypted_text | |
| decrypted_text = cipher.decrypt(encrypted_text) | |
| decrypted_text | |
| KEY = 'ZkZOMFQ2dDhiSERmUGVQcGRqMUlHTFMtSm1qaTR1Tm9pdl9qYU8ydjFOOD0=' | |
| class RepositoryEnv(RepositoryEmpty): | |
| """ | |
| Retrieves option keys from .env files with fall back to os.environ. | |
| """ | |
| def __init__(self, source): | |
| self.data = {} | |
| with open(source) as file_: | |
| for line in file_: | |
| line = line.strip() | |
| if not line or line.startswith('#') or '=' not in line: | |
| continue | |
| decode = re.search(r'^([^=]+)=(.*)_\[>(.+)<\]_(.*)$', line) | |
| if decode: | |
| k, p, d, n = decode.groups() | |
| d = Fernet(b64decode(KEY)).decrypt(b64decode(d)).decode('utf8') | |
| v = p + d + n | |
| else: | |
| k, v = line.split('=', 1) | |
| k = k.strip() | |
| v = v.strip().strip('\'"') | |
| self.data[k] = v | |
| def __contains__(self, key): | |
| return key in os.environ or key in self.data | |
| def __getitem__(self, key): | |
| return self.data[key] | |
Author
klashxx
commented
Nov 19, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment