Skip to content

Instantly share code, notes, and snippets.

@thomashikaru
Created January 18, 2021 15:11
Show Gist options
  • Save thomashikaru/dfcc11b34a1d1dc6df94c30623460862 to your computer and use it in GitHub Desktop.
Save thomashikaru/dfcc11b34a1d1dc6df94c30623460862 to your computer and use it in GitHub Desktop.
import zlib
# set your text to whatever you like
text = "This is an example text to demonstrate compressibility of text before and after a Caesar Cipher."
# convert text to bytes
text_b = bytes(text, encoding="utf8")
print("Length of Original Text: ", len(text_b))
# perform compression on original text
text_c = zlib.compress(text_b)
print("Length of Compressed Original Text: ", len(text_c))
print("Compression Factor: ", 1.0-(len(text_c)/len(text_b)))
# get set of all characters used in the text
alphabet = "".join(set(text))
# perform Modified Caesar Cipher
ciphertext = ""
shift = 1
for i in text:
ind = (alphabet.index(i) + shift) % len(alphabet)
ciphertext += alphabet[ind]
# convert text to bytes
ciphertext_b = bytes(ciphertext, encoding="utf8")
print("Length of Ciphertext: ", len(ciphertext_b))
# perform compression on original text
ciphertext_c = zlib.compress(ciphertext_b)
print("Length of Compressed Ciphertext: ", len(ciphertext_c))
print("Compression Factor: ", 1.0-(len(ciphertext_c)/len(ciphertext_b)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment