我只想用这3种模式测试openSSL中的AES:128192和256密钥长度,但我解密的文本与我的输入不同,我不知道为什么。此外,当我传递一个巨大的输入长度(比如1024字节)时,我的程序显示核心转储
。。。我的意见总是一样的,但这并不重要,至少现在是这样。代码如下:
#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;
}
编辑:
当我将输出大小更改为inputsl长度
而不是AES_BLOCK_SIZE
时,我得到了结果:
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)?
@非常感谢你的帮助!它向我解释了很多!但还有一个问题。我将静态数组更改为动态数组。当我这样做的时候,发生了一些错误。但只有当我给出一个巨大的输入大小时,它们才会出现,看看valgrind输出:http://pastie.org/private/bzofrrtgrlzr0doyb3g.只有当我传递一个大的输入时才会发生错误,当我传递一个小的输入时(比如在你的例子中,10),就可以了。其他一切都很顺利。
#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)
{
size_t inputslength = 0;
printf("Give an input's length:\n");
scanf("%lu", &inputslength);
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 = (unsigned char*)malloc(sizeof(unsigned char) * (keylength/8));
memset(aes_key, 0, keylength/8);
RAND_bytes(aes_key, keylength/8);
// generate input with a given length
unsigned char *aes_input = (unsigned char*)malloc(sizeof(unsigned char) * (inputslength));
memset(aes_input, 'X', sizeof(aes_input));
// init vectors
unsigned char *iv_enc = (unsigned char*)malloc(sizeof(unsigned char) * (AES_BLOCK_SIZE));
unsigned char *iv_dec = (unsigned char*)malloc(sizeof(unsigned char) * (AES_BLOCK_SIZE));
// iv_dec == iv_enc
RAND_bytes(iv_enc, AES_BLOCK_SIZE);
memcpy(iv_dec, iv_enc, AES_BLOCK_SIZE);
// buffers for encryption and decryption
const size_t length = (((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE);
unsigned char *enc_out = (unsigned char*)malloc(sizeof(unsigned char) * (length));
unsigned char *dec_out = (unsigned char*)malloc(sizeof(unsigned char) * (inputslength));
// so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256
AES_KEY encKey, decKey;
AES_set_encrypt_key(aes_key, keylength, &encKey);
AES_cbc_encrypt(aes_input, enc_out, inputslength, &encKey, iv_enc, AES_ENCRYPT);
AES_set_decrypt_key(aes_key, keylength, &decKey);
AES_cbc_encrypt(enc_out, dec_out, length, &decKey, 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));
free(aes_key);
aes_key = NULL;
free(aes_input);
aes_input = NULL;
free(iv_enc);
iv_enc = NULL;
free(iv_dec);
iv_dec = NULL;
free(enc_out);
enc_out = NULL;
free(dec_out);
dec_out = NULL;
return 0;
}
编辑:
好的,我发布的上一个代码有点问题,这里有一个新的,工作得很好,即使输入量很大。再次为帮助我干杯!:)
#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 = (unsigned char*)malloc(sizeof(unsigned char) * (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 = (unsigned char*)malloc(sizeof(unsigned char) *inputslength);
memset(aes_input, 'X', inputslength);
/* init vector */
unsigned char *iv_enc = (unsigned char*)malloc(sizeof(unsigned char) *AES_BLOCK_SIZE), *iv_dec = (unsigned char*)malloc(sizeof(unsigned char) *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 = (unsigned char*)malloc(sizeof(unsigned char) *encslength);
unsigned char *dec_out = (unsigned char*)malloc(sizeof(unsigned char) *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, inputslength);
printf("encrypt:\t");
hex_print(enc_out, encslength);
printf("decrypt:\t");
hex_print(dec_out, inputslength);
// free memory here
return 0;
}
看一眼这个修改过的代码版本。注意以下内容:
AES\u cbc\u encrypt()
(请参阅调用中的最后一个参数)
源代码
#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
我真诚地希望这能有所帮助。
我希望有一个用C编写的程序,可以在没有openssl这样的大型库的帮助下,用AES-CBC对字符串进行编码/解码。 目标: 使用密码短语对字符串进行编码/解码: 因此,应用程序需要接受3个输入参数。。。 输入字符串(待编码)/或已编码字符串(待解码) 用于编码/解码字符串的密码 编码或解码指示器 我对C语言不熟悉(我可以用C#编码)。 我已经找到了https://github.com/kokke/
我用java编写了这段代码,以便解密密文。我有钥匙。对我来说,一切都是正确的,但我有我要解释的问题。 这是我的代码: 我收到以下错误: 出了什么问题?我知道这个问题在某种程度上与衬垫有关,但我不知道确切的解决方案。我只有一个密文IV和密钥。
问题内容: 我在解密以前在PHP应用程序中加密过的pgcrypto中的数据时遇到问题。 我尝试了3种加密类型: 1)mcrypt-RIJNDAEL 128 CBC 2)mcrypt-RIJNDAEL 256 CBC 3)openssl_encrypt-aes-256-cbc 一切都在PHP中很好地解密,但是在pgcrypto中,我只能使用相同的密钥和iv进行解密1)mcrypt-RIJNDAEL
我在pgcrypto中解密以前在PHP应用程序中加密的数据时遇到问题。 我尝试了3种类型的加密: 1)mcrypt-rijndael 128 cbc 2)mcrypt-rijndael 256 cbc 3)openssl_encrypt-aes-256-cbc 在PHP中,所有的东西都可以加密解密,但在pgcrypto中,我可以使用相同的密钥和iv解密,仅1)mcrypt-rijndael 128
我只需要通过代码加密AES CBC 128位模式的字符串。我使用openssl库完成了这项工作,但无法获得正确的输出。 到目前为止,我已经完成了。 我的十六进制输出是:B0 15 751B50 80 D4 FF 81 68 146BB71B95 99 37 38 但正确的输出是:< code > 73 5C 04 F9 57 18 43 7C EE 68 27 59 2B 41 A8 DA (通过
bouncyCastle新手,欢迎提供帮助。我正在尝试使用bounncycastle java API在我的系统上解密一个由第三方加密的文件。它似乎可以很好地解密文件,除了解密文件开头的一团垃圾数据。代码如下 解密的数据块看起来很好,除了开头的“?”??? 用于解密文件的openssl命令在下面的命令中运行良好。事实上,我在解密时使用的是openssl打印的密钥和iv。 加密文件中的openssl