的AES(AES-CBC-128,AES-CBC-192,AES-CBC-256)用的opensslÇ加密/解密 [英] AES (aes-cbc-128, aes-cbc-192, aes-cbc-256) encryption/decryption with openssl C

查看:400
本文介绍了的AES(AES-CBC-128,AES-CBC-192,AES-CBC-256)用的opensslÇ加密/解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想从OpenSSL的与此3种模式测试AES:用128,192和256密钥长度,但我的解密文本是从我输入的不同,我不知道为什么。此外,当我通过一个巨大的输入长度(可以说1024字节)我的方案显示核心转储 ...我的投入始终是相同的,但它不事关,至少现在。继承人的code:

I just want to test AES from openSSL with this 3 modes: with 128,192 and 256 key length but my decrypted text is different from my input and I dont know why. Also, when I pass a huge inputs length (lets say 1024 bytes) my program shows core dumped... My input is always the same but it doesnt matter, at least for now. Heres the code:

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

int main(int argc, char **argv)
{
    int i;
    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];
    memset(aes_key, 0, sizeof(aes_key));
    if (!RAND_bytes(aes_key, keylength))
    {
        exit(-1);
    }
    aes_key[keylength-1] = '\0';

    int inputslength;
    printf("Give an input's length:\n");
    scanf("%d", &inputslength);

    /* generate input with a given length */
    unsigned char aes_input[inputslength+1];
    memset(aes_input, '0', sizeof(aes_input));
    aes_input[inputslength] = '\0';

    /*printf("original:\t");
    for(i=0; i<inputslength; i++)
    {
        printf("%c ", aes_input[i]);
    }
    printf("\n");*/

    /* init vector */
    unsigned char iv[AES_BLOCK_SIZE];
    if (!RAND_bytes(iv, AES_BLOCK_SIZE))
    {
        exit(-1);
    }

    //printf("AES_BLOCK_SIZE = %d\n", AES_BLOCK_SIZE); // aes block size is 16 bytes = 128 bits
    AES_KEY enc_key, dec_key;
    unsigned char enc_out[AES_BLOCK_SIZE];
    unsigned char dec_out[AES_BLOCK_SIZE];

    // so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256
    AES_set_encrypt_key(aes_key, keylength, &enc_key);
    AES_cbc_encrypt(aes_input, enc_out, inputslength, &enc_key, iv, AES_ENCRYPT);

    AES_set_decrypt_key(aes_key, keylength, &dec_key);
    AES_decrypt(enc_out, dec_out, &dec_key);

    printf("original:\t");
    for(i=0;*(aes_input+i)!=0x00;i++)
        printf("%X ",*(aes_input+i));
    printf("\nencrypted:\t");

    for(i=0;*(enc_out+i)!=0x00;i++)
        printf("%X ",*(enc_out+i));

    printf("\ndecrypted:\t");
    for(i=0;*(dec_out+i)!=0x00;i++)
        printf("%X ",*(dec_out+i));
    printf("\n");

    /*printf("\n\noriginal:\t");
    for(i=0; i<inputslength; i++)
    {
        printf("%x ", dec_out[i]);
    }
    printf("\n");*/


    return 0;
}

编辑:

当我改变输出大小,以 inputslength 而不是 AES_BLOCK_SIZE 我得到的结果:

When I changed outputs sizes to inputslength instead of AES_BLOCK_SIZE I got results:

Give a key length [only 128 or 192 or 256!]:
128
Give an input's length:
5
original:       30 30 30 30 30 
encrypted:      94 56 50 7E 19 B2 1C CE 20 23 4A E7 10 AF DB E3 30 30 30 30 30 
decrypted:      E1 5F F4 3D E8 8D 91 19 CD 3E 22 1E AF 1C 8F 5A 94 56 50 7E 19 B2 1C CE 20 23 4A E7 10 AF DB E3 30 30 30 30 30

那么,这可能是那里有与outpus大小的问题和IV的大小?他们应该有什么尺寸(用于AES-CBC-128,AES-CBC-192,AES-CBC-256)?

So is it possible that theres an issue with outpus sizes and the size of the iv? What sizes they should have (for AES-CBC-128, AES-CBC-192, AES-CBC-256)?

推荐答案

采取偷看你的code的这款改装版。请注意以下几点:

Take a peek at this modified version of your code. Note the following:


  1. 新增hex_print(未成年人)

  2. 添加关键字缓冲区(中)适当的大小。

  3. 添加加密输出缓冲器正常大小(它必须是块大小的倍数,如果原来的源缓冲区是一个确切的块大小倍数,你仍然需要填充的一个完整的块(参见PKCS 5填充以获得更多信息)

  4. 同样IV用于加密和解密。

  5. 最后,奇怪的,因为它看起来 AES_cbc_encrypt()用于的两个的加密和解密(见呼叫的最后一个参数)。

  1. Added hex_print (minor)
  2. Added proper sizing of key buffer (medium).
  3. Added proper sizing of output encryption buffer (which must be a block-size multiple, and if original source buffer is an exact block-size multiple, you still need one full block of padding (see PKCS 5 padding for more info).
  4. Same IV used for both encrypt and decrypt.
  5. Finally, odd as it may seem AES_cbc_encrypt() is used for both encryption and decryption (see the last parameter in the call).

来源$ C ​​$ C

#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);

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

    // 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_cbc_encrypt(aes_input, enc_out, inputslength, &enc_key, iv_enc, AES_ENCRYPT);

    AES_set_decrypt_key(aes_key, keylength, &dec_key);
    AES_cbc_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;
}

测试输出

Give a key length [only 128 or 192 or 256!]:
128
Give an input's length:
10
original:   58 58 58 58 58 58 58 58 58 58 
encrypt:    A9 66 C5 24 A4 02 AB 96 08 65 F7 22 A5 FB BE 26 
decrypt:    58 58 58 58 58 58 58 58 58 58 

第二个测试输出

Give a key length [only 128 or 192 or 256!]:
128
Give an input's length:
10
original:   58 58 58 58 58 58 58 58 58 58 
encrypt:    C2 47 6D B1 A1 68 29 53 55 74 C5 CC 3F 27 0A 3F 
decrypt:    58 58 58 58 58 58 58 58 58 58 

我衷心希望这有助于。

I sincerely hope this helps.

这篇关于的AES(AES-CBC-128,AES-CBC-192,AES-CBC-256)用的opensslÇ加密/解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