tiencr.c
/*
 * \brief Write out the specified buffer
 *
 * \param file_path Output file
 * \param buffer Pointer to byte buffer to write out
 * \param buf_size Size of buffer
 */
\brief Write out the specified buffer
\param file_path Output file
\param buffer Pointer to byte buffer to write out
\param buf_size Size of buffer
\ brief Write out the specified buffer \ param file_path Output file \ param buffer Pointer to byte buffer to write out \ param buf_size Size of buffer
int write_buffer_as_encr(const char* file_path, uint8_t* buffer, size_t buf_size)
{
	int ret = 0;
	FILE *fp = NULL;
	uint8_t key_len_xor_val = rand();
	size_t encode_index = 0;
	uint8_t key[ENCRYPT_KEY_SIZE] = {0};
	uint8_t key_copy[ENCRYPT_KEY_SIZE] = {0};
	uint8_t key_len = ENCRYPT_KEY_SIZE;
	uint8_t dummy_buffer[10] = {0};
	size_t i = 0;
	populate_key(key,sizeof(key));
	memcpy(key_copy,key,sizeof(key));
	fp = fopen(file_path,"wb");
	if( fp == NULL )
	{
		printf("Error opening .encr file\n");
		ret = ERR_FILE_IO;
		goto err;
	}
	dummy_buffer[0] = 0x1;	
	dummy_buffer[1] = key_len_xor_val;
	dummy_buffer[7] = key_len ^ key_len_xor_val;
	fwrite(header_str_bytes,sizeof(header_str_bytes),1,fp);
	fwrite(dummy_buffer,8,1,fp);
	xor_key(key, key_len, true);
	fwrite(key,key_len,1,fp);
	for(i=0;i<buf_size;i++)
	{
		buffer[i] = encode_char(key_copy, key_len, buffer[i], &encode_index);
		fwrite(&buffer[i],1,1,fp);
	}
	fclose(fp);
err:
	fclose(fp);
	return ret;
}
int write_buffer_as_encr ( const char * file_path , uint8_t * buffer , size_t buf_size ) { int ret = 0 ; FILE * fp = NULL ; uint8_t key_len_xor_val = rand ( ) ; size_t encode_index = 0 ; uint8_t key [ ENCRYPT_KEY_SIZE ] = { 0 } ; uint8_t key_copy [ ENCRYPT_KEY_SIZE ] = { 0 } ; uint8_t key_len = ENCRYPT_KEY_SIZE ; uint8_t dummy_buffer [ 10 ] = { 0 } ; size_t i = 0 ; populate_key ( key , sizeof ( key ) ) ; memcpy ( key_copy , key , sizeof ( key ) ) ; fp = fopen ( file_path , " " ) ; if ( fp == NULL ) { printf ( " \n " ) ; ret = ERR_FILE_IO ; goto err ; } dummy_buffer [ 0 ] = 0x1 ; dummy_buffer [ 1 ] = key_len_xor_val ; dummy_buffer [ 7 ] = key_len ^ key_len_xor_val ; fwrite ( header_str_bytes , sizeof ( header_str_bytes ) , 1 , fp ) ; fwrite ( dummy_buffer , 8 , 1 , fp ) ; xor_key ( key , key_len , true ) ; fwrite ( key , key_len , 1 , fp ) ; for ( i = 0 ; i < buf_size ; i ++ ) { buffer [ i ] = encode_char ( key_copy , key_len , buffer [ i ] , & encode_index ) ; fwrite ( & buffer [ i ] , 1 , 1 , fp ) ; } fclose ( fp ) ; err : fclose ( fp ) ; return ret ; }
\brief Write out the specified buffer
 \param file_path Output file
 \param buffer Pointer to byte buffer to write out
 \param buf_size Size of buffer
\ brief Write out the specified buffer \ param file_path Output file \ param buffer Pointer to byte buffer to write out \ param buf_size Size of buffer
//fixed