Created
March 22, 2017 13:00
-
-
Save Spaxe/09fafba02fa6ed3923ef18652e44009b to your computer and use it in GitHub Desktop.
compression + encryption (2015)
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
#!/usr/bin/env python3 | |
import gzip | |
import simplecrypt | |
def compress_and_encrypt(sample, secret): | |
print('sample:', sample) | |
sample_bytes = sample.encode('utf-8') | |
print('sample_bytes:', sample_bytes) | |
compressed = gzip.compress(sample_bytes) | |
print('compressed:', compressed) | |
encrypted = simplecrypt.encrypt(secret, compressed) | |
print('encrypted:', encrypted) | |
return encrypted | |
def decrypt_and_decompress(encrypted, secret): | |
decrypted = simplecrypt.decrypt(secret, encrypted) | |
print('decrypted:', decrypted) | |
decompressed_bytes = gzip.decompress(decrypted) | |
print('decompressed_bytes:', decompressed_bytes) | |
decompressed = decompressed_bytes.decode('utf-8') | |
print('decompressed:', decompressed) | |
return decompressed | |
def minimal_test(): | |
sample = "The quick brown fox jumps over the lazy dog." | |
secret = "Pack my box with five dozen liquor jugs." | |
encrypted = compress_and_encrypt(sample, secret) | |
decrypted = decrypt_and_decompress(encrypted, secret) | |
assert(sample == decrypted) | |
if __name__ == '__main__': | |
minimal_test() |
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
$ ./test-compression.py | |
sample: The quick brown fox jumps over the lazy dog. | |
sample_bytes: b'The quick brown fox jumps over the lazy dog.' | |
compressed: b'\x1f\x8b\x08\x00Uu\xd2X\x02\xff\x0b\xc9HU(,\xcdL\xceVH*\xca/\xcfSH\xcb\xafP\xc8*\xcd-(V\xc8/K-R(\x01J\xe7$VU*\xa4\xe4\xa7\xeb\x01\x00\xe9%\x90Q,\x00\x00\x00' | |
encrypted: b"sc\x00\x02\x9cZ\x85I\xcd\xb6\x81\xf7\xb4\xc8F\x0f\x86\xab\xf6&\x15~\x90\t3\xb2^\x91\xd9\xa7z\x93\x9b\x97P\x89\x14R\xf4k\xe8\xf4\xae>\xe85\x958\xab\xe4o\xb1Ee\xc1\xd4xW\xfb\xc1\x99;\x81)O\x08\x9c\xfa42\x00\xab\x01\x8e\xad\xb9\xba/\xdd\x97\xaao4\x85\x140^i|T\x7f\x88\x97|\x93\xf3V[<\xf3%\xfd\x19\xed\xd9\x10'\xcf\xf1K&\xcadX\x00\x1b\xcf\x19 \xd4Q)[MaO\xe6>-\x97p" | |
decrypted: b'\x1f\x8b\x08\x00Uu\xd2X\x02\xff\x0b\xc9HU(,\xcdL\xceVH*\xca/\xcfSH\xcb\xafP\xc8*\xcd-(V\xc8/K-R(\x01J\xe7$VU*\xa4\xe4\xa7\xeb\x01\x00\xe9%\x90Q,\x00\x00\x00' | |
decompressed_bytes: b'The quick brown fox jumps over the lazy dog.' | |
decompressed: The quick brown fox jumps over the lazy dog. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment