To See the Basic Uses of libtomcrypt from an example of AES-CTR

The description of functions for following codes

The functions of the block could be categorized by

  1. generate data
  2. encrypt data
  3. decrypt data

The codes in three different files

file_1 named Block.hpp

#ifndef BLOCK_HPP
#define BLOCK_HPP
#include "config.hpp"
// a block class
class Block{
    public:
        TYPE_INDEX index;
        TYPE_INDEX data_size;
        Block(TYPE_INDEX index, TYPE_INDEX block_size);
        ~Block();
        
        char* decrypt();
        char* encrypt();
        char* get_data();

    private:
        char *data;
        unsigned char *iv;
};


#endif

file_2 named Block.cpp

#include "Block.hpp"
#include "config.hpp"

#include <tomcrypt.h>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <iostream>
// constructor
Block::Block(TYPE_INDEX index, TYPE_INDEX data_size){
    this->index = index;
    this->data_size = data_size;
    this->data = new char[data_size];
    // initialize the data with some data some characters
    for(int i = 0; i < data_size; i++){
        data[i] = 'a' + i % 26;
    }
    this->iv = new unsigned char[IV_SIZE];

    // initialize the aes_desc 
    if (-1 == register_cipher(&aes_desc)) {
		std::cout << "[Block] Register cipher err" << std::endl;
		exit(-1);
	}
    // generate random IV
    srand((unsigned)time(NULL));
}
// destructor
Block::~Block(){
    delete[] data;
    delete[] iv;
}
// realize get_data()
char* Block::get_data(){
    return data;
}
char *Block::encrypt()
{
	/* Generate random IV */
	for (int i = 0; i < IV_SIZE; i += sizeof(int)) {
		int random_int = rand();
		memcpy(iv + i, &random_int, sizeof(int));
	}
	if (IV_SIZE / sizeof(int) != 0) {
		int random_int = rand();
		memcpy(iv + IV_SIZE - sizeof(int), &random_int, sizeof(int));
	}

	int err = 0;
	symmetric_CTR ctr;
	char cipher_data[this->data_size];
    
	/* encrypt */
	if ((err =  ctr_start(find_cipher("aes"), iv, ENCRYPT_KEY, sizeof(ENCRYPT_KEY), 0, CTR_COUNTER_LITTLE_ENDIAN, &ctr)) != CRYPT_OK) 
    {
        std::cout << "[Block] ctr_start err: " << error_to_string(err) << std::endl;
		exit(-1);
	}
	if ((err = ctr_encrypt((unsigned char *)data, (unsigned char *)cipher_data, this->data_size, &ctr)) != CRYPT_OK) {
        std::cout << "[Block] ctr_encrypt err: " << error_to_string(err) << std::endl;
        exit(-1);
	}
	if ((err = ctr_done(&ctr)) != CRYPT_OK) {
        std::cout << "[Block] ctr_done err: " << error_to_string(err) << std::endl;
		exit(-1);
	}

	memcpy(data, cipher_data, this->data_size);
    return data;
}

char *Block::decrypt()
{
	int err;
	symmetric_CTR ctr;
	char decipher_data[this->data_size];

	if ((err =  ctr_start(find_cipher("aes"), iv, ENCRYPT_KEY, sizeof(ENCRYPT_KEY), 0, CTR_COUNTER_LITTLE_ENDIAN, &ctr)) != CRYPT_OK) {
		std::cout << "[Block] ctr_start err: " << error_to_string(err) << std::endl;
		exit(-1);
	}
	if ((err = ctr_decrypt((unsigned char *)data, (unsigned char *)decipher_data, this->data_size, &ctr)) != CRYPT_OK) {
        std::cout << "[Block] ctr_encrypt err: " << error_to_string(err) << std::endl;
		exit(-1);
	}
	if ((err = ctr_done(&ctr)) != CRYPT_OK) {
        std::cout << "[Block] ctr_done err: " << error_to_string(err) << std::endl;
		exit(-1);
	}

	memcpy(data, decipher_data, this->data_size);
    return data;
}

file_3 main.cpp

#include <iostream>
#include "Block.hpp"
#include <cstring>
int main()
{
    Block block(1, 2);
    std::cout <<"The size of block " << sizeof(block) << std::endl;
    std::cout << block.index << std::endl;
    std::cout << block.data_size << std::endl;
    char *data = new char[2];
    data = block.get_data();
    char *encrypt_data = new char[2];
    encrypt_data = block.encrypt();
    char *decrypt_data = new char[2];
    decrypt_data = block.decrypt();
    // check out whether the data and decrypt_data are the same or not
    if (memcmp(data, decrypt_data, 2) == 0) {
        std::cout << "data and decrypt_data are the same" << std::endl;
    } else {
        std::cout << "data and decrypt_data are not the same" << std::endl;
    }

    return 0;
}

How to compile the above files simultaneously

g++ main.cpp Block.cpp -o direct_access -ltomcrypt

The corresponding results

The size of block 32
1
2
data and decrypt_data are the same
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值