使用openssl C进行AES(aes-ige-128,aes-ige-192,aes-ige-256)加密/解密

ivy*_*ivy 5 c openssl aes

最近我终于(在stackoverflow用户的帮助下,@ WhozCraig)开始在CBC模式下工作AES.现在,我想用AES IGE做同样的事情.我看了看openssl-1.0.1e/test/igetest.c并尝试建立自己的测试.但是,我再次遇到输入和输出尺寸问题.一切是好的,因为我从以前的代码复制它:AES(AES-CBC-128,AES-CBC-192,AES-CBC-256)使用OpenSSLç加密/解密.

现在,当我传递一个小于32的输入长度时,它说:

Give a key length [only 128 or 192 or 256!]:
256
Give an input's length:
3
aes_ige.c(88): OpenSSL internal error, assertion failed: (length%AES_BLOCK_SIZE) == 0
 (core dumped)
Run Code Online (Sandbox Code Playgroud)

但是当长度大于32时,我也不确定它是否一切正常:

Give a key length [only 128 or 192 or 256!]:
256
Give an input's length:
48
original:       58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 
encrypt:        A4 1F C4 E8 42 5E E5 62 1A B6 C1 47 D2 2F 8D 98 D0 0B 32 77 4E 36 84 25 15 5B BA 60 EA A9 64 D2 53 D1 98 78 83 21 90 90 74 44 C7 AA 3E AD 9B 26 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
decrypt:        58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 
Run Code Online (Sandbox Code Playgroud)

下面是代码:

    #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/rand.h>

// a simple hex-print routine. could be modified to print 16 bytes-per-line
static void hex_print(const void* pv, size_t len)
{
    const unsigned char * p = (const unsigned char*)pv;
    if (NULL == pv)
        printf("NULL");
    else
    {
        size_t i = 0;
        for (; i<len;++i)
            printf("%02X ", *p++);
    }
    printf("\n");
}

// main entrypoint
int main(int argc, char **argv)
{
    int keylength;
    printf("Give a key length [only 128 or 192 or 256!]:\n");
    scanf("%d", &keylength);

    /* generate a key with a given length */
    unsigned char aes_key[keylength/8];
    memset(aes_key, 0, keylength/8);
    if (!RAND_bytes(aes_key, keylength/8))
        exit(-1);

    size_t inputslength = 0;
    printf("Give an input's length:\n");
    scanf("%lu", &inputslength);

    /* generate input with a given length */
    unsigned char aes_input[inputslength];
    memset(aes_input, 'X', inputslength);

    const size_t ivsize = AES_BLOCK_SIZE*2;

    /* init vector */
    unsigned char iv_enc[ivsize], iv_dec[ivsize];
    RAND_bytes(iv_enc, ivsize);
    memcpy(iv_dec, iv_enc, ivsize);

    // buffers for encryption and decryption
    const size_t encslength = (inputslength/AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE;//((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
    unsigned char enc_out[encslength];
    unsigned char dec_out[inputslength];
    memset(enc_out, 0, sizeof(enc_out));
    memset(dec_out, 0, sizeof(dec_out));

    AES_KEY enc_key, dec_key;
    AES_set_encrypt_key(aes_key, keylength, &enc_key);
    AES_set_decrypt_key(aes_key, keylength, &dec_key);

    AES_ige_encrypt(aes_input, enc_out, inputslength, &enc_key, iv_enc, AES_ENCRYPT);

    AES_ige_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT);

    printf("original:\t");
    hex_print(aes_input, sizeof(aes_input));

    printf("encrypt:\t");
    hex_print(enc_out, sizeof(enc_out));

    printf("decrypt:\t");
    hex_print(dec_out, sizeof(dec_out));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

最后!得到它的工作(希望如此).但是,如果有人能说,下面这段代码是100%好的话,我会感激不尽;)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/rand.h>

// a simple hex-print routine. could be modified to print 16 bytes-per-line
static void hex_print(const void* pv, size_t len)
{
    const unsigned char * p = (const unsigned char*)pv;
    if (NULL == pv)
        printf("NULL");
    else
    {
        size_t i = 0;
        for (; i<len;++i)
            printf("%02X ", *p++);
    }
    printf("\n");
}

// main entrypoint
int main(int argc, char **argv)
{
    int keylength = 256;
    //printf("Give a key length [only 128 or 192 or 256!]:\n");
    //scanf("%d", &keylength);

    /* generate a key with a given length */
    unsigned char aes_key[keylength/8];
    memset(aes_key, 0, keylength/8);
    if (!RAND_bytes(aes_key, keylength/8))
        exit(-1);

    size_t inputslength = 0;
    printf("Give an input's length:\n");
    scanf("%lu", &inputslength);

    /* generate input with a given length */
    unsigned char aes_input[inputslength];
    memset(aes_input, 'X', inputslength);

    const size_t ivsize = AES_BLOCK_SIZE*2;
    /* init vector */
    unsigned char iv_enc[ivsize], iv_dec[ivsize];
    RAND_bytes(iv_enc, ivsize);
    memcpy(iv_dec, iv_enc, ivsize);

    // buffers for encryption and decryption
    const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
    unsigned char enc_out[encslength];
    unsigned char dec_out[inputslength];
    memset(enc_out, 0, sizeof(enc_out));
    memset(dec_out, 0, sizeof(dec_out));

    // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256
    AES_KEY enc_key, dec_key;
    AES_set_encrypt_key(aes_key, keylength, &enc_key);
    AES_ige_encrypt(aes_input, enc_out, encslength, &enc_key, iv_enc, AES_ENCRYPT);

    AES_set_decrypt_key(aes_key, keylength, &dec_key);
    AES_ige_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT);

    printf("original:\t");
    hex_print(aes_input, sizeof(aes_input));

    printf("encrypt:\t");
    hex_print(enc_out, sizeof(enc_out));

    printf("decrypt:\t");
    hex_print(dec_out, sizeof(dec_out));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我只改变了这个:

