Skip to content

Instantly share code, notes, and snippets.

@Patchyst
Created August 15, 2019 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Patchyst/88b544098522ff6c742d6811e3c9bcce to your computer and use it in GitHub Desktop.
Save Patchyst/88b544098522ff6c742d6811e3c9bcce to your computer and use it in GitHub Desktop.
The code for a secure Block Cipher Encryption in Python
def chunk_message(message, block_data=4):
message_chunked = []
block = 0
block_count = len(message) // block_data + 1
for character in range(block_count * block_data):
block = block << 8
if character < len(message):
block += ord(message[character])
else:
block += 0
if block.bit_length() > (block_data - 1) * 8:
message_chunked.append(block)
block = 0
return message_chunked
def apply_shift(message_chunked, key, block_data=4):
encrypted_message = []
max_bit = block_data * 8
for n in range(len(message_chunked)):
block = message_chunked[n]
carry = block % (2**key)
carry = carry << (max_bit - key)
encrypted_character = (block >> key) + carry
encrypted_message.append(encrypted_character)
return encrypted_message
def string_message(message_chunked, block_data = 4):
message = ""
for i in range(len(message_chunked)):
block = message_chunked[i]
for x in range(block_data):
byte = (block >> (8 * (block_data - 1 - x))) % 2**8
message += chr(byte)
return message
unencrypted_text = "Hello World!"
key = 7
block_list = chunk_message(unencrypted_text)
encrypted_list = apply_shift(block_list, key)
encrypted_string = string_message(encrypted_list)
print(encrypted_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment