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 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)
|
|
|