Add the modules for encryption, decryption.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
3bdb333dc0
commit
74fc05e62b
@ -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)
|
||||
|
@ -0,0 +1,29 @@
|
||||
import pytest
|
||||
from cryptography_s_des_exploring.sdes.decrypt import decrypt, decrypt_block
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key, message, expected_ciphertext", [
|
||||
('1111001110', '00100001', '11110011'),
|
||||
('1111001111', '11110111', '00010011'),
|
||||
# 0b1101000 = 104 ('h')
|
||||
('1111001110', '10100011', '01101000'),
|
||||
# 0b1100101 = 101 ('e')
|
||||
('1111001110', '10000111', '01100101'),
|
||||
# 0b1101100 = 108 ('l')
|
||||
('1111001110', '11100110', '01101100'),
|
||||
# 0b1101111 = 111 ('o')
|
||||
('1111001110', '11001110', '01101111'),
|
||||
])
|
||||
def test_encrypt_block(key, message, expected_ciphertext):
|
||||
result = decrypt_block(key, message)
|
||||
assert expected_ciphertext == result
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key, message, expected_ciphertext", [
|
||||
('1111001110', 'wkwaR1xMpoE=', 'hello'),
|
||||
('1111001110', 'nEp+f+C3CRmcSkp/5s1MCQ==', 'привіт'),
|
||||
])
|
||||
def test_encrypt(key, message, expected_ciphertext):
|
||||
result = decrypt(key, message)
|
||||
assert expected_ciphertext == result
|
||||
|
@ -0,0 +1,29 @@
|
||||
import pytest
|
||||
from cryptography_s_des_exploring.sdes.encrypt import encrypt, encrypt_block
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key, message, expected_ciphertext", [
|
||||
('1111001110', '11110011', '00100001'),
|
||||
('1111001111', '00010011', '11110111'),
|
||||
# 0b1101000 = 104 ('h')
|
||||
('1111001110', '01101000', '10100011'),
|
||||
# 0b1100101 = 101 ('e')
|
||||
('1111001110', '01100101', '10000111'),
|
||||
# 0b1101100 = 108 ('l')
|
||||
('1111001110', '01101100', '11100110'),
|
||||
# 0b1101111 = 111 ('o')
|
||||
('1111001110', '01101111', '11001110'),
|
||||
])
|
||||
def test_encrypt_block(key, message, expected_ciphertext):
|
||||
result = encrypt_block(key, message)
|
||||
assert expected_ciphertext == result
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key, message, expected_ciphertext", [
|
||||
('1111001110', 'hello', b'wkwaR1xMpoE='),
|
||||
('1111001110', 'привіт', b'nEp+f+C3CRmcSkp/5s1MCQ=='),
|
||||
])
|
||||
def test_encrypt(key, message, expected_ciphertext):
|
||||
result = encrypt(key, message)
|
||||
assert expected_ciphertext == result
|
||||
|
Loading…
Reference in new issue