1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use aes::cipher::{
    crypto_common::generic_array::GenericArray, typenum::U32, BlockDecrypt, BlockEncrypt,
};
use aes::{cipher::KeyInit, Aes256};

impl TryFrom<&str> for Aes256Cryptor {
    type Error = std::io::Error;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        if value.len() != 32 {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "The number of bytes of the key shall be 32",
            ));
        }
        let mut key = [0u8; 32];
        key.copy_from_slice(value.as_bytes());
        Ok(Aes256Cryptor::new(key))
    }
}

impl TryFrom<String> for Aes256Cryptor {
    type Error = std::io::Error;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Aes256Cryptor::try_from(&value as &str)
    }
}

impl TryFrom<&String> for Aes256Cryptor {
    type Error = std::io::Error;

    fn try_from(value: &String) -> Result<Self, Self::Error> {
        Aes256Cryptor::try_from(&value as &str)
    }
}

#[derive(Clone, Debug)]
pub struct Aes256Cryptor {
    key: [u8; 32],
    aes_256: Aes256,
}

impl Aes256Cryptor {
    pub fn new(key: [u8; 32]) -> Self {
        Self {
            // Here U32 is not same as u32. U32 creates our GenericArray Key which is 32 byte long. If the key length is not 32 bytes in length, then this from_slice() call fails with assertion error
            key,
            aes_256: Aes256::new(GenericArray::<u8, U32>::from_slice(&key)),
        }
    }

    pub fn key(&self) -> &[u8] {
        &self.key
    }

    pub fn encrypt<T, U>(&self, plaintext: T) -> Vec<u8>
    where
        BytesWrapper<BytesWrapper<T>>: IntoBytes<U>,
    {
        let mut blocks =
            super::get_generic_array(BytesWrapper(BytesWrapper(plaintext)).into_bytes(), false);
        // Encrypt the single unit at once (This Unit will contain all the blocks)
        self.aes_256.encrypt_blocks(blocks.as_mut_slice());
        blocks.concat().into_iter().collect()
    }

    pub fn decrypt<T, U>(&self, ciphertext: T) -> std::io::Result<Vec<u8>>
    where
        BytesWrapper<BytesWrapper<T>>: IntoBytes<U>,
    {
        let raw_bytes = BytesWrapper(BytesWrapper(ciphertext)).into_bytes();
        if raw_bytes.len() % 16 != 0 {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "The number of bytes of the encrypted data shall be multiple of 16",
            ));
        }
        let mut blocks = super::get_generic_array(raw_bytes, true);
        // Decrypt the single unit at once (This Unit will contain all the blocks)
        self.aes_256.decrypt_blocks(blocks.as_mut_slice());
        // Concat the decrypted block which the deref_generic_block hold and turn then in a Vec<u8>
        let decrypted_bytes = blocks.concat().into_iter().collect::<Vec<u8>>();

        if let Some(v) = decrypted_bytes.last() {
            if *v == 16 && decrypted_bytes[decrypted_bytes.len() - 16..] == [16u8; 16] {
                return Ok(decrypted_bytes[..decrypted_bytes.len() - 16].to_vec());
            } else if *v < 16 {
                return Ok(decrypted_bytes[..decrypted_bytes.len() - (*v as usize)].to_vec());
            }
            Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!(
                    "Invalid encrypted data, the padding number cannot be {}",
                    *v
                ),
            ))
        } else {
            Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "The number of bytes of the encrypted data shall be at least 16 even if the original data is empty"))
        }
    }
}

pub struct BytesWrapper<T>(T);

pub trait IntoBytes<T> {
    fn into_bytes(self) -> Vec<u8>;
}

impl<T: Into<Vec<u8>>> IntoBytes<T> for T {
    fn into_bytes(self) -> Vec<u8> {
        self.into()
    }
}

impl<T: IntoBytes<U>, U> IntoBytes<U> for BytesWrapper<T> {
    fn into_bytes(self) -> Vec<u8> {
        self.0.into_bytes()
    }
}

impl IntoBytes<()> for BytesWrapper<BytesWrapper<&String>> {
    fn into_bytes(self) -> Vec<u8> {
        self.0 .0.to_owned().into_bytes()
    }
}