从文件读取密文时,Openssl EVP API无法解密密文(AES 128 ecb)

从文件读取密文时,Openssl EVP API无法解密密文(AES 128 ecb),c,encryption,openssl,evp-cipher,C,Encryption,Openssl,Evp Cipher,我正在尝试创建一个简单的应用程序来加密和解密一个小于10字节的小文件。我正在使用C的OpenSSL API(AES-128 ecb加密),当我加密字符串并将其保存到文件时,遇到了一个奇怪的“bug” unsigned char buffer[256]; unsigned char rcv[256]; char my_string[]={"123456"}; int bil = aes_encrypt(my_string, strlen(my_string), "1", NULL, buffer

我正在尝试创建一个简单的应用程序来加密和解密一个小于10字节的小文件。我正在使用C的OpenSSL API(AES-128 ecb加密),当我加密字符串并将其保存到文件时,遇到了一个奇怪的“bug”

unsigned char buffer[256];
unsigned char rcv[256];
char my_string[]={"123456"};

int bil = aes_encrypt(my_string, strlen(my_string), "1", NULL, buffer);

FILE* fp =fopen("encrypted_file","w");
write(fileno(fp),buffer,bil);
fclose(fp);

aes_decrypt(buffer, bil, "1", NULL, rec);

printf("%s\n",rec); /* Correct: Prints 123456 */
这里的问题是,如果我从文件中读取密文,尽管它正是我以前保存的密文,但它似乎没有被正确解密

FILE* fp =fopen("encrypted_file","r");

int bil = read(fileno(fp),buffer,256); /* The buffer contains the exact cipher that was created by the aes_encrypt in the first place */
fclose(fp);

int y = aes_decrypt(buffer, bil, "1", NULL, rec);

printf("%s\n",rec);  /* Emptry string */
加密和解密功能如下所示:

加密

int
aes_encrypt(unsigned char *plaintext, int plaintext_len, unsigned   char *key,
unsigned char *iv, unsigned char *ciphertext){
    EVP_CIPHER_CTX* ctx;

    int len;
    int ciphertext_len;

    ctx = EVP_CIPHER_CTX_new();
    EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv);
    EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len);

    ciphertext_len = len;
    EVP_EncryptFinal_ex(ctx, ciphertext + len, &len);
    ciphertext_len += len;
    EVP_CIPHER_CTX_free(ctx);
    return ciphertext_len;
}`
int
aes_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext){
    EVP_CIPHER_CTX *ctx;
    int len;
    int plaintext_len;

    ctx = EVP_CIPHER_CTX_new();
    EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv);
    EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len);

    plaintext_len = len;

    EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
    plaintext_len += len;

    EVP_CIPHER_CTX_free(ctx);

    plaintext[plaintext_len] = '\0';

    return plaintext_len;
}
解密

int
aes_encrypt(unsigned char *plaintext, int plaintext_len, unsigned   char *key,
unsigned char *iv, unsigned char *ciphertext){
    EVP_CIPHER_CTX* ctx;

    int len;
    int ciphertext_len;

    ctx = EVP_CIPHER_CTX_new();
    EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv);
    EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len);

    ciphertext_len = len;
    EVP_EncryptFinal_ex(ctx, ciphertext + len, &len);
    ciphertext_len += len;
    EVP_CIPHER_CTX_free(ctx);
    return ciphertext_len;
}`
int
aes_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext){
    EVP_CIPHER_CTX *ctx;
    int len;
    int plaintext_len;

    ctx = EVP_CIPHER_CTX_new();
    EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv);
    EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len);

    plaintext_len = len;

    EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
    plaintext_len += len;

    EVP_CIPHER_CTX_free(ctx);

    plaintext[plaintext_len] = '\0';

    return plaintext_len;
}

您不需要使用
fread
?抱歉,我不是C程序员。我猜它是从您的键指针读取128位键,而不仅仅是您给它的“1”(=16位:1和0字节),因此您的键部分基于内存中的其他数据。试着给它一个更大的键,比如说16个以上的字符。(实际上,如果您想在此处使用字符串,则应使用密钥派生函数或类似函数将字符串转换为二进制密钥。)或者查看从EVP_CIPHER_key_length()和EVP_CIPHER_CTX_key_length()获得的数字。对于AES密钥,您需要16、24或32字节。如果您的输入是密码而不是密钥,那么您需要PBKDF2(由OpenSSL实现)或
EVP_BytesToKey
,最好两者都应该具有16字节的salt和高迭代计数。正确!关键是实际问题!谢谢你的帮助@Rup:作为答案发布。让OP接受它。你不需要使用
fread
?抱歉,我不是C程序员。我猜它是从您的键指针读取128位键,而不仅仅是您给它的“1”(=16位:1和0字节),因此您的键部分基于内存中的其他数据。试着给它一个更大的键,比如说16个以上的字符。(实际上,如果您想在此处使用字符串,则应使用密钥派生函数或类似函数将字符串转换为二进制密钥。)或者查看从EVP_CIPHER_key_length()和EVP_CIPHER_CTX_key_length()获得的数字。对于AES密钥,您需要16、24或32字节。如果您的输入是密码而不是密钥,那么您需要PBKDF2(由OpenSSL实现)或
EVP_BytesToKey
,最好两者都应该具有16字节的salt和高迭代计数。正确!关键是实际问题!谢谢你的帮助@Rup:作为答案发布。让我们接受它吧。