des decryption and encryption in bas64

This commit is contained in:
Gregory 2019-03-23 11:33:28 +02:00
parent 67af77126e
commit b6460d9b6b
10 changed files with 133 additions and 62 deletions

View file

@ -132,6 +132,8 @@ DES_SRC = ft_des_initial_permutation.c \
ft_des_arg_parsers.c \ ft_des_arg_parsers.c \
ft_des_ecb_encrypt.c \ ft_des_ecb_encrypt.c \
ft_des_ecb_decrypt.c \ ft_des_ecb_decrypt.c \
ft_des_ecb_decrypt_b64.c \
ft_des_ecb_encrypt_b64.c \
ft_des_ecb_encode_process_chunk.c \ ft_des_ecb_encode_process_chunk.c \
ft_des_hex_to_bit_key.c \ ft_des_hex_to_bit_key.c \
ft_des_ecb_finish_encrypt.c ft_des_ecb_finish_encrypt.c

View file

@ -14,7 +14,6 @@
# define FT_DES_H # define FT_DES_H
# include <stdint.h> # include <stdint.h>
# include "ft_base64.h"
# define FT_DES_BYTE_BLOCK_SIZE 8 # define FT_DES_BYTE_BLOCK_SIZE 8
# define FT_DES_BIT_BLOCK_SIZE 64 # define FT_DES_BIT_BLOCK_SIZE 64
@ -46,9 +45,6 @@ typedef struct s_des_ctx
t_byte1 key[FT_DES_INITIAL_KEY_SIZE]; t_byte1 key[FT_DES_INITIAL_KEY_SIZE];
t_byte1 round_keys[FT_DES_ROUND_COUNT] t_byte1 round_keys[FT_DES_ROUND_COUNT]
[FT_DES_ROUND_KEY_SIZE]; [FT_DES_ROUND_KEY_SIZE];
t_base64_ctx b64_ctx;
t_base64_encode_buffer b64_encode_buffer;
t_base64_decode_buffer b64_decode_buffer;
} t_des_ctx; } t_des_ctx;
typedef int (*t_ft_des_arg_parser_function) typedef int (*t_ft_des_arg_parser_function)
@ -309,11 +305,21 @@ void ft_des_ecb_decrypt
t_des_ctx *ctx t_des_ctx *ctx
); );
void ft_des_ecb_decrypt_b64
(
t_des_ctx *ctx
);
void ft_des_ecb_encrypt void ft_des_ecb_encrypt
( (
t_des_ctx *ctx t_des_ctx *ctx
); );
void ft_des_ecb_encrypt_b64
(
t_des_ctx *ctx
);
void ft_des_ecb_encode_process_chunk void ft_des_ecb_encode_process_chunk
( (
t_des_ctx *ctx, t_des_ctx *ctx,

View file

@ -11,7 +11,12 @@ static void ft_base64_decode_write
t_byte1 decoded_block[FT_BASE64_ENCODE_BLOCK_SIZE]; t_byte1 decoded_block[FT_BASE64_ENCODE_BLOCK_SIZE];
ft_base64_decode_transform(ctx, block, decoded_block); ft_base64_decode_transform(ctx, block, decoded_block);
write(ctx->output_fd, decoded_block, FT_BASE64_ENCODE_BLOCK_SIZE); if (block[3] == '=' && block[2] == '=')
write(ctx->output_fd, decoded_block, FT_BASE64_ENCODE_BLOCK_SIZE - 2);
else if (block[3] == '=')
write(ctx->output_fd, decoded_block, FT_BASE64_ENCODE_BLOCK_SIZE - 1);
else
write(ctx->output_fd, decoded_block, FT_BASE64_ENCODE_BLOCK_SIZE);
} }
void ft_base64_decode_chunk void ft_base64_decode_chunk

View file

@ -60,12 +60,18 @@ void ft_des_ecb
if (ctx.decode) if (ctx.decode)
{ {
ft_des_generate_decryption_round_keys(ctx.key, ctx.round_keys); ft_des_generate_decryption_round_keys(ctx.key, ctx.round_keys);
ft_des_ecb_decrypt(&ctx); if (ctx.b64)
ft_des_ecb_decrypt_b64(&ctx);
else
ft_des_ecb_decrypt(&ctx);
} }
else else
{ {
ft_des_generate_encryption_round_keys(ctx.key, ctx.round_keys); ft_des_generate_encryption_round_keys(ctx.key, ctx.round_keys);
ft_des_ecb_encrypt(&ctx); if (ctx.b64)
ft_des_ecb_encrypt_b64(&ctx);
else
ft_des_ecb_encrypt(&ctx);
} }
exit(0); exit(0);
} }

View file

