You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
39 lines
1.1 KiB
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)
|
|
|