Add the modules for encryption, decryption.
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-11-26 20:21:56 +02:00
parent 3bdb333dc0
commit 74fc05e62b
4 changed files with 134 additions and 0 deletions
@@ -0,0 +1,38 @@
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 decode, decode_bytes
def decrypt_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, key2, temp)
temp = temp[4:] + temp[:4]
temp = function(expansion, s0, s1, key1, temp)
plaintext = apply_table(temp, IP_inv)
return plaintext
def decrypt(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.
"""
decoded = decode_bytes(message)
decrypted = list(map(lambda b: decrypt_block(key, b), decoded))
return decode(decrypted)
@@ -0,0 +1,38 @@
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)