Files
cryptography-s-des-exploring/cryptography_s_des_exploring/sdes/encrypt.py
T
KKlochko 74fc05e62b
continuous-integration/drone/push Build is passing
Add the modules for encryption, decryption.
2023-11-26 20:21:56 +02:00

39 lines
1.1 KiB
Python

from cryptography_s_des_exploring.sdes.keys import generate_subkeys
from cryptography_s_des_exploring.sdes.sdes import apply_table, function, IP, s0, expansion, s1, IP_inv
from cryptography_s_des_exploring.sdes.blocks import encode, encode_bytes
def encrypt_block(key: str, block: str):
"""
Encrypt the 8-bit block with 10-bit key.
key is a binary which represented with a 10 character string.
block is a binary which represented with 8 character string.
"""
key1, key2 = generate_subkeys(key)
temp = apply_table(block, IP)
temp = function(expansion, s0, s1, key1, temp)
temp = temp[4:] + temp[:4]
temp = function(expansion, s0, s1, key2, temp)
ciphertext = apply_table(temp, IP_inv)
return ciphertext
def encrypt(key: str, message: str):
"""
Encrypt the message with the 10-bit key.
The message is split into 8-bit blocks.
key is a binary which represented with a 10 character string.
"""
encoded = encode(message)
encrypted = list(map(lambda b: encrypt_block(key, b), encoded))
return encode_bytes(encrypted)