为什么使用EVP加密/解密aes_128_cbc()而不是aes_128_gcm()?

时间:2018-04-17 06:25:39

标签: c authentication encryption openssl aes-gcm

下面的代码使用aes_128_cbc并正确加密代码,但当我将其更改为aes_128_gcm时,没有输出加密。下面的代码是我原来的工作代码。我的密钥是128位(长度为16),iv也是长度为16。

    #include <stdlib.h>
    #include <openssl/evp.h>
    #include <openssl/aes.h>
    #include <string.h>


    EVP_CIPHER_CTX *ctx;
    char key[]="somerandomkey123"; /*length 16*/
    char output[1024];
    char input[]= "Message here.";
    int len;
    FILE *binfile;


    if(!ctx = EVP_CIPHER_CTX_new()){
        (error message)
    }
    if(1 != EVP_EncryptInit_ex(ctx,EVP_aes_128_cbc(),NULL,key,"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\")){
        (error message)
    }
    if(1 != EVP_EncryptUpdate(ctx,output,&len,input,strlen(input))){
        (error message)
    }
    if(1 != EVP_EncryptFinal_ex(ctx, output+len, &len)){
        (error message)
    }

    EVP_CIPHER_CTX_free(ctx)

    /*This prints out 0 when I change cbc to gcm, but prints a correct    size when I use cbc*/
    printf("Output size: %d \n", len);

    /*Properly writes to file when cbc used but not when gcm used. Nothing is in encrypted.bin when gcm is used when encrypted text should be there.*/
    binfile = fopen("encrypted.bin","wb");
    fwrite(outbuf,len,1,binfile);

当我将EVP_aes_128_cbc()更改为EVP_aes_128_gcm()时,代码不再起作用。我还将iv更改为长度12(“\ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0”)。最后,我在EVP_EncryptFinal_ex之后添加了这个代码块:

    char tag[] = "1234567890123456";

    if(1 != EVP_CIPHER_CTX_ctrl(ctx,EVP_CTRL_GCM_GET_TAG,16,tag)){
        (error message)
    }

我打印的最终输出大小为0且没有任何内容(原始代码中注明)。我的问题是,当我只将cbc更改为gcm时,为什么我没有得到任何加密?是由于键/ iv尺寸问题还是更大的问题?

1 个答案:

答案 0 :(得分:0)

EncryptUpdate和EncryptFinal都添加到输出缓冲区(或不添加)。 有机会,对于您的短文本消息,在CBC模式下,所有输出都由EncryptFinal写入,而对于GCM模式,所有输出都由EncryptUpdate写入。

您的代码中的问题是,您在EncryptUpdate和EncryptFinal中重用“len”变量。它们都(通过)通过调用添加的数据量来写入len。因此在CBC模式下,EncryptFinal会将初始0重写为16,而在GCM模式下,初始值13将被0写入。

您应该按照openssl wiki(https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption)中的说明进行操作:

/* Provide the message to be encrypted, and obtain the encrypted output.
 * EVP_EncryptUpdate can be called multiple times if necessary
 */
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
  handleErrors();
ciphertext_len = len;

/* Finalise the encryption. Further ciphertext bytes may be written at
 * this stage.
 */
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) 
  handleErrors();
ciphertext_len += len;

顺便说一句:你可以继续使用strlen作为输入数据的长度,如果它只是文本,但请记住,这不会加密零终止符,所以你必须在解密后手动添加它。