【发布时间】:2016-09-13 04:29:09
【问题描述】:
我正在尝试使用 OpenSSL 库使用 Blowfish 的 CBC 块模式加密和解密字符串。由于平台原因,这需要在直接 C 而不是 C++ 中完成。由于某种原因,当输入字符串长于 8 个字符(1 个块)时,输出的大小会急剧增加。
例如:加密 abcdefgh 将生成一个长度为 8 个字符的输出 - 一切都很好。但是,加密 abcdefgha 将生成 699 个字符长的输出!
我在这个库上做错了什么吗?我的理解是 Blowfish 的输出应该与输入的大小相同。谁能解释我做错了什么?如果这是正确的,我怎么知道创建输出缓冲区有多大,因为当输入超过 1 个块时,这段代码会超出它。
下面的代码示例:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <openssl/blowfish.h>
const unsigned char* BLOWFISH_KEY = "TestKey!";
#define SIZE 8
unsigned char* blowfish_decrypt(unsigned char* in);
unsigned char* blowfish_encrypt(unsigned char* in);
int main(){
char* message = "abcdefgha";
char* encrypted = blowfish_encrypt(message);
printf("Encrypted: %s\n", encrypted);
printf("Size of encrypted: %d\n\n", strlen(encrypted));
char* decrypted = blowfish_decrypt(encrypted);
printf("Decrypt: %s\n", decrypted);
printf("Size of encrypted: %d\n\n", strlen(encrypted));
return 0;
}
unsigned char* blowfish_encrypt(unsigned char* in){
int i;
int SIZE_IN = strlen(in);
unsigned char *out = calloc(SIZE_IN+1, sizeof(char));
char ivec[8];
for(i=0; i<8; i++) ivec[i] = 'i';
BF_KEY *key = calloc(1, sizeof(BF_KEY));
/* set up a test key */
BF_set_key(key, SIZE, BLOWFISH_KEY );
BF_cbc_encrypt(in, out, strlen(in), key, ivec, BF_ENCRYPT);
printf("Size of out: %d\n", strlen(out));
printf("Size of in: %d\n", strlen(in));
return out;
}
unsigned char* blowfish_decrypt(unsigned char* in){
int i;
int SIZE_IN = strlen(in);
unsigned char *out = calloc(SIZE_IN+1, sizeof(char));
char ivec[8];
for(i=0; i<8; i++) ivec[i] = 'i';
BF_KEY *key = calloc(1, sizeof(BF_KEY));
/* set up a test key */
BF_set_key(key, SIZE, BLOWFISH_KEY );
BF_cbc_encrypt(in, out, strlen(in), key, ivec, BF_DECRYPT);
printf("Size of out: %d\n", strlen(out));
printf("Size of in: %d\n", strlen(in));
return out;
}
【问题讨论】:
标签: c encryption openssl blowfish