@ -25,37 +25,6 @@ static t_byte8 buffered_read
return (total_readed); return (total_readed);
} }
static void ouput
(
t_des_ctx *ctx,
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE]
)
{
if (ctx->b64)
ft_base64_decode_chunk(
&ctx->b64_ctx,
FT_DES_BYTE_BLOCK_SIZE, buffer, &ctx->b64_decode_buffer);
else
write(ctx->output_fd, buffer, FT_DES_BYTE_BLOCK_SIZE);
}
static void finish
(
t_des_ctx *ctx,
t_byte1 buffer[FT_DES_BYTE_BLOCK_SIZE]
)
{
if (ctx->b64)
{
}
else
{
if (buffer[7] < 0 || buffer[7] > 8)
ft_des_print_error("wrong padding");
write(ctx->output_fd, buffer, FT_DES_BYTE_BLOCK_SIZE - buffer[7]);
}
}
void ft_des_ecb_decrypt void ft_des_ecb_decrypt
( (
t_des_ctx *c t_des_ctx *c
@ -72,9 +41,11 @@ void ft_des_ecb_decrypt
if (readed != FT_DES_BYTE_BLOCK_SIZE) if (readed != FT_DES_BYTE_BLOCK_SIZE)
ft_des_print_error("wrong message size"); ft_des_print_error("wrong message size");
if (last_read) if (last_read)
ouput(c, message); write(c->output_fd, message, FT_DES_BYTE_BLOCK_SIZE);
ft_des_process_block(buffer, c->round_keys, message); ft_des_process_block(buffer, c->round_keys, message);
last_read = readed; last_read = readed;
} }
finish(c, message); if (message[7] < 0 || message[7] > 8)
ft_des_print_error("wrong padding");
write(c->output_fd, message, FT_DES_BYTE_BLOCK_SIZE - message[7]);
} }

View file

@ -0,0 +1,50 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "ft_des.h"
#include "ft_base64.h"
static void base64_decode
(
t_des_ctx *ctx,
int pipe_fd[2]
)
{
t_base64_ctx b64_ctx;
ft_base64_init(&b64_ctx);
b64_ctx.input_fd = ctx->input_fd;
b64_ctx.output_fd = pipe_fd[1];
ft_base64_decode(&b64_ctx);
}
static void des_ecb_decrypt
(
t_des_ctx *ctx,
int pipe_fd[2]
)
{
close(pipe_fd[1]);
ctx->input_fd = pipe_fd[0];
ft_des_ecb_decrypt(ctx);
}
void ft_des_ecb_decrypt_b64
(
t_des_ctx *ctx
)
{
pid_t pid;
int pipe_fd[2];
(void)ctx;
if (pipe(pipe_fd))
ft_des_print_error("failded to create pipe");
pid = fork();
if (pid == 0)
base64_decode(ctx, pipe_fd);
else if (pid < 0)
ft_des_print_error("failded to create child process");
else
des_ecb_decrypt(ctx, pipe_fd);
}

View file

@ -10,15 +10,7 @@ static void ft_des_ecb_write
{ {
t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE]; t_byte1 cyphertext[FT_DES_BYTE_BLOCK_SIZE];
ft_des_process_block(buffer, ctx->round_keys, cyphertext); ft_des_process_block(buffer, ctx->round_keys, cyphertext);
if (ctx->b64) write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
ft_base64_encode_chunk(
&ctx->b64_ctx,
FT_DES_BYTE_BLOCK_SIZE,
cyphertext,
&ctx->b64_encode_buffer
);
else
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
} }
void ft_des_ecb_encode_process_chunk void ft_des_ecb_encode_process_chunk

View file

@ -0,0 +1,51 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "ft_des.h"
#include "ft_base64.h"
static void base64_encode
(
t_des_ctx *ctx,
int pipe_fd[2]
)
{
t_base64_ctx b64_ctx;
close(pipe_fd[1]);
ft_base64_init(&b64_ctx);
b64_ctx.output_fd = ctx->output_fd;
b64_ctx.input_fd = pipe_fd[0];
ft_base64_encode(&b64_ctx);
}
static void des_ecb_encrypt
(
t_des_ctx *ctx,
int pipe_fd[2]
)
{
close(pipe_fd[0]);
ctx->output_fd = pipe_fd[1];
ft_des_ecb_encrypt(ctx);
}
void ft_des_ecb_encrypt_b64
(
t_des_ctx *ctx
)
{
pid_t pid;
int pipe_fd[2];
(void)ctx;
if (pipe(pipe_fd))
ft_des_print_error("failded to create pipe");
pid = fork();
if (pid == 0)
des_ecb_encrypt(ctx, pipe_fd);
else if (pid < 0)
ft_des_print_error("failded to create child process");
else
base64_encode(ctx, pipe_fd);
}

View file

@ -19,14 +19,5 @@ void ft_des_ecb_finish_encrypt
buffer_index++; buffer_index++;
} }
ft_des_process_block(ctx->buffer, ctx->round_keys, cyphertext); ft_des_process_block(ctx->buffer, ctx->round_keys, cyphertext);
if (ctx->b64) write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
{
ft_base64_encode_chunk(&ctx->b64_ctx, FT_DES_BYTE_BLOCK_SIZE,
cyphertext,
&ctx->b64_encode_buffer
);
ft_base64_encode_finish(&ctx->b64_ctx, &ctx->b64_encode_buffer);
}
else
write(ctx->output_fd, cyphertext, FT_DES_BYTE_BLOCK_SIZE);
} }

View file

@ -23,7 +23,4 @@ void ft_des_init_ctx
ctx->output_in_base64 = 0; ctx->output_in_base64 = 0;
ctx->input_fd = STDIN_FILENO; ctx->input_fd = STDIN_FILENO;
ctx->output_fd = STDOUT_FILENO; ctx->output_fd = STDOUT_FILENO;
ft_base64_init(&ctx->b64_ctx);
ft_base64_init_encode_buffer(&ctx->b64_encode_buffer);
ft_base64_init_decode_buffer(&ctx->b64_decode_buffer);
} }