简体   繁体   English

在 Linux 上使用 openssl AES 加密和解密

[英]encryption and decryption with openssl AES on Linux

I want to use AES to communicate tcp/ip.我想使用 AES 通信 tcp/ip。 However, difficulties arose in making the AES function.然而,在制作 AES function 时出现了困难。 In the process of decoding, dummy values are generated or the values are deleted.在解码过程中,会产生虚拟值或删除这些值。 I'd appreciate it if you could give me a little help.如果您能给我一点帮助,我将不胜感激。

int main(void)
{
unsigned char mykey[] = "01234567890123456789012345678\0";
unsigned  char iv[] = "0123456789012\0";
char buf[BUF_SIZE]="hi";
char enc[BUF_SIZE];
char dec[BUF_SIZE];
AES_encryption(buf,enc,mykey,iv);
AES_decryption(enc,dec,mykey,iv);
printf("buf : %s\n",buf);
printf("enc: %s\n",enc);
printf("dec: %s\n", dec);
return 0;
}


void AES_encryption(char plainfn[], char cipherfn[], unsigned char key[],unsigned char iv[])
   {

    EVP_CIPHER_CTX ctx;
    int in_len, out_len=0;
    in_len=strlen(plainfn);
    EVP_CIPHER_CTX_init(&ctx);
    EVP_CipherInit_ex(&ctx,EVP_aes_128_cbc(),NULL,key,iv,AES_ENCRYPT);
    EVP_CipherUpdate(&ctx,cipherfn,&out_len,plainfn,in_len);
    EVP_CipherFinal_ex(&ctx,cipherfn,&out_len);

    EVP_CIPHER_CTX_cleanup(&ctx);
    }

void AES_decryption(char cipherfn[], char plainfn[], unsigned char key[], unsigned char iv[])
    {

    EVP_CIPHER_CTX ctx;
    int in_len, out_len=0;
    in_len=strlen(cipherfn);
    EVP_CIPHER_CTX_init(&ctx);
    EVP_CipherInit_ex(&ctx,EVP_aes_128_cbc(),NULL,key,iv,AES_DECRYPT);
    EVP_CipherUpdate(&ctx,plainfn,&out_len,cipherfn,in_len);
    EVP_CipherFinal_ex(&ctx,plainfn,&out_len);
    EVP_CIPHER_CTX_cleanup(&ctx);
    }

These results come out.这些结果出来了。 buf: hi enc: U▒▒B▒ac▒▒]▒▒▒▒Y▒- dec: hi▒?!▒ buf: hi enc: U▒▒B▒ac▒▒]▒▒▒▒Y▒- dec: hi▒?!▒

The main problem is that AES_encryption is likely put NULL chars to the enc buffer.主要问题是 AES_encryption 可能会将 NULL 字符放入enc缓冲区。 You then count the scrambled enc buffer "string length" via strlen() in AES_decryption.然后,您通过 AES_decryption 中的strlen()计算加扰的enc缓冲区“字符串长度”。 This is certainly wrong since decryption can stop too early thus not reading the entire input buffer.这肯定是错误的,因为解密可能过早停止,因此不会读取整个输入缓冲区。

You should probably pass an buffer size argument to encrypt and decrypt functions to properly encrypt/decrypt the buffer(s).您可能应该将缓冲区大小参数传递给加密和解密函数,以正确加密/解密缓冲区。 Calculate the string length before encryption and some how pass the same buffer length also to the decryption stage.在加密之前计算字符串长度以及如何将相同的缓冲区长度也传递给解密阶段。 You probably have to encode the string length in your buf before the actual data.您可能必须在实际数据之前对buf中的字符串长度进行编码。

Also since enc buffer is by definition scrambled you can't just printf("%s",enc) it for the same reason strlen() doesn't work for it.此外,由于enc缓冲区根据定义是加扰的,因此您不能只 printf("%s",enc) 使用它,原因与 strlen() 不起作用的原因相同。 You need to print the chars one by one by putchar() or some other way that is immune to null chars.您需要通过 putchar() 或其他不受 null 字符影响的方式逐个打印字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM