AES encrypt/decrypt demo programs.

Next they will be part of MaxScale
This commit is contained in:
Massimiliano Pinto
2013-07-19 14:31:18 +02:00
parent 075f1a2903
commit 5989a93b0d
5 changed files with 152 additions and 0 deletions

2
aes/COMPILE_DEC Normal file
View File

@ -0,0 +1,2 @@
gcc -c aes_decode.c
gcc -o aes_decode aes_decode.o utils.o -lssl

2
aes/COMPILE_ENC Normal file
View File

@ -0,0 +1,2 @@
gcc -c aes_encode.c
gcc -o aes_encode aes_encode.o utils.o -lssl

36
aes/README Normal file
View File

@ -0,0 +1,36 @@
aes_encode and aes_decode with 256 bits key and aes_cbc_encrypt/decrypt
---------
Usage:
---------
./aes_encode '_This is a input text_'
./aes_decode 42C2BCD0FB633C2FAB58DDC65FA4412B02340F752E0C869B95D232472C9A8B27
-------------
Compilation:
-------------
openssl and openssl-dev installed.
This i part of MaxScale SkysSQL Ab project.
utils.c is needed.
Best way is copy the thow .c files and COMPILE_* as well, then:
sh COMPILE_DEC
sh COMPILE_ENC
Once the firt test it's ok it will be possible to integrate the content of aes_encode and aes_decode into MaxScale code and remove this path with the files included

48
aes/aes_decode.c Executable file
View File

@ -0,0 +1,48 @@
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <openssl/aes.h>
int main(int argc, char *argv[])
{
char *input_data = NULL;
unsigned char output[1 + 128]="";
char original_data[1 + 128]="";
char hex_output[1 + 128]="";
uint8_t encrypted_data[1 + 128] ="";
int input_len = 0;
char stored_passwd[1 + 128]="5B2A43A3F04233652E44D34D123837C3F0659AEE03254AFFD7140CED5AAE231B";
char dec_key[1 + AES_BLOCK_SIZE * 2]="12345678901234567890123456789012";
char ivdec[1 + AES_BLOCK_SIZE]="GW_SCALE_M_M_V__";
AES_KEY dectx;
if (argv[1]) {
input_data = argv[1];
} else {
input_data = stored_passwd;
}
fprintf(stderr,"OPENSSL: Input HEX to decode is [%s], %i bytes\n", input_data, strlen(input_data));
AES_set_decrypt_key(dec_key, (AES_BLOCK_SIZE * 2) * 8, &dectx);
gw_hex2bin(encrypted_data, input_data, strlen(input_data));
input_len = strlen(stored_passwd) / 2;
fprintf(stderr, "ENCRYPTED data from HEX is %i bytes long\n", input_len);
AES_cbc_encrypt(encrypted_data, original_data, input_len, &dectx, ivdec, AES_DECRYPT);
printf("\nCLEAR data is: [%s], %i bytes\n", original_data, strlen(original_data));
exit(0);
}

64
aes/aes_encode.c Executable file
View File

@ -0,0 +1,64 @@
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <openssl/aes.h>
int main(int argc, char *argv[])
{
char *input_data = NULL;
char *input_data_padded = NULL;
uint8_t output[1 + 128] = "";
char hex_output[1 + 128 * 2] ="";
int input_len = 0;
char enc_key[1 + AES_BLOCK_SIZE * 2]="12345678901234567890123456789012";
char iv[1 + AES_BLOCK_SIZE]="GW_SCALE_M_M_V__";
AES_KEY ctx;
AES_KEY dectx;
if (argv[1]) {
input_data = argv[1];
} else {
input_data = "|_AES256_input_text_|";
}
input_len = strlen(input_data) - 1 ;
/* Please note AES_BLOCK_SIZE is 16 bytes */
if (input_len >= 0)
input_len = (( input_len / AES_BLOCK_SIZE ) + 1) * AES_BLOCK_SIZE;
else
input_len = AES_BLOCK_SIZE;
if (input_len > strlen(input_data)) {
input_data_padded = calloc(1, input_len);
strcpy(input_data_padded, input_data);
} else {
input_data_padded = input_data;
}
fprintf(stderr,"OPENSSL: Input text [%s] is %i -> 16 bytes rounded is %i\n", input_data_padded, strlen(input_data_padded), input_len);
/* Setting AES 256 ecryption */
AES_set_encrypt_key(enc_key, (AES_BLOCK_SIZE * 2) * 8, &ctx);
/* Let's encrypt the input text */
AES_cbc_encrypt(input_data_padded, output, input_len, &ctx, iv, AES_ENCRYPT);
/* Convert binary data to HEX: output size is twice the inoput */
gw_bin2hex(hex_output, output, input_len);
printf("\nEncrypted HEX is [%s]: keep it!\n", hex_output);
exit(0);
}