
#ifndef SYMMETRIA_2_ENCRYPTION_H
#define SYMMETRIA_2_ENCRYPTION_H
//
// Created by 甜甜 on 2020/12/2.
//

#include <string.h>
#include <memory>

#include "t.h"

using namespace std;

#define AES_BLOCK_SIZE 16
#define BUF_SIZE 1024

#define BUFFER_SIZE 1024

static const unsigned char en_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static unsigned char de_table[128] = {0};

class encryption {
public:
    static void init_de_table();

    int get_encode_len(int len);

    int get_decode_len(int len);

    int base64_encode(const unsigned char *data, int data_len, char *dst, int dst_len);

    int base64_decode(const unsigned char *data, int data_len, char *dst, int dst_len);

    // 根据数据字节数 获取加密字节数
    int get_crypt_size(int data_len);

    // 加密
    //int aes_encrypt(char* in, char* key, char* out);
    int aes_encrypt(char *in, char *key, char *out, std::size_t inlen);
    int aeshmac_encrypt(char *in, int inlen, char *key, char *out);

    // 解密
    int aes_decrypt(char *in, int in_len, char *key, char *out);
    int aeshmac_decrypt(char *in, int inlen, char *key, char *out);

    long long charToLongLong(char b[]);

    //    char* DET_encrypt(char* m);
    char *DET_encrypt(char *m, std::size_t *cipher_length);

    char *DET_decrypt(char *ciphertext, std::size_t *out_len);
};

void print_hex(const char *title, const unsigned char *s, int len);

int apply_pkcs7_padding(const unsigned char *input, int input_len, unsigned char *padded_input, int block_size);

int remove_pkcs7_padding(unsigned char *data, int input_len);

inline void iv_create(unsigned char *iv);

void iv_extract(unsigned char *iv, const unsigned char *encrypted, int encrypted_len);

void iv_append(unsigned char *iv, unsigned char *encrypted, int encrypted_len, unsigned char *output);

int aes_encrypt(const unsigned char *plaintext, int plaintext_len, const unsigned char *key, unsigned char *encrypted);

int aes_decrypt(const unsigned char *ciphertext, int ciphertext_len, const unsigned char *key, unsigned char *decrypted,
                int padded_plaintext_len);

#endif //SYMMETRIA_2_ENCRYPTION_H
