1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#define P_BUF_SIZE 8192
#define BUF_SIZE ((P_BUF_SIZE * 3 / 4) - 24) /* 24 is the overhead of blowfish, 3/4 is the overhead of base64 */
int main(int argc, char ** argv) {
FILE *fd;
char *sReadBuf, *sWriteBuf;
short iReadSize = 0;
short result;
BIO *b64, *bmem, *bfish, *bStart;
BUF_MEM *bptr;
if(argc < 3) {
fprintf(stderr, "USAGE: %s filename key\n", argv[0]);
return 3;
}
fd = fopen(argv[1], "r");
if(NULL == fd) {
fprintf(stderr, "Can't open file \"%s\"for reading\n", argv[1]);
return 4;
}
sReadBuf = (char *) malloc(BUF_SIZE * sizeof(char) + 1);
sWriteBuf = (char *) malloc(P_BUF_SIZE * sizeof(char) + 1);
b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bfish = BIO_new(BIO_f_cipher());
BIO_set_cipher(bfish, EVP_bf_cbc(), "pouet", "pouet", 1);
bfish = BIO_push(bfish, b64);
bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
bStart = bfish;
while(0 < (iReadSize = fread(sReadBuf, sizeof(char), BUF_SIZE, fd))) {
result = BIO_write(bStart, sReadBuf, iReadSize);
BIO_flush(bStart);
if(result > 0) {
BIO_get_mem_ptr(bStart, &bptr);
memcpy(sWriteBuf, bptr->data, bptr->length);
sWriteBuf[bptr->length] = 0;
fprintf(stdout, "%s\n", sWriteBuf);
fflush(stdout);
}
else {
return 5;
}
}
BIO_free_all(bStart);
free(sReadBuf);
free(sWriteBuf);
fclose(fd);
return 0;
} |
Partager