The description of functions for following codes
The functions of the block could be categorized by
- generate data
- encrypt data
- decrypt data
The codes in three different files
file_1 named Block.hpp
#ifndef BLOCK_HPP
#define BLOCK_HPP
#include "config.hpp"
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>
Block::Block(TYPE_INDEX index, TYPE_INDEX data_size){
this->index = index;
this->data_size = data_size;
this->data = new char[data_size];
for(int i = 0; i < data_size; i++){
data[i] = 'a' + i % 26;
}
this->iv = new unsigned char[IV_SIZE];
if (-1 == register_cipher(&aes_desc)) {
std::cout << "[Block] Register cipher err" << std::endl;
exit(-1);
}
srand((unsigned)time(NULL));
}
Block::~Block(){
delete[] data;
delete[] iv;
}
char* Block::get_data(){
return data;
}
char *Block::encrypt()
{
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];
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();
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
|