AES_ige_encrypt(aes_input, enc_out, **inputslength**, &enc_key, iv_enc, AES_ENCRYPT);

进入:

AES_ige_encrypt(aes_input, enc_out, **encslength**, &enc_key, iv_enc, AES_ENCRYPT);

这是对的吗?

编辑2号;)

好的,做了另一个例子,你的建议在输入中添加了一些填充.希望现在好吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/rand.h>

// a simple hex-print routine. could be modified to print 16 bytes-per-line
static void hex_print(const void* pv, size_t len)
{
    const unsigned char * p = (const unsigned char*)pv;
    if (NULL == pv)
        printf("NULL");
    else
    {
        size_t i = 0;
        for (; i<len;++i)
            printf("%02X ", *p++);
    }
    printf("\n");
}

// main entrypoint
int main(int argc, char **argv)
{
    int keylength = 256;
    //printf("Give a key length [only 128 or 192 or 256!]:\n");
    //scanf("%d", &keylength);

    /* generate a key with a given length */
    unsigned char aes_key[keylength/8];
    memset(aes_key, 0, keylength/8);
    if (!RAND_bytes(aes_key, keylength/8))
        exit(-1);

    size_t inputslength = 0;
    printf("Give an input's length:\n");
    scanf("%lu", &inputslength);

    /* generate input with a given length */
    unsigned char aes_input[inputslength];
    memset(aes_input, 'X', inputslength);

    const size_t ivsize = AES_BLOCK_SIZE*2;
    /* init vector */
    unsigned char iv_enc[ivsize], iv_dec[ivsize];
    RAND_bytes(iv_enc, ivsize);
    memcpy(iv_dec, iv_enc, ivsize);

    // buffers for encryption and decryption
    const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;

    unsigned char paddedinput[encslength];
    memset(paddedinput, 0, encslength);
    memcpy(paddedinput, aes_input, inputslength);

    unsigned char enc_out[encslength];
    unsigned char dec_out[inputslength];
    memset(enc_out, 0, sizeof(enc_out));
    memset(dec_out, 0, sizeof(dec_out));

    AES_KEY enc_key, dec_key;
    AES_set_encrypt_key(aes_key, keylength, &enc_key);
    AES_ige_encrypt(paddedinput, enc_out, encslength, &enc_key, iv_enc, AES_ENCRYPT);

    AES_set_decrypt_key(aes_key, keylength, &dec_key);
    AES_ige_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT);

    printf("original:\t");
    hex_print(aes_input, sizeof(aes_input));

    printf("encrypt:\t");
    hex_print(enc_out, sizeof(enc_out));

    printf("decrypt:\t");
    hex_print(dec_out, sizeof(dec_out));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

The*_*ist 5

错误消息说,这一切.

aes_ige.c(88): OpenSSL internal error, assertion failed: (length%AES_BLOCK_SIZE) == 0
Run Code Online (Sandbox Code Playgroud)

这基本上是一个运行时检查(断言)失败由于提供给功能无效的输入AES_ige_encrypt()目前在源88线aes_ige.c.

OPENSSL_assert((length%AES_BLOCK_SIZE) == 0);
Run Code Online (Sandbox Code Playgroud)

断言基本上检查length(传递给函数的第3个参数)是否为整数倍AES_BLOCK_SIZE.如果是,则继续执行,否则程序将停止并打印有关失败的断言的警告.

  1. 因此,请确保传递给的数据大小AES_ige_encrypt()是其倍数AES_BLOCK_SIZE.

  2. 如果数据的大小不是整数倍,则NUL向其附加字节以使总大小为最接近的倍数AES_BLOCK_SIZE.

  3. 此外,始终将" 整数倍AES_BLOCK_SIZE "值作为length参数传递给AES_ige_encrypt().

  • 早些时候,当你调用[**`AES_cbc_encrypt()`**](http://fossies.org/dox/openssl-1.0.1e/aes__cbc_8c_source.html#l00055)时,它内部调用[**`CRYPTO_cbc128_encrypt() `**](http://fossies.org/dox/openssl-1.0.1e/cbc128_8c.html#a5cbe79f5b3a935a6e49ac72e3f3e4fb2),它对要加密的有效载荷数据的长度没有限制.因此你没有得到任何错误. (4认同)
  • 关于你对块大小**的**查询,答案取决于库/框架的实现.顾名思义,BLOCK密码需要标准块大小.当传递非标准大小时,***有效负载是否自动填充***或***会引发错误***取决于实现加密框架/库的代码. (4认同)
  • 最初[**归零一个适当长度的缓冲区**](http://pastie.org/private/yzlkcudvpwwfczvpvo8gw#51)然后将有效载荷数据复制到它看起来对我来说很好.修改`encslength`的计算如下:**`encslength =((inputslength + AES_BLOCK_SIZE -1)/ AES_BLOCK_SIZE)*AES_BLOCK_SIZE;`**.如果有效载荷**已经是'AES_BLOCK_SIZE'的精确倍数,那么较旧的逻辑会不必要地使用更大的缓冲区. (3认同